Full implemantation
This commit is contained in:
108
api/src/Repository/Job.php
Normal file
108
api/src/Repository/Job.php
Normal file
@ -0,0 +1,108 @@
|
||||
<?php
|
||||
namespace ProVM\Emails\Repository;
|
||||
|
||||
use PDO;
|
||||
use ProVM\Common\Factory\Model;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Safe\DateTimeImmutable;
|
||||
use ProVM\Common\Define\Model as ModelInterface;
|
||||
use ProVM\Common\Implement\Repository;
|
||||
use ProVM\Emails\Model\Job as BaseModel;
|
||||
|
||||
class Job extends Repository
|
||||
{
|
||||
public function __construct(PDO $connection, LoggerInterface $logger, Model $factory)
|
||||
{
|
||||
parent::__construct($connection, $logger);
|
||||
$this->setFactory($factory)
|
||||
->setTable('attachments_jobs');
|
||||
}
|
||||
|
||||
protected \ProVM\Common\Factory\Model $factory;
|
||||
public function getFactory(): \ProVM\Common\Factory\Model
|
||||
{
|
||||
return $this->factory;
|
||||
}
|
||||
public function setFactory(\ProVM\Common\Factory\Model $factory): Job
|
||||
{
|
||||
$this->factory = $factory;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function fieldsForUpdate(): array
|
||||
{
|
||||
return $this->fieldsForInsert();
|
||||
}
|
||||
protected function valuesForUpdate(ModelInterface $model): array
|
||||
{
|
||||
return $this->valuesForInsert($model);
|
||||
}
|
||||
protected function fieldsForInsert(): array
|
||||
{
|
||||
return [
|
||||
'message_id',
|
||||
'date_time',
|
||||
'executed'
|
||||
];
|
||||
}
|
||||
protected function valuesForInsert(ModelInterface $model): array
|
||||
{
|
||||
return [
|
||||
$model->getMessage()->getId(),
|
||||
$model->getDateTime()->format('Y-m-d H:i:s'),
|
||||
$model->isExecuted() ? 1 : 0
|
||||
];
|
||||
}
|
||||
protected function defaultFind(ModelInterface $model): ModelInterface
|
||||
{
|
||||
return $this->fetchByMessageAndDate($model->getMessage()->getId(), $model->getDateTime()->format('Y-m-d H:i:s'));
|
||||
}
|
||||
protected function fieldsForCreate(): array
|
||||
{
|
||||
return [
|
||||
'message_id',
|
||||
'date_time',
|
||||
'executed'
|
||||
];
|
||||
}
|
||||
protected function valuesForCreate(array $data): array
|
||||
{
|
||||
return [
|
||||
$data['message_id'],
|
||||
$data['date_time'],
|
||||
$data['executed'] ?? 0
|
||||
];
|
||||
}
|
||||
protected function defaultSearch(array $data): ModelInterface
|
||||
{
|
||||
return $this->fetchByMessageAndDate($data['message_id'], $data['date_time']);
|
||||
}
|
||||
|
||||
public function load(array $row): ModelInterface
|
||||
{
|
||||
$model = (new BaseModel())
|
||||
->setId($row['id'])
|
||||
->setMessage($this->getFactory()->find(\ProVM\Emails\Model\Message::class)->fetchById($row['message_id']))
|
||||
->setDateTime(new DateTimeImmutable($row['date_time']));
|
||||
if ($row['executed'] ?? 0 === 1) {
|
||||
$model->wasExecuted();
|
||||
}
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function fetchAllPending(): array
|
||||
{
|
||||
$query = "SELECT * FROM {$this->getTable()} WHERE `executed` = 0";
|
||||
return $this->fetchMany($query);
|
||||
}
|
||||
public function fetchByMessageAndDate(int $message_id, string $date_time): \ProVM\Emails\Model\Job
|
||||
{
|
||||
$query = "SELECT * FROM {$this->getTable()} WHERE `message_id` = ? AND `date_time` = ?";
|
||||
return $this->fetchOne($query, [$message_id, $date_time]);
|
||||
}
|
||||
public function fetchPendingByMessage(int $message_id): \ProVM\Emails\Model\Job
|
||||
{
|
||||
$query = "SELECT * FROM {$this->getTable()} WHERE `message_id` = ? AND `executed` = 0";
|
||||
return $this->fetchOne($query, [$message_id]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user