2025-06-30 15:52:28 -04:00
< ? php
namespace Incoviba\Command\Queue ;
use Throwable ;
2025-07-15 22:56:48 -04:00
use Psr\Log\LoggerInterface ;
2025-06-30 15:52:28 -04:00
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
{
2025-07-15 22:56:48 -04:00
public function __construct ( protected LoggerInterface $logger , protected Service\Job $jobService , ? string $name = null )
2025-06-30 15:52:28 -04:00
{
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' );
2025-07-15 22:56:48 -04:00
$this -> addOption ( 'files' , 'f' , Console\Input\InputOption :: VALUE_REQUIRED | Console\Input\InputOption :: VALUE_IS_ARRAY , 'Paths to jobs configurations files with JSON array content' );
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
2025-07-15 22:56:48 -04:00
{
return [
... $this -> getFilesConfigurations ( $input ),
... $this -> getOptionConfigurations ( $input ),
];
}
protected function getFilesConfigurations ( Console\Input\InputInterface $input ) : array
2025-07-15 22:19:07 -04:00
{
$configurations = [];
2025-07-15 22:56:48 -04:00
$files = $input -> getOption ( 'files' );
if ( $files === null ) {
return $configurations ;
}
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 ]);
2025-07-15 22:19:07 -04:00
}
}
2025-07-15 22:56:48 -04:00
return $configurations ;
}
protected function getOptionConfigurations ( Console\Input\InputInterface $input ) : array
{
$configurations = [];
2025-07-15 22:19:07 -04:00
$configOptions = $input -> getOption ( 'configurations' );
2025-07-15 22:56:48 -04:00
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 ]);
}
2025-07-15 22:19:07 -04:00
}
return $configurations ;
}
2025-07-15 22:56:48 -04:00
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' ]);
}
2025-06-30 15:52:28 -04:00
}