Files
emails/api/common/Service/Jobs.php
2022-11-29 08:57:56 -03:00

71 lines
1.8 KiB
PHP

<?php
namespace ProVM\Common\Service;
use PDOException;
use ProVM\Common\Exception\Database\BlankResult;
use Safe\DateTimeImmutable;
use ProVM\Emails\Repository\Job;
class Jobs extends Base
{
public function __construct(Job $repository)
{
$this->setRepository($repository);
}
protected Job $repository;
public function getRepository(): Job
{
return $this->repository;
}
public function setRepository(Job $repository): Jobs
{
$this->repository = $repository;
return $this;
}
public function schedule(int $message_id): bool
{
$data = [
'message_id' => $message_id,
'date_time' => (new DateTimeImmutable())->format('Y-m-d H:i:s')
];
try {
$job = $this->getRepository()->create($data);
$this->getRepository()->save($job);
return true;
} catch (PDOException $e) {
return false;
}
}
public function getPending(): array
{
return $this->getRepository()->fetchAllPending();
}
public function isPending(int $message_id): bool
{
try {
$this->getRepository()->fetchPendingByMessage($message_id);
return true;
} catch (BlankResult $e) {
return false;
}
}
public function find(int $message_id): \ProVM\Emails\Model\Job
{
return $this->getRepository()->fetchPendingByMessage($message_id);
}
public function execute(int $job_id): bool
{
try {
$job = $this->getRepository()->fetchById($job_id);
$job->wasExecuted();
$this->getRepository()->save($job);
return true;
} catch (PDOException $e) {
return false;
}
}
}