Cleanup of cli
This commit is contained in:
15
api/common/Exception/Job/Stateless.php
Normal file
15
api/common/Exception/Job/Stateless.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Exception\Job;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
class Stateless extends Exception
|
||||
{
|
||||
public function __construct(int $job_id, ?Throwable $previous = null)
|
||||
{
|
||||
$message = "Job {$job_id} does not have any state";
|
||||
$code = 2002;
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Exception;
|
||||
|
||||
use Ddeboer\Imap\MailboxInterface;
|
||||
use Exception;
|
||||
use Throwable;
|
||||
use Ddeboer\Imap\MailboxInterface;
|
||||
|
||||
class EmptyMailbox extends Exception
|
||||
{
|
||||
@ -12,4 +13,4 @@ class EmptyMailbox extends Exception
|
||||
$code = 101;
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
namespace ProVM\Common\Exception\Mailbox;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
class Invalid extends Exception
|
||||
{
|
||||
@ -11,4 +12,4 @@ class Invalid extends Exception
|
||||
$code = 100;
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
namespace ProVM\Common\Exception\Mailbox;
|
||||
|
||||
use Exception;
|
||||
use Throwable;
|
||||
use ProVM\Emails\Model\Mailbox;
|
||||
|
||||
class Stateless extends Exception
|
||||
@ -12,4 +13,4 @@ class Stateless extends Exception
|
||||
$code = 102;
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ $app->group('/mailboxes', function($app) {
|
||||
|
||||
$app->group('/mailbox/{mailbox_id}', function($app) {
|
||||
$app->group('/messages', function($app) {
|
||||
$app->get('/grab[/]', [Messages::class, 'grab']);
|
||||
$app->get('/valid[/]', [Messages::class, 'valid']);
|
||||
$app->get('[/]', Messages::class);
|
||||
});
|
||||
|
@ -3,9 +3,10 @@ use ProVM\Common\Controller\Attachments;
|
||||
|
||||
$app->group('/attachments', function($app) {
|
||||
$app->put('/grab', [Attachments::class, 'grab']);
|
||||
$app->get('/pending', [Attachments::class, 'pending']);
|
||||
$app->post('/decrypt', [Attachments::class, 'decrypt']);
|
||||
$app->get('[/]', Attachments::class);
|
||||
});
|
||||
$app->group('/attachment/{attachment_id}', function($app) {
|
||||
$app->get('[/]', [Attachments::class, 'get']);
|
||||
});
|
||||
});
|
||||
|
@ -6,6 +6,7 @@ return [
|
||||
return [
|
||||
$container->get(Monolog\Processor\PsrLogMessageProcessor::class),
|
||||
$container->get(Monolog\Processor\IntrospectionProcessor::class),
|
||||
$container->get(Monolog\Processor\WebProcessor::class),
|
||||
$container->get(Monolog\Processor\MemoryPeakUsageProcessor::class),
|
||||
];
|
||||
},
|
||||
@ -36,7 +37,7 @@ return [
|
||||
);
|
||||
},
|
||||
Psr\Log\LoggerInterface::class => function(ContainerInterface $container) {
|
||||
return $container->get('elk_logger');
|
||||
return $container->get('file_logger');
|
||||
},
|
||||
'file_logger' => function(ContainerInterface $container) {
|
||||
return new Monolog\Logger(
|
||||
|
@ -3,29 +3,31 @@ namespace ProVM\Emails\Model;
|
||||
|
||||
use DateTimeInterface;
|
||||
use ProVM\Common\Define\Model;
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use ProVM\Common\Exception\Job\Stateless;
|
||||
use ProVM\Emails;
|
||||
|
||||
class Job implements Model
|
||||
{
|
||||
protected int $id;
|
||||
protected Message $message;
|
||||
protected DateTimeInterface $dateTime;
|
||||
protected bool $executed;
|
||||
protected string $command;
|
||||
protected string $arguments;
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
public function getMessage(): Message
|
||||
public function getCommand(): string
|
||||
{
|
||||
return $this->message;
|
||||
return $this->command;
|
||||
}
|
||||
public function getDateTime(): DateTimeInterface
|
||||
public function getArguments(): string
|
||||
{
|
||||
return $this->dateTime;
|
||||
return $this->arguments ?? '';
|
||||
}
|
||||
public function isExecuted(): bool
|
||||
{
|
||||
return $this->executed ?? false;
|
||||
return $this->lastState()->getStatus() === State\Job::Executed;
|
||||
}
|
||||
|
||||
public function setId(int $id): Job
|
||||
@ -33,22 +35,61 @@ class Job implements Model
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
public function setMessage(Message $message): Job
|
||||
public function setCommand(string $message): Job
|
||||
{
|
||||
$this->message = $message;
|
||||
return $this;
|
||||
}
|
||||
public function setDateTime(DateTimeInterface $dateTime): Job
|
||||
public function setArguments(string $dateTime): Job
|
||||
{
|
||||
$this->dateTime = $dateTime;
|
||||
return $this;
|
||||
}
|
||||
public function wasExecuted(): Job
|
||||
|
||||
protected Emails\Repository\State\Job $stateRepository;
|
||||
public function getStateRepository(): Emails\Repository\State\Job
|
||||
{
|
||||
$this->executed = true;
|
||||
return $this->stateRepository;
|
||||
}
|
||||
public function setStateRepository(Emails\Repository\State\Job $repository): Job
|
||||
{
|
||||
$this->stateRepository = $repository;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected array $states;
|
||||
public function getStates(): array
|
||||
{
|
||||
if (!isset($this->states)) {
|
||||
try {
|
||||
$this->setStates($this->getStateRepository()->fetchByJob($this->getId()));
|
||||
} catch (BlankResult $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
return $this->states;
|
||||
}
|
||||
public function addState(State\Job $state): Job
|
||||
{
|
||||
$this->states []= $state;
|
||||
return $this;
|
||||
}
|
||||
public function setStates(array $states): Job
|
||||
{
|
||||
foreach ($states as $state) {
|
||||
$this->addState($state);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function lastState(): State\Job
|
||||
{
|
||||
if (count($this->getStates()) === 0) {
|
||||
throw new Stateless($this);
|
||||
}
|
||||
return $this->getStates()[array_key_last($this->getStates())];
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
|
65
api/src/Model/State/Job.php
Normal file
65
api/src/Model/State/Job.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace ProVM\Emails\Model\State;
|
||||
|
||||
use ProVM\Common\Define\Model;
|
||||
use ProVM\Emails;
|
||||
|
||||
class Job implements Model
|
||||
{
|
||||
const Executed = 0;
|
||||
const Pending = 1;
|
||||
const Failure = -1;
|
||||
|
||||
protected int $id;
|
||||
protected Emails\Model\Job $job;
|
||||
protected \DateTimeInterface $dateTime;
|
||||
protected int $status;
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
public function getJob(): Emails\Model\Job
|
||||
{
|
||||
return $this->job;
|
||||
}
|
||||
public function getDateTime(): \DateTimeInterface
|
||||
{
|
||||
return $this->dateTime;
|
||||
}
|
||||
public function getStatus(): int
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
public function setId(int $id): Job
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
public function setJob(Emails\Model\Job $job): Job
|
||||
{
|
||||
$this->job = $job;
|
||||
return $this;
|
||||
}
|
||||
public function setDateTime(\DateTimeInterface $dateTime): Job
|
||||
{
|
||||
$this->dateTime = $dateTime;
|
||||
return $this;
|
||||
}
|
||||
public function setStatus(int $status): Job
|
||||
{
|
||||
$this->status = $status;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed
|
||||
{
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'job' => $this->getJob(),
|
||||
'date' => $this->getDateTime(),
|
||||
'status' => $this->getStatus()
|
||||
];
|
||||
}
|
||||
}
|
@ -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