2022-11-28 22:56:21 -03:00
|
|
|
<?php
|
|
|
|
namespace ProVM\Emails\Model;
|
|
|
|
|
|
|
|
use DateTimeInterface;
|
|
|
|
use ProVM\Common\Define\Model;
|
2023-06-09 00:54:34 -04:00
|
|
|
use ProVM\Common\Exception\Database\BlankResult;
|
|
|
|
use ProVM\Common\Exception\Job\Stateless;
|
|
|
|
use ProVM\Emails;
|
2022-11-28 22:56:21 -03:00
|
|
|
|
|
|
|
class Job implements Model
|
|
|
|
{
|
|
|
|
protected int $id;
|
2023-06-09 00:54:34 -04:00
|
|
|
protected string $command;
|
|
|
|
protected string $arguments;
|
2022-11-28 22:56:21 -03:00
|
|
|
|
|
|
|
public function getId(): int
|
|
|
|
{
|
|
|
|
return $this->id;
|
|
|
|
}
|
2023-06-09 00:54:34 -04:00
|
|
|
public function getCommand(): string
|
2022-11-28 22:56:21 -03:00
|
|
|
{
|
2023-06-09 00:54:34 -04:00
|
|
|
return $this->command;
|
2022-11-28 22:56:21 -03:00
|
|
|
}
|
2023-06-09 00:54:34 -04:00
|
|
|
public function getArguments(): string
|
2022-11-28 22:56:21 -03:00
|
|
|
{
|
2023-06-09 00:54:34 -04:00
|
|
|
return $this->arguments ?? '';
|
2022-11-28 22:56:21 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setId(int $id): Job
|
|
|
|
{
|
|
|
|
$this->id = $id;
|
|
|
|
return $this;
|
|
|
|
}
|
2023-06-12 21:14:07 -04:00
|
|
|
public function setCommand(string $command): Job
|
2022-11-28 22:56:21 -03:00
|
|
|
{
|
2023-06-12 21:14:07 -04:00
|
|
|
$this->command = $command;
|
2022-11-28 22:56:21 -03:00
|
|
|
return $this;
|
|
|
|
}
|
2023-06-12 21:14:07 -04:00
|
|
|
public function setArguments(string $arguments): Job
|
2022-11-28 22:56:21 -03:00
|
|
|
{
|
2023-06-12 21:14:07 -04:00
|
|
|
$this->arguments = $arguments;
|
2022-11-28 22:56:21 -03:00
|
|
|
return $this;
|
|
|
|
}
|
2023-06-09 00:54:34 -04:00
|
|
|
|
|
|
|
protected Emails\Repository\State\Job $stateRepository;
|
|
|
|
public function getStateRepository(): Emails\Repository\State\Job
|
|
|
|
{
|
|
|
|
return $this->stateRepository;
|
|
|
|
}
|
|
|
|
public function setStateRepository(Emails\Repository\State\Job $repository): Job
|
2022-11-28 22:56:21 -03:00
|
|
|
{
|
2023-06-09 00:54:34 -04:00
|
|
|
$this->stateRepository = $repository;
|
2022-11-28 22:56:21 -03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-06-09 00:54:34 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-12 21:14:07 -04:00
|
|
|
public function isExecuted(): bool
|
|
|
|
{
|
|
|
|
return $this->lastState()->getStatus() === State\Job::Executed;
|
|
|
|
}
|
2023-06-09 00:54:34 -04:00
|
|
|
public function lastState(): State\Job
|
|
|
|
{
|
|
|
|
if (count($this->getStates()) === 0) {
|
2023-06-12 21:14:07 -04:00
|
|
|
throw new Stateless($this->getId());
|
2023-06-09 00:54:34 -04:00
|
|
|
}
|
|
|
|
return $this->getStates()[array_key_last($this->getStates())];
|
|
|
|
}
|
|
|
|
|
2022-11-28 22:56:21 -03:00
|
|
|
public function toArray(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => $this->getId(),
|
2023-06-12 21:14:07 -04:00
|
|
|
'command' => $this->getCommand(),
|
|
|
|
'arguments' => $this->getArguments(),
|
|
|
|
'states' => $this->getStates()
|
2022-11-28 22:56:21 -03:00
|
|
|
];
|
|
|
|
}
|
2023-06-08 20:49:27 -04:00
|
|
|
public function jsonSerialize(): mixed
|
|
|
|
{
|
|
|
|
return $this->toArray();
|
|
|
|
}
|
|
|
|
}
|