Cleanup of cli
This commit is contained in:
@ -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
|
||||
{
|
||||
|
113
api/src/Repository/State/Job.php
Normal file
113
api/src/Repository/State/Job.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
namespace ProVM\Emails\Repository\State;
|
||||
|
||||
use PDO;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ProVM\Common\Define;
|
||||
use ProVM\Common\Factory;
|
||||
use ProVM\Common\Implement\Repository;
|
||||
use ProVM\Emails;
|
||||
|
||||
class Job extends Repository
|
||||
{
|
||||
public function __construct(PDO $connection, LoggerInterface $logger, Factory\Model $factory)
|
||||
{
|
||||
parent::__construct($connection, $logger);
|
||||
$this->setTable('jobs_states')
|
||||
->setFactory($factory);
|
||||
}
|
||||
|
||||
protected Factory\Model $factory;
|
||||
public function getFactory(): Factory\Model
|
||||
{
|
||||
return $this->factory;
|
||||
}
|
||||
public function setFactory(Factory\Model $factory): Job
|
||||
{
|
||||
$this->factory = $factory;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function install(): void
|
||||
{
|
||||
$query = "
|
||||
CREATE TABLE {$this->getTable()} (
|
||||
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
`job_id` INT UNSIGNED NOT NULL,
|
||||
`date_time` DATETIME NOT NULL,
|
||||
`status` INT NOT NULL DEFAULT 1,
|
||||
PRIMARY KEY (`id`),
|
||||
FOREIGN KEY `fk_jobs_{$this->getTable()}` (`job_id`)
|
||||
REFERENCES `jobs` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
)";
|
||||
$this->getConnection()->query($query);
|
||||
}
|
||||
|
||||
public function load(array $row): Define\Model
|
||||
{
|
||||
return (new Emails\Model\State\Job())
|
||||
->setId($row['id'])
|
||||
->setJob($this->getFactory()->find(Emails\Model\Job::class)->fetchById($row['job_id']))
|
||||
->setDateTime(new \DateTimeImmutable($row['date_time']))
|
||||
->setStatus($row['status']);
|
||||
}
|
||||
|
||||
public function fetchByJob(int $job_id): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `job_id` = ?";
|
||||
return $this->fetchMany($query, [$job_id]);
|
||||
}
|
||||
public function fetchByJobAndStatus(int $job_id, int $status): Emails\Model\Job
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `job_id` = ? AND `status` = ?";
|
||||
return $this->fetchOne($query, [$job_id, $status]);
|
||||
}
|
||||
|
||||
protected function fieldsForInsert(): array
|
||||
{
|
||||
return [
|
||||
'job_id',
|
||||
'date_time',
|
||||
'status'
|
||||
];
|
||||
}
|
||||
protected function valuesForInsert(Define\Model $model): array
|
||||
{
|
||||
return [
|
||||
$model->getJob()->getId(),
|
||||
$model->getDateTime()->format('Y-m-d H:i:s'),
|
||||
$model->getStatus()
|
||||
];
|
||||
}
|
||||
|
||||
protected function fieldsForUpdate(): array
|
||||
{
|
||||
return $this->fieldsForInsert();
|
||||
}
|
||||
protected function fieldsForCreate(): array
|
||||
{
|
||||
return $this->fieldsForInsert();
|
||||
}
|
||||
|
||||
protected function valuesForUpdate(Define\Model $model): array
|
||||
{
|
||||
return $this->valuesForInsert($model);
|
||||
}
|
||||
protected function valuesForCreate(array $data): array
|
||||
{
|
||||
return [
|
||||
$data['job_id'],
|
||||
$data['date_time'],
|
||||
$data['status']
|
||||
];
|
||||
}
|
||||
|
||||
protected function defaultFind(Define\Model $model): Define\Model
|
||||
{
|
||||
return $this->fetchByJobAndStatus($model->getJob()->getId(), $model->getStatus());
|
||||
}
|
||||
protected function defaultSearch(array $data): Define\Model
|
||||
{
|
||||
return $this->fetchByJobAndStatus($data['job_id'], $data['status']);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user