Skip to content

Commit ac89c6e

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

File tree

2 files changed

+25
-40
lines changed

2 files changed

+25
-40
lines changed

src/Service/InstallationRunner.php

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,29 @@ 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 = [InitOption::INITIAL_API_KEY->asCliFlag() => null];
29+
return $this->run($initCommand, $io, $initCommandInput, ImportedConfig::notImported());
2930
}
3031

3132
/** @inheritDoc */
3233
public function runUpdate(Command|null $initCommand, SymfonyStyle $io): int
3334
{
3435
$importConfig = $this->assetsHandler->resolvePreviousConfig($io);
35-
return $this->run($initCommand, $io, isUpdate: true, importedConfig: $importConfig);
36+
37+
// Check if a cached config file exists and drop it if so
38+
$this->assetsHandler->dropCachedConfigIfAny($io);
39+
$this->assetsHandler->importShlinkAssetsFromPath($io, $importConfig->importPath);
40+
41+
$initCommandInput = [
42+
InitOption::SKIP_INITIALIZE_DB->asCliFlag() => null,
43+
InitOption::CLEAR_DB_CACHE->asCliFlag() => null,
44+
];
45+
46+
if ($this->assetsHandler->roadRunnerBinaryExistsInPath($importConfig->importPath)) {
47+
$initCommandInput[InitOption::DOWNLOAD_RR_BINARY->asCliFlag()] = null;
48+
}
49+
50+
return $this->run($initCommand, $io, $initCommandInput, $importConfig);
3651
}
3752

3853
/**
@@ -41,20 +56,14 @@ public function runUpdate(Command|null $initCommand, SymfonyStyle $io): int
4156
private function run(
4257
Command|null $initCommand,
4358
SymfonyStyle $io,
44-
bool $isUpdate,
59+
array $initCommandInput,
4560
ImportedConfig $importedConfig,
4661
): int {
4762
$io->text([
4863
'<info>Welcome to Shlink!!</info>',
4964
'This tool will guide you through the installation process.',
5065
]);
5166

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-
}
5867
$config = $this->configGenerator->generateConfigInteractively($io, $importedConfig->importedConfig);
5968
$normalizedConfig = Utils::normalizeAndKeepEnvVarKeys($config);
6069

@@ -63,31 +72,12 @@ private function run(
6372
$io->text('<info>Custom configuration properly generated!</info>');
6473
$io->newLine();
6574

66-
if (! $this->execInitCommand($initCommand, $io, $isUpdate, $importedConfig)) {
75+
$initCommandResult = $initCommand?->run(new ArrayInput($initCommandInput), $io);
76+
if ($initCommandResult !== Command::SUCCESS) {
6777
return Command::FAILURE;
6878
}
6979

7080
$io->success('Installation complete!');
7181
return Command::SUCCESS;
7282
}
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-
}
9383
}

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('--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)