Job Queue
This commit is contained in:
51
app/src/Service/Job.php
Normal file
51
app/src/Service/Job.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use PDOException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Exception\ServiceAction\{Create,Read};
|
||||
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 {
|
||||
$job = $this->jobRepository->create(compact('configuration'));
|
||||
return $this->process($this->jobRepository->save($job));
|
||||
} catch (PDOException $exception) {
|
||||
throw new Create(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws Read
|
||||
*/
|
||||
public function getPending(): array
|
||||
{
|
||||
try {
|
||||
return array_merge([$this, 'process'],$this->jobRepository->fetchPending());
|
||||
} catch (EmptyResult $exception) {
|
||||
throw new Read(__CLASS__, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
protected function process(Model\Job $job): Model\Job
|
||||
{
|
||||
return $job;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user