Cleanup of cli

This commit is contained in:
2023-06-09 00:54:34 -04:00
parent 9307ba330c
commit 03c1dac2f2
36 changed files with 614 additions and 272 deletions

View File

@ -2,28 +2,28 @@
namespace ProVM\Emails\Repository;
use PDO;
use ProVM\Common\Factory\Model;
use ProVM\Common\Factory;
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;
use ProVM\Emails;
class Job extends Repository
{
public function __construct(PDO $connection, LoggerInterface $logger, Model $factory)
public function __construct(PDO $connection, LoggerInterface $logger, Factory\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
protected Factory\Model $factory;
public function getFactory(): Factory\Model
{
return $this->factory;
}
public function setFactory(\ProVM\Common\Factory\Model $factory): Job
public function setFactory(Factory\Model $factory): Job
{
$this->factory = $factory;
return $this;
@ -34,12 +34,9 @@ class Job extends Repository
$query = "
CREATE TABLE {$this->getTable()} (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`message_id` INT UNSIGNED NOT NULL,
`date_time` DATETIME NOT NULL,
`executed` INT(1) UNSIGNED DEFAULT 0,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_messages_{$this->getTable()}` (`message_id`)
REFERENCES `messages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
`command` VARCHAR(100) NOT NULL,
`arguments` TEXT NOT NULL,
PRIMARY KEY (`id`)
)";
$this->getConnection()->query($query);
}
@ -55,65 +52,61 @@ CREATE TABLE {$this->getTable()} (
protected function fieldsForInsert(): array
{
return [
'message_id',
'date_time',
'executed'
'command',
'arguments',
];
}
protected function valuesForInsert(ModelInterface $model): array
{
return [
$model->getMessage()->getId(),
$model->getDateTime()->format('Y-m-d H:i:s'),
$model->isExecuted() ? 1 : 0
$model->getCommand(),
$model->getArguments(),
];
}
protected function defaultFind(ModelInterface $model): ModelInterface
{
return $this->fetchByMessageAndDate($model->getMessage()->getId(), $model->getDateTime()->format('Y-m-d H:i:s'));
return $this->fetchByCommandAndArguments($model->getCommand(), $model->getArguments());
}
protected function fieldsForCreate(): array
{
return [
'message_id',
'date_time',
'executed'
'command',
'arguments',
];
}
protected function valuesForCreate(array $data): array
{
return [
$data['message_id'],
$data['date_time'],
$data['executed'] ?? 0
$data['command'],
$data['arguments'],
];
}
protected function defaultSearch(array $data): ModelInterface
{
return $this->fetchByMessageAndDate($data['message_id'], $data['date_time']);
return $this->fetchByCommandAndArguments($data['command'], $data['arguments']);
}
public function load(array $row): ModelInterface
{
$model = (new BaseModel())
return (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;
->setCommand($row['command'])
->setArguments($row['arguments'])
->setStateRepository($this->getFactory()->find(Emails\Model\State\Job::class));
}
public function fetchAllPending(): array
{
$query = "SELECT * FROM {$this->getTable()} WHERE `executed` = 0";
$query = "SELECT a.*
FROM {$this->getTable()} a
JOIN `jobs_states` b ON b.job_id = a.id
WHERE b.`status` = ?";
return $this->fetchMany($query);
}
public function fetchByMessageAndDate(int $message_id, string $date_time): \ProVM\Emails\Model\Job
public function fetchByCommandAndArguments(string $command, string $arguments): \ProVM\Emails\Model\Job
{
$query = "SELECT * FROM {$this->getTable()} WHERE `message_id` = ? AND `date_time` = ?";
return $this->fetchOne($query, [$message_id, $date_time]);
$query = "SELECT * FROM {$this->getTable()} WHERE `command` = ? AND `arguments` = ?";
return $this->fetchOne($query, [$command, $arguments]);
}
public function fetchPendingByMessage(int $message_id): \ProVM\Emails\Model\Job
{