Automation.php
2 months ago
AutomationRun.php
1 year ago
AutomationRunLog.php
1 year ago
AutomationStatistics.php
9 months ago
AutomationTemplate.php
6 months ago
AutomationTemplateCategory.php
2 years ago
Field.php
2 years ago
Filter.php
3 years ago
FilterGroup.php
2 months ago
Filters.php
2 months ago
NextStep.php
2 years ago
Step.php
2 months ago
StepRunArgs.php
2 years ago
StepValidationArgs.php
3 years ago
Subject.php
3 years ago
SubjectEntry.php
3 years ago
index.php
3 years ago
AutomationRun.php
138 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Data; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use DateTimeImmutable; |
| 9 | |
| 10 | class AutomationRun { |
| 11 | public const STATUS_RUNNING = 'running'; |
| 12 | public const STATUS_COMPLETE = 'complete'; |
| 13 | public const STATUS_CANCELLED = 'cancelled'; |
| 14 | public const STATUS_FAILED = 'failed'; |
| 15 | |
| 16 | /** @var int */ |
| 17 | private $id; |
| 18 | |
| 19 | /** @var int */ |
| 20 | private $automationId; |
| 21 | |
| 22 | /** @var int */ |
| 23 | private $versionId; |
| 24 | |
| 25 | /** @var string */ |
| 26 | private $triggerKey; |
| 27 | |
| 28 | /** @var string */ |
| 29 | private $status = self::STATUS_RUNNING; |
| 30 | |
| 31 | /** @var DateTimeImmutable */ |
| 32 | private $createdAt; |
| 33 | |
| 34 | /** @var DateTimeImmutable */ |
| 35 | private $updatedAt; |
| 36 | |
| 37 | /** @var Subject[] */ |
| 38 | private $subjects; |
| 39 | |
| 40 | /** |
| 41 | * @param Subject[] $subjects |
| 42 | */ |
| 43 | public function __construct( |
| 44 | int $automationId, |
| 45 | int $versionId, |
| 46 | string $triggerKey, |
| 47 | array $subjects, |
| 48 | ?int $id = null |
| 49 | ) { |
| 50 | $this->automationId = $automationId; |
| 51 | $this->versionId = $versionId; |
| 52 | $this->triggerKey = $triggerKey; |
| 53 | $this->subjects = $subjects; |
| 54 | |
| 55 | if ($id) { |
| 56 | $this->id = $id; |
| 57 | } |
| 58 | |
| 59 | $now = new DateTimeImmutable(); |
| 60 | $this->createdAt = $now; |
| 61 | $this->updatedAt = $now; |
| 62 | } |
| 63 | |
| 64 | public function getId(): int { |
| 65 | return $this->id; |
| 66 | } |
| 67 | |
| 68 | public function setId(int $id): void { |
| 69 | $this->id = $id; |
| 70 | } |
| 71 | |
| 72 | public function getAutomationId(): int { |
| 73 | return $this->automationId; |
| 74 | } |
| 75 | |
| 76 | public function getVersionId(): int { |
| 77 | return $this->versionId; |
| 78 | } |
| 79 | |
| 80 | public function getTriggerKey(): string { |
| 81 | return $this->triggerKey; |
| 82 | } |
| 83 | |
| 84 | public function getStatus(): string { |
| 85 | return $this->status; |
| 86 | } |
| 87 | |
| 88 | public function getCreatedAt(): DateTimeImmutable { |
| 89 | return $this->createdAt; |
| 90 | } |
| 91 | |
| 92 | public function getUpdatedAt(): DateTimeImmutable { |
| 93 | return $this->updatedAt; |
| 94 | } |
| 95 | |
| 96 | /** @return Subject[] */ |
| 97 | public function getSubjects(?string $key = null): array { |
| 98 | if ($key) { |
| 99 | return array_values( |
| 100 | array_filter($this->subjects, function (Subject $subject) use ($key) { |
| 101 | return $subject->getKey() === $key; |
| 102 | }) |
| 103 | ); |
| 104 | } |
| 105 | return $this->subjects; |
| 106 | } |
| 107 | |
| 108 | public function toArray(): array { |
| 109 | return [ |
| 110 | 'automation_id' => $this->automationId, |
| 111 | 'version_id' => $this->versionId, |
| 112 | 'trigger_key' => $this->triggerKey, |
| 113 | 'status' => $this->status, |
| 114 | 'created_at' => $this->createdAt->format(DateTimeImmutable::W3C), |
| 115 | 'updated_at' => $this->updatedAt->format(DateTimeImmutable::W3C), |
| 116 | 'subjects' => array_map(function (Subject $subject): array { |
| 117 | return $subject->toArray(); |
| 118 | }, $this->subjects), |
| 119 | ]; |
| 120 | } |
| 121 | |
| 122 | public static function fromArray(array $data): self { |
| 123 | $automationRun = new AutomationRun( |
| 124 | (int)$data['automation_id'], |
| 125 | (int)$data['version_id'], |
| 126 | $data['trigger_key'], |
| 127 | array_map(function (array $subject) { |
| 128 | return Subject::fromArray($subject); |
| 129 | }, $data['subjects']) |
| 130 | ); |
| 131 | $automationRun->id = (int)$data['id']; |
| 132 | $automationRun->status = $data['status']; |
| 133 | $automationRun->createdAt = new DateTimeImmutable($data['created_at']); |
| 134 | $automationRun->updatedAt = new DateTimeImmutable($data['updated_at']); |
| 135 | return $automationRun; |
| 136 | } |
| 137 | } |
| 138 |