diff --git a/app/code/Magento/Downloadable/Console/Command/DomainsAddCommand.php b/app/code/Magento/Downloadable/Console/Command/DomainsAddCommand.php index 7155adeadaf09..6e7ac77b8b261 100644 --- a/app/code/Magento/Downloadable/Console/Command/DomainsAddCommand.php +++ b/app/code/Magento/Downloadable/Console/Command/DomainsAddCommand.php @@ -1,18 +1,19 @@ domainManager = $domainManager; + $this->downloadManager = $downloadManager; parent::__construct(); } @@ -69,27 +78,24 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { try { - if ($input->getArgument(self::INPUT_KEY_DOMAINS)) { - $whitelistBefore = $this->domainManager->getDomains(); - $newDomains = $input->getArgument(self::INPUT_KEY_DOMAINS); - $newDomains = array_filter(array_map('trim', $newDomains), 'strlen'); + $domains = $input->getArgument(self::INPUT_KEY_DOMAINS); + + $this->downloadManager->validateDomains($domains); - $this->domainManager->addDomains($newDomains); + $whitelistBefore = $this->domainManager->getDomains(); + $newDomains = array_filter(array_map('trim', $domains), 'strlen'); - foreach (array_diff($this->domainManager->getDomains(), $whitelistBefore) as $newHost) { - $output->writeln( - $newHost . ' was added to the whitelist.' . PHP_EOL - ); - } + $this->domainManager->addDomains($newDomains); + + foreach (array_diff($this->domainManager->getDomains(), $whitelistBefore) as $newHost) { + $output->writeln($newHost . ' was added to the whitelist.' . PHP_EOL); } + + return Cli::RETURN_SUCCESS; + } catch (InvalidArgumentException $e) { + return $this->downloadManager->handleInvalidArgumentException($e, $output); } catch (Exception $e) { - $output->writeln('' . $e->getMessage() . ''); - if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { - $output->writeln($e->getTraceAsString()); - } - return Cli::RETURN_FAILURE; + return $this->downloadManager->handleException($e, $output); } - - return Cli::RETURN_SUCCESS; } } diff --git a/app/code/Magento/Downloadable/Console/Command/DomainsRemoveCommand.php b/app/code/Magento/Downloadable/Console/Command/DomainsRemoveCommand.php index d3acb8e4ae75a..d4e631656ee2e 100644 --- a/app/code/Magento/Downloadable/Console/Command/DomainsRemoveCommand.php +++ b/app/code/Magento/Downloadable/Console/Command/DomainsRemoveCommand.php @@ -1,18 +1,19 @@ domainManager = $domainManager; + $this->downloadManager = $downloadManager; parent::__construct(); } @@ -70,26 +79,24 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { try { - if ($input->getArgument(self::INPUT_KEY_DOMAINS)) { - $whitelistBefore = $this->domainManager->getDomains(); - $removeDomains = $input->getArgument(self::INPUT_KEY_DOMAINS); - $removeDomains = array_filter(array_map('trim', $removeDomains), 'strlen'); - $this->domainManager->removeDomains($removeDomains); + $domains = $input->getArgument(self::INPUT_KEY_DOMAINS); + + $this->downloadManager->validateDomains($domains); - foreach (array_diff($whitelistBefore, $this->domainManager->getDomains()) as $removedHost) { - $output->writeln( - $removedHost . ' was removed from the whitelist.' - ); - } + $whitelistBefore = $this->domainManager->getDomains(); + $removedDomains = array_filter(array_map('trim', $domains), 'strlen'); + + $this->domainManager->removeDomains($removedDomains); + + foreach (array_intersect($removedDomains, $whitelistBefore) as $removedHost) { + $output->writeln($removedHost . ' was removed from the whitelist.' . PHP_EOL); } + + return Cli::RETURN_SUCCESS; + } catch (InvalidArgumentException $e) { + return $this->downloadManager->handleInvalidArgumentException($e, $output); } catch (Exception $e) { - $output->writeln('' . $e->getMessage() . ''); - if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { - $output->writeln($e->getTraceAsString()); - } - return Cli::RETURN_FAILURE; + return $this->downloadManager->handleException($e, $output); } - - return Cli::RETURN_SUCCESS; } } diff --git a/app/code/Magento/Downloadable/Helper/Download.php b/app/code/Magento/Downloadable/Helper/Download.php index ce2ff2c7117ab..7627512115711 100644 --- a/app/code/Magento/Downloadable/Helper/Download.php +++ b/app/code/Magento/Downloadable/Helper/Download.php @@ -1,16 +1,20 @@ writeln('' . $e->getMessage() . ''); + if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { + $output->writeln($e->getTraceAsString()); + } + return Cli::RETURN_FAILURE; + } + + /** + * Validate the input domains array + * + * @param array $domains + * @return void + * @throws InvalidArgumentException + */ + public function validateDomains(array $domains): void + { + if (empty($domains)) { + throw new InvalidArgumentException('Error: Domains parameter is missing.'); + } + } + + /** + * Handle the \InvalidArgumentException exception. + * + * @param InvalidArgumentException $e + * @param OutputInterface $output + * @return int + */ + public function handleInvalidArgumentException(InvalidArgumentException $e, OutputInterface $output): int + { + $output->writeln('' . $e->getMessage() . ''); + return Cli::RETURN_FAILURE; + } }