From 4986c3c6bb98223da6d99d55fa865a961cc42bc1 Mon Sep 17 00:00:00 2001 From: Romain Monteil Date: Tue, 3 Dec 2019 02:27:00 +0100 Subject: [PATCH] Move Exception into getConfiguration method --- .gitignore | 1 + Command/DeleteObsoleteCommand.php | 4 +--- Command/DownloadCommand.php | 9 +++------ Command/ExtractCommand.php | 4 +--- Command/StatusCommand.php | 4 +--- Service/ConfigurationManager.php | 4 ++-- Tests/Unit/Service/ConfigurationManagerTest.php | 5 ++++- TranslationBundle.php | 2 +- 8 files changed, 14 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 772214e8..4d522194 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /phpunit.xml /vendor/ .php_cs.cache +.phpunit.result.cache diff --git a/Command/DeleteObsoleteCommand.php b/Command/DeleteObsoleteCommand.php index 834963b7..67a9dfda 100644 --- a/Command/DeleteObsoleteCommand.php +++ b/Command/DeleteObsoleteCommand.php @@ -80,9 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $locales = [$inputLocale]; } - if (null === $config = $this->configurationManager->getConfiguration($configName)) { - throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName)); - } + $config = $this->configurationManager->getConfiguration($configName); $this->configureBundleDirs($input, $config); $this->catalogueManager->load($this->catalogueFetcher->getCatalogues($config, $locales)); diff --git a/Command/DownloadCommand.php b/Command/DownloadCommand.php index 21d4265f..02215e9b 100644 --- a/Command/DownloadCommand.php +++ b/Command/DownloadCommand.php @@ -66,16 +66,13 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $configName = $input->getArgument('configuration'); + $config = $this->configurationManager->getConfiguration($configName); $storage = $this->getStorage($configName); - if (null === $configuration = $this->configurationManager->getConfiguration($configName)) { - throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName)); - } - - $this->configureBundleDirs($input, $configuration); + $this->configureBundleDirs($input, $config); if ($input->getOption('cache')) { - $translationsDirectory = $configuration->getOutputDir(); + $translationsDirectory = $config->getOutputDir(); $md5BeforeDownload = $this->hashDirectory($translationsDirectory); $storage->download(); $md5AfterDownload = $this->hashDirectory($translationsDirectory); diff --git a/Command/ExtractCommand.php b/Command/ExtractCommand.php index a34ca551..16c77dbd 100644 --- a/Command/ExtractCommand.php +++ b/Command/ExtractCommand.php @@ -91,9 +91,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $configName = $input->getArgument('configuration'); - if (null === $config = $this->configurationManager->getConfiguration($configName)) { - throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName)); - } + $config = $this->configurationManager->getConfiguration($configName); $locales = []; if ($inputLocale = $input->getArgument('locale')) { diff --git a/Command/StatusCommand.php b/Command/StatusCommand.php index db3b37ad..157cd480 100644 --- a/Command/StatusCommand.php +++ b/Command/StatusCommand.php @@ -72,9 +72,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $configName = $input->getArgument('configuration'); - if (null === $config = $this->configurationManager->getConfiguration($configName)) { - throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName)); - } + $config = $this->configurationManager->getConfiguration($configName); $this->configureBundleDirs($input, $config); diff --git a/Service/ConfigurationManager.php b/Service/ConfigurationManager.php index 2e18da65..c4b22386 100644 --- a/Service/ConfigurationManager.php +++ b/Service/ConfigurationManager.php @@ -33,7 +33,7 @@ public function addConfiguration(string $name, Configuration $configuration): vo /** * @param string|string[]|null $name */ - public function getConfiguration($name = null): ?Configuration + public function getConfiguration($name = null): Configuration { if (empty($name)) { return $this->getConfiguration('default'); @@ -50,7 +50,7 @@ public function getConfiguration($name = null): ?Configuration } } - return null; + throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $name)); } public function getFirstName(): ?string diff --git a/Tests/Unit/Service/ConfigurationManagerTest.php b/Tests/Unit/Service/ConfigurationManagerTest.php index 30464abb..d553e98b 100644 --- a/Tests/Unit/Service/ConfigurationManagerTest.php +++ b/Tests/Unit/Service/ConfigurationManagerTest.php @@ -52,7 +52,10 @@ public function testGetConfigurationMissing(): void $manager->addConfiguration('bar', $this->createConfiguration()); $manager->addConfiguration('default', $correctConfiguration); - $this->assertNull($manager->getConfiguration('missing')); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('No configuration found for "missing"'); + + $manager->getConfiguration('missing'); } public function testFirstName(): void diff --git a/TranslationBundle.php b/TranslationBundle.php index 2c4cfff8..5b355437 100644 --- a/TranslationBundle.php +++ b/TranslationBundle.php @@ -27,7 +27,7 @@ */ class TranslationBundle extends Bundle { - public function build(ContainerBuilder $container) + public function build(ContainerBuilder $container): void { $container->addCompilerPass(new SymfonyProfilerPass()); $container->addCompilerPass(new ValidatorVisitorPass());