2025-05-10 12:30:35 -04:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Service;
|
|
|
|
|
|
|
|
use PDOException;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Incoviba\Common\Ideal;
|
|
|
|
use Incoviba\Common\Implement\Exception\EmptyResult;
|
2025-05-13 16:06:09 -04:00
|
|
|
use Incoviba\Exception\ServiceAction\{Create, Read, Update};
|
2025-05-10 12:30:35 -04:00
|
|
|
use Incoviba\Repository;
|
|
|
|
use Incoviba\Model;
|
|
|
|
|
|
|
|
class Job extends Ideal\Service
|
|
|
|
{
|
|
|
|
public function __construct(LoggerInterface $logger, protected Repository\Job $jobRepository)
|
|
|
|
{
|
|
|
|
parent::__construct($logger);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $configuration
|
|
|
|
* @return Model\Job
|
|
|
|
* @throws Create
|
|
|
|
*/
|
|
|
|
public function add(array $configuration): Model\Job
|
|
|
|
{
|
|
|
|
try {
|
2025-05-13 20:19:03 -04:00
|
|
|
$data = [
|
|
|
|
'configuration' => json_encode($configuration)
|
|
|
|
];
|
|
|
|
$job = $this->jobRepository->create($data);
|
2025-05-10 12:30:35 -04:00
|
|
|
return $this->process($this->jobRepository->save($job));
|
|
|
|
} catch (PDOException $exception) {
|
|
|
|
throw new Create(__CLASS__, $exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @throws Read
|
|
|
|
*/
|
|
|
|
public function getPending(): array
|
|
|
|
{
|
|
|
|
try {
|
2025-05-12 19:46:09 -04:00
|
|
|
return array_map([$this, 'process'],$this->jobRepository->fetchPending());
|
2025-05-10 12:30:35 -04:00
|
|
|
} catch (EmptyResult $exception) {
|
|
|
|
throw new Read(__CLASS__, $exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-13 16:06:09 -04:00
|
|
|
/**
|
|
|
|
* @param Model\Job $job
|
|
|
|
* @return bool
|
|
|
|
* @throws Update
|
|
|
|
*/
|
|
|
|
public function execute(Model\Job $job): bool
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->jobRepository->edit($job, ['executed' => true]);
|
|
|
|
return true;
|
|
|
|
} catch (EmptyResult | PDOException $exception) {
|
|
|
|
throw new Update(__CLASS__, $exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-10 12:30:35 -04:00
|
|
|
protected function process(Model\Job $job): Model\Job
|
|
|
|
{
|
|
|
|
return $job;
|
|
|
|
}
|
|
|
|
}
|