Files
emails/api/src/Repository/Job.php

120 lines
3.6 KiB
PHP
Raw Normal View History

2022-11-28 22:56:21 -03:00
<?php
namespace ProVM\Emails\Repository;
use PDO;
2023-06-09 00:54:34 -04:00
use ProVM\Common\Factory;
2022-11-28 22:56:21 -03:00
use Psr\Log\LoggerInterface;
use ProVM\Common\Define\Model as ModelInterface;
use ProVM\Common\Implement\Repository;
use ProVM\Emails\Model\Job as BaseModel;
2023-06-09 00:54:34 -04:00
use ProVM\Emails;
2022-11-28 22:56:21 -03:00
class Job extends Repository
{
2023-06-09 00:54:34 -04:00
public function __construct(PDO $connection, LoggerInterface $logger, Factory\Model $factory)
2022-11-28 22:56:21 -03:00
{
parent::__construct($connection, $logger);
$this->setFactory($factory)
2023-06-12 21:14:07 -04:00
->setTable('jobs');
2022-11-28 22:56:21 -03:00
}
2023-06-09 00:54:34 -04:00
protected Factory\Model $factory;
public function getFactory(): Factory\Model
2022-11-28 22:56:21 -03:00
{
return $this->factory;
}
2023-06-09 00:54:34 -04:00
public function setFactory(Factory\Model $factory): Job
2022-11-28 22:56:21 -03:00
{
$this->factory = $factory;
return $this;
}
2023-06-08 20:49:27 -04:00
public function install(): void
{
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
2023-06-09 00:54:34 -04:00
`command` VARCHAR(100) NOT NULL,
`arguments` TEXT NOT NULL,
PRIMARY KEY (`id`)
2023-06-08 20:49:27 -04:00
)";
$this->getConnection()->query($query);
}
2022-11-28 22:56:21 -03:00
protected function fieldsForUpdate(): array
{
return $this->fieldsForInsert();
}
protected function valuesForUpdate(ModelInterface $model): array
{
return $this->valuesForInsert($model);
}
protected function fieldsForInsert(): array
{
return [
2023-06-09 00:54:34 -04:00
'command',
'arguments',
2022-11-28 22:56:21 -03:00
];
}
protected function valuesForInsert(ModelInterface $model): array
{
return [
2023-06-09 00:54:34 -04:00
$model->getCommand(),
$model->getArguments(),
2022-11-28 22:56:21 -03:00
];
}
protected function defaultFind(ModelInterface $model): ModelInterface
{
2023-06-09 00:54:34 -04:00
return $this->fetchByCommandAndArguments($model->getCommand(), $model->getArguments());
2022-11-28 22:56:21 -03:00
}
protected function fieldsForCreate(): array
{
return [
2023-06-09 00:54:34 -04:00
'command',
'arguments',
2022-11-28 22:56:21 -03:00
];
}
protected function valuesForCreate(array $data): array
{
return [
2023-06-09 00:54:34 -04:00
$data['command'],
$data['arguments'],
2022-11-28 22:56:21 -03:00
];
}
protected function defaultSearch(array $data): ModelInterface
{
2023-06-09 00:54:34 -04:00
return $this->fetchByCommandAndArguments($data['command'], $data['arguments']);
2022-11-28 22:56:21 -03:00
}
public function load(array $row): ModelInterface
{
2023-06-09 00:54:34 -04:00
return (new BaseModel())
2022-11-28 22:56:21 -03:00
->setId($row['id'])
2023-06-09 00:54:34 -04:00
->setCommand($row['command'])
->setArguments($row['arguments'])
->setStateRepository($this->getFactory()->find(Emails\Model\State\Job::class));
2022-11-28 22:56:21 -03:00
}
public function fetchAllPending(): array
{
2023-06-09 00:54:34 -04:00
$query = "SELECT a.*
2023-06-12 21:14:07 -04:00
FROM `{$this->getTable()}` a
JOIN (SELECT s1.* FROM `jobs_states` s1 JOIN (SELECT MAX(id) AS id, job_id FROM `jobs_states` GROUP BY job_id) s2 ON s2.id = s1.id) b ON b.`job_id` = a.`id`
2023-06-09 00:54:34 -04:00
WHERE b.`status` = ?";
2023-06-12 21:14:07 -04:00
return $this->fetchMany($query, [Emails\Model\State\Job::Pending]);
2022-11-28 22:56:21 -03:00
}
2023-06-12 21:14:07 -04:00
public function fetchByCommandAndArguments(string $command, string $arguments): Emails\Model\Job
2022-11-28 22:56:21 -03:00
{
2023-06-09 00:54:34 -04:00
$query = "SELECT * FROM {$this->getTable()} WHERE `command` = ? AND `arguments` = ?";
return $this->fetchOne($query, [$command, $arguments]);
2022-11-28 22:56:21 -03:00
}
2023-06-12 21:14:07 -04:00
public function fetchAllPendingByCommand(string $command): array
2022-11-28 22:56:21 -03:00
{
2023-06-12 21:14:07 -04:00
$query = "SELECT a.*
FROM `{$this->getTable()}` a
JOIN (SELECT s1.* FROM `jobs_states` s1 JOIN (SELECT MAX(id) AS id, job_id FROM `jobs_states` GROUP BY job_id) s2 ON s2.id = s1.id) b ON b.`job_id` = a.`id`
WHERE a.`command` = ? AND b.`status` = ?";
return $this->fetchMany($query, [$command, Emails\Model\State\Job::Pending]);
2022-11-28 22:56:21 -03:00
}
2023-06-08 20:49:27 -04:00
}