Skip to content

Commit 4d235be

Browse files
committed
Reduce conditional code in InstallationRunner
1 parent e0163d6 commit 4d235be

File tree

2 files changed

+28
-40
lines changed

2 files changed

+28
-40
lines changed

src/Service/InstallationRunner.php

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,32 @@ public function __construct(
2525
/** @inheritDoc */
2626
public function runInstallation(Command|null $initCommand, SymfonyStyle $io): int
2727
{
28-
return $this->run($initCommand, $io, isUpdate: false, importedConfig: ImportedConfig::notImported());
28+
$initCommandInput = [
29+
InitOption::DOWNLOAD_RR_BINARY->asCliFlag() => null,
30+
InitOption::INITIAL_API_KEY->asCliFlag() => null,
31+
];
32+
return $this->run($initCommand, $io, $initCommandInput, ImportedConfig::notImported());
2933
}
3034

3135
/** @inheritDoc */
3236
public function runUpdate(Command|null $initCommand, SymfonyStyle $io): int
3337
{
3438
$importConfig = $this->assetsHandler->resolvePreviousConfig($io);
35-
return $this->run($initCommand, $io, isUpdate: true, importedConfig: $importConfig);
39+
40+
// Check if a cached config file exists and drop it if so
41+
$this->assetsHandler->dropCachedConfigIfAny($io);
42+
$this->assetsHandler->importShlinkAssetsFromPath($io, $importConfig->importPath);
43+
44+
$initCommandInput = [
45+
InitOption::SKIP_INITIALIZE_DB->asCliFlag() => null,
46+
InitOption::CLEAR_DB_CACHE->asCliFlag() => null,
47+
];
48+
49+
if ($this->assetsHandler->roadRunnerBinaryExistsInPath($importConfig->importPath)) {
50+
$initCommandInput[InitOption::DOWNLOAD_RR_BINARY->asCliFlag()] = null;
51+
}
52+
53+
return $this->run($initCommand, $io, $initCommandInput, $importConfig);
3654
}
3755

3856
/**
@@ -41,20 +59,14 @@ public function runUpdate(Command|null $initCommand, SymfonyStyle $io): int
4159
private function run(
4260
Command|null $initCommand,
4361
SymfonyStyle $io,
44-
bool $isUpdate,
62+
array $initCommandInput,
4563
ImportedConfig $importedConfig,
4664
): int {
4765
$io->text([
4866
'<info>Welcome to Shlink!!</info>',
4967
'This tool will guide you through the installation process.',
5068
]);
5169

52-
// Check if a cached config file exists and drop it if so
53-
$this->assetsHandler->dropCachedConfigIfAny($io);
54-
55-
if ($isUpdate) {
56-
$this->assetsHandler->importShlinkAssetsFromPath($io, $importedConfig->importPath);
57-
}
5870
$config = $this->configGenerator->generateConfigInteractively($io, $importedConfig->importedConfig);
5971
$normalizedConfig = Utils::normalizeAndKeepEnvVarKeys($config);
6072

@@ -63,31 +75,12 @@ private function run(
6375
$io->text('<info>Custom configuration properly generated!</info>');
6476
$io->newLine();
6577

66-
if (! $this->execInitCommand($initCommand, $io, $isUpdate, $importedConfig)) {
78+
$initCommandResult = $initCommand?->run(new ArrayInput($initCommandInput), $io);
79+
if ($initCommandResult !== Command::SUCCESS) {
6780
return Command::FAILURE;
6881
}
6982

7083
$io->success('Installation complete!');
7184
return Command::SUCCESS;
7285
}
73-
74-
private function execInitCommand(
75-
Command|null $initCommand,
76-
SymfonyStyle $io,
77-
bool $isUpdate,
78-
ImportedConfig $importedConfig,
79-
): bool {
80-
$input = [
81-
InitOption::SKIP_INITIALIZE_DB->asCliFlag() => $isUpdate,
82-
InitOption::CLEAR_DB_CACHE->asCliFlag() => $isUpdate,
83-
InitOption::DOWNLOAD_RR_BINARY->asCliFlag() =>
84-
$isUpdate && $this->assetsHandler->roadRunnerBinaryExistsInPath($importedConfig->importPath),
85-
];
86-
87-
if (! $isUpdate) {
88-
$input[InitOption::INITIAL_API_KEY->asCliFlag()] = null;
89-
}
90-
91-
return $initCommand?->run(new ArrayInput($input), $io) === Command::SUCCESS;
92-
}
9386
}

test/Service/InstallationRunnerTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ class InstallationRunnerTest extends TestCase
2828
protected function setUp(): void
2929
{
3030
$this->initCommand = $this->createMock(Command::class);
31-
3231
$this->assetsHandler = $this->createMock(ShlinkAssetsHandlerInterface::class);
33-
$this->assetsHandler->expects($this->once())->method('dropCachedConfigIfAny');
34-
3532
$this->configWriter = $this->createMock(ConfigWriterInterface::class);
3633

3734
$configGenerator = $this->createStub(ConfigGeneratorInterface::class);
@@ -45,16 +42,13 @@ public function installationIsExecutedAsExpected(): void
4542
{
4643
$this->initCommand->expects($this->once())->method('run')->with(
4744
$this->callback(function (ArrayInput $input) {
48-
49-
Assert::assertEquals(
50-
'--skip-initialize-db --clear-db-cache --download-rr-binary --initial-api-key',
51-
$input->__toString(),
52-
);
45+
Assert::assertEquals('--download-rr-binary --initial-api-key', $input->__toString());
5346
return true;
5447
}),
5548
$this->anything(),
5649
)->willReturn(Command::SUCCESS);
5750

51+
$this->assetsHandler->expects($this->never())->method('dropCachedConfigIfAny');
5852
$this->assetsHandler->expects($this->never())->method('resolvePreviousConfig');
5953
$this->assetsHandler->expects($this->never())->method('roadRunnerBinaryExistsInPath');
6054
$this->assetsHandler->expects($this->never())->method('importShlinkAssetsFromPath');
@@ -75,6 +69,7 @@ public function updateIsExecutedAsExpected(bool $rrBinExists, string $postUpdate
7569
$this->anything(),
7670
)->willReturn(0);
7771

72+
$this->assetsHandler->expects($this->once())->method('dropCachedConfigIfAny');
7873
$this->assetsHandler->expects($this->once())->method('resolvePreviousConfig')->willReturn(
7974
ImportedConfig::notImported(),
8075
);
@@ -90,11 +85,11 @@ public static function provideCommands(): iterable
9085
{
9186
yield 'update with no rr binary' => [
9287
false,
93-
'--skip-initialize-db=1 --clear-db-cache=1 --download-rr-binary',
88+
'--skip-initialize-db --clear-db-cache',
9489
];
9590
yield 'update with rr binary' => [
9691
true,
97-
'--skip-initialize-db=1 --clear-db-cache=1 --download-rr-binary=1',
92+
'--skip-initialize-db --clear-db-cache --download-rr-binary',
9893
];
9994
}
10095
}

0 commit comments

Comments
 (0)