diff --git a/cli/src/Command/Queue/Push.php b/cli/src/Command/Queue/Push.php index d0f2fd6..d0bfa63 100644 --- a/cli/src/Command/Queue/Push.php +++ b/cli/src/Command/Queue/Push.php @@ -2,13 +2,14 @@ namespace Incoviba\Command\Queue; use Throwable; +use Psr\Log\LoggerInterface; use Symfony\Component\Console; use Incoviba\Service; #[Console\Attribute\AsCommand(name: 'queue:push', description: 'Push a job to the queue')] class Push extends Console\Command\Command { - public function __construct(protected Service\Job $jobService, ?string $name = null) + public function __construct(protected LoggerInterface $logger, protected Service\Job $jobService, ?string $name = null) { parent::__construct($name); } @@ -16,7 +17,7 @@ class Push extends Console\Command\Command protected function configure(): void { $this->addOption('configurations', 'c', Console\Input\InputOption::VALUE_REQUIRED | Console\Input\InputOption::VALUE_IS_ARRAY, 'Job configuration options array, each job configuration must be in valid JSON format'); - $this->addOption('file', 'f', Console\Input\InputOption::VALUE_REQUIRED, 'Path to jobs configuration file with JSON array'); + $this->addOption('files', 'f', Console\Input\InputOption::VALUE_REQUIRED | Console\Input\InputOption::VALUE_IS_ARRAY, 'Paths to jobs configurations files with JSON array content'); } protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int @@ -49,19 +50,72 @@ class Push extends Console\Command\Command } protected function getConfigurations(Console\Input\InputInterface $input): array + { + return [ + ...$this->getFilesConfigurations($input), + ...$this->getOptionConfigurations($input), + ]; + } + protected function getFilesConfigurations(Console\Input\InputInterface $input): array { $configurations = []; - $filePath = $input->getOption('file'); - if ($filePath !== null and file_exists($filePath)) { - $json = file_get_contents($filePath); - if (json_validate($json)) { - $configurations = array_map(fn($configArray) => json_encode($configArray), json_decode($json, true)); - } + $files = $input->getOption('files'); + if ($files === null) { + return $configurations; } - $configOptions = $input->getOption('configurations'); - if ($configOptions !== null) { - $configurations = array_merge($configurations, array_filter($configOptions, fn($config) => json_validate($config))); + foreach ($files as $filePath) { + if (!file_exists($filePath)) { + continue; + } + $configurations = array_merge($configurations, $this->getFileConfigurations($filePath)); } return $configurations; } + protected function getFileConfigurations(string $filePath): array + { + $configurations = []; + if (!file_exists($filePath)) { + return $configurations; + } + $json = file_get_contents($filePath); + if (!json_validate($json)) { + return $configurations; + } + $tmp = json_decode($json, true); + foreach ($tmp as $config) { + try { + $configurations []= $this->processConfiguration(json_encode($config)); + } catch (Throwable $exception) { + $this->logger->warning($exception->getMessage(), ['exception' => $exception, 'config' => $config]); + } + } + return $configurations; + } + protected function getOptionConfigurations(Console\Input\InputInterface $input): array + { + $configurations = []; + $configOptions = $input->getOption('configurations'); + if ($configOptions === null) { + return $configurations; + } + foreach ($configOptions as $config) { + try { + $configurations []= $this->processConfiguration($config); + } catch (Throwable $exception) { + $this->logger->warning($exception->getMessage(), ['exception' => $exception, 'config' => $config]); + } + } + return $configurations; + } + protected function processConfiguration(string $configuration): string + { + $json = json_decode($configuration, true); + if (!array_key_exists('type', $json) and !array_key_exists('configuration', $json)) { + throw new Console\Exception\InvalidArgumentException('Missing type or configuration key in JSON'); + } + if (array_key_exists('type', $json)) { + return json_encode($json); + } + return json_encode($json['configuration']); + } }