2025-06-30 15:52:28 -04:00
< ? php
namespace Incoviba\Command\Queue ;
use Throwable ;
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 )
{
parent :: __construct ( $name );
}
protected function configure () : void
{
2025-07-15 22:19:07 -04:00
$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' );
2025-06-30 15:52:28 -04:00
}
protected function execute ( Console\Input\InputInterface $input , Console\Output\OutputInterface $output ) : int
{
$io = new Console\Style\SymfonyStyle ( $input , $output );
$io -> title ( " Pushing job " );
2025-07-15 22:19:07 -04:00
$configurations = $this -> getConfigurations ( $input );
if ( count ( $configurations ) === 0 ) {
2025-06-30 15:52:28 -04:00
$io -> error ( 'Missing configurations' );
return self :: FAILURE ;
}
$result = self :: SUCCESS ;
foreach ( $configurations as $configuration ) {
if ( ! json_validate ( $configuration )) {
$io -> error ( " Invalid JSON: { $configuration } " );
continue ;
}
$configuration = json_decode ( $configuration , true );
try {
$job = $this -> jobService -> push ( $configuration );
$io -> success ( " Job pushed with ID { $job [ 'id' ] } " );
} catch ( Throwable $exception ) {
$io -> error ( $exception -> getMessage ());
$result = self :: FAILURE ;
}
}
return $result ;
}
2025-07-15 22:19:07 -04:00
protected function getConfigurations ( 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 ));
}
}
$configOptions = $input -> getOption ( 'configurations' );
if ( $configOptions !== null ) {
$configurations = array_merge ( $configurations , array_filter ( $configOptions , fn ( $config ) => json_validate ( $config )));
}
return $configurations ;
}
2025-06-30 15:52:28 -04:00
}