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
Automation.php
267 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 | use MailPoet\Automation\Engine\Exceptions\InvalidStateException; |
| 10 | use MailPoet\Automation\Engine\Utils\Json; |
| 11 | |
| 12 | class Automation { |
| 13 | public const STATUS_ACTIVE = 'active'; |
| 14 | public const STATUS_DEACTIVATING = 'deactivating'; |
| 15 | public const STATUS_DRAFT = 'draft'; |
| 16 | public const STATUS_TRASH = 'trash'; |
| 17 | public const STATUS_ALL = [ |
| 18 | self::STATUS_ACTIVE, |
| 19 | self::STATUS_DEACTIVATING, |
| 20 | self::STATUS_DRAFT, |
| 21 | self::STATUS_TRASH, |
| 22 | ]; |
| 23 | |
| 24 | /** @var int|null */ |
| 25 | private $id; |
| 26 | |
| 27 | /** @var int|null */ |
| 28 | private $versionId; |
| 29 | |
| 30 | /** @var string */ |
| 31 | private $name; |
| 32 | |
| 33 | /** @var \WP_User */ |
| 34 | private $author; |
| 35 | |
| 36 | /** @var string */ |
| 37 | private $status = self::STATUS_DRAFT; |
| 38 | |
| 39 | /** @var DateTimeImmutable */ |
| 40 | private $createdAt; |
| 41 | |
| 42 | /** @var DateTimeImmutable */ |
| 43 | private $updatedAt; |
| 44 | |
| 45 | /** @var ?DateTimeImmutable */ |
| 46 | private $activatedAt = null; |
| 47 | |
| 48 | /** @var array<string|int, Step> */ |
| 49 | private $steps; |
| 50 | |
| 51 | /** @var array<string, mixed> */ |
| 52 | private $meta = []; |
| 53 | |
| 54 | public function __clone() { |
| 55 | $this->author = clone $this->author; |
| 56 | $this->steps = array_map(function(Step $step): Step { |
| 57 | return clone $step; |
| 58 | }, $this->steps); |
| 59 | } |
| 60 | |
| 61 | /** @param array<string, Step> $steps */ |
| 62 | public function __construct( |
| 63 | string $name, |
| 64 | array $steps, |
| 65 | \WP_User $author, |
| 66 | ?int $id = null, |
| 67 | ?int $versionId = null |
| 68 | ) { |
| 69 | $this->name = $name; |
| 70 | $this->steps = $steps; |
| 71 | $this->author = $author; |
| 72 | $this->id = $id; |
| 73 | $this->versionId = $versionId; |
| 74 | |
| 75 | $now = new DateTimeImmutable(); |
| 76 | $this->createdAt = $now; |
| 77 | $this->updatedAt = $now; |
| 78 | } |
| 79 | |
| 80 | public function getId(): int { |
| 81 | if ($this->id === null) { |
| 82 | throw InvalidStateException::create()->withMessage('No automation ID was set'); |
| 83 | } |
| 84 | return $this->id; |
| 85 | } |
| 86 | |
| 87 | public function setId(int $id): void { |
| 88 | $this->id = $id; |
| 89 | } |
| 90 | |
| 91 | public function getVersionId(): int { |
| 92 | if (!$this->versionId) { |
| 93 | throw InvalidStateException::create()->withMessage('No automation version ID was set'); |
| 94 | } |
| 95 | return $this->versionId; |
| 96 | } |
| 97 | |
| 98 | public function getName(): string { |
| 99 | return $this->name; |
| 100 | } |
| 101 | |
| 102 | public function setName(string $name): void { |
| 103 | $this->name = $name; |
| 104 | $this->setUpdatedAt(); |
| 105 | } |
| 106 | |
| 107 | public function getStatus(): string { |
| 108 | return $this->status; |
| 109 | } |
| 110 | |
| 111 | public function setStatus(string $status): void { |
| 112 | if ($status === self::STATUS_ACTIVE && $this->status !== self::STATUS_ACTIVE) { |
| 113 | $this->activatedAt = new DateTimeImmutable(); |
| 114 | } |
| 115 | $this->status = $status; |
| 116 | $this->setUpdatedAt(); |
| 117 | } |
| 118 | |
| 119 | public function getCreatedAt(): DateTimeImmutable { |
| 120 | return $this->createdAt; |
| 121 | } |
| 122 | |
| 123 | public function setCreatedAt(DateTimeImmutable $createdAt): void { |
| 124 | $this->createdAt = $createdAt; |
| 125 | } |
| 126 | |
| 127 | public function getAuthor(): \WP_User { |
| 128 | return $this->author; |
| 129 | } |
| 130 | |
| 131 | public function getUpdatedAt(): DateTimeImmutable { |
| 132 | return $this->updatedAt; |
| 133 | } |
| 134 | |
| 135 | public function getActivatedAt(): ?DateTimeImmutable { |
| 136 | return $this->activatedAt; |
| 137 | } |
| 138 | |
| 139 | /** @return array<string|int, Step> */ |
| 140 | public function getSteps(): array { |
| 141 | return $this->steps; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @return array<string|int, Step> |
| 146 | */ |
| 147 | public function getTriggers(): array { |
| 148 | return array_filter( |
| 149 | $this->steps, |
| 150 | function (Step $step) { |
| 151 | return $step->getType() === Step::TYPE_TRIGGER; |
| 152 | } |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | /** @param array<string|int, Step> $steps */ |
| 157 | public function setSteps(array $steps): void { |
| 158 | $this->steps = $steps; |
| 159 | $this->setUpdatedAt(); |
| 160 | } |
| 161 | |
| 162 | public function getStep(string $id): ?Step { |
| 163 | return $this->steps[$id] ?? null; |
| 164 | } |
| 165 | |
| 166 | public function getTrigger(string $key): ?Step { |
| 167 | foreach ($this->steps as $step) { |
| 168 | if ($step->getType() === Step::TYPE_TRIGGER && $step->getKey() === $key) { |
| 169 | return $step; |
| 170 | } |
| 171 | } |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | public function equals(Automation $compare): bool { |
| 176 | $compareArray = $compare->toArray(); |
| 177 | $currentArray = $this->toArray(); |
| 178 | $ignoreValues = [ |
| 179 | 'created_at', |
| 180 | 'updated_at', |
| 181 | ]; |
| 182 | foreach ($ignoreValues as $ignore) { |
| 183 | unset($compareArray[$ignore]); |
| 184 | unset($currentArray[$ignore]); |
| 185 | } |
| 186 | return $compareArray === $currentArray; |
| 187 | } |
| 188 | |
| 189 | public function needsFullValidation(): bool { |
| 190 | return in_array($this->status, [Automation::STATUS_ACTIVE, Automation::STATUS_DEACTIVATING], true); |
| 191 | } |
| 192 | |
| 193 | public function toArray(): array { |
| 194 | return [ |
| 195 | 'id' => $this->id, |
| 196 | 'name' => $this->name, |
| 197 | 'status' => $this->status, |
| 198 | 'author' => $this->author->ID, |
| 199 | 'created_at' => $this->createdAt->format(DateTimeImmutable::W3C), |
| 200 | 'updated_at' => $this->updatedAt->format(DateTimeImmutable::W3C), |
| 201 | 'activated_at' => $this->activatedAt ? $this->activatedAt->format(DateTimeImmutable::W3C) : null, |
| 202 | 'steps' => Json::encode( |
| 203 | array_map(function (Step $step) { |
| 204 | return $step->toArray(); |
| 205 | }, $this->steps) |
| 206 | ), |
| 207 | 'meta' => Json::encode($this->meta), |
| 208 | ]; |
| 209 | } |
| 210 | |
| 211 | private function setUpdatedAt(): void { |
| 212 | $this->updatedAt = new DateTimeImmutable(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @param string $key |
| 217 | * @return mixed|null |
| 218 | */ |
| 219 | public function getMeta(string $key) { |
| 220 | return $this->meta[$key] ?? null; |
| 221 | } |
| 222 | |
| 223 | public function getAllMetas(): array { |
| 224 | return $this->meta; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * @param string $key |
| 229 | * @param mixed $value |
| 230 | * @return void |
| 231 | */ |
| 232 | public function setMeta(string $key, $value): void { |
| 233 | $this->meta[$key] = $value; |
| 234 | $this->setUpdatedAt(); |
| 235 | } |
| 236 | |
| 237 | public function deleteMeta(string $key): void { |
| 238 | unset($this->meta[$key]); |
| 239 | $this->setUpdatedAt(); |
| 240 | } |
| 241 | |
| 242 | public function deleteAllMetas(): void { |
| 243 | $this->meta = []; |
| 244 | $this->setUpdatedAt(); |
| 245 | } |
| 246 | |
| 247 | public static function fromArray(array $data): self { |
| 248 | // TODO: validation |
| 249 | $automation = new self( |
| 250 | $data['name'], |
| 251 | array_map(function (array $stepData): Step { |
| 252 | return Step::fromArray($stepData); |
| 253 | }, Json::decode($data['steps'])), |
| 254 | new \WP_User((int)$data['author']) |
| 255 | ); |
| 256 | $automation->id = (int)$data['id']; |
| 257 | $automation->versionId = (int)$data['version_id']; |
| 258 | $automation->status = $data['status']; |
| 259 | $automation->createdAt = new DateTimeImmutable($data['created_at']); |
| 260 | $automation->updatedAt = new DateTimeImmutable($data['updated_at']); |
| 261 | $automation->activatedAt = $data['activated_at'] !== null ? new DateTimeImmutable($data['activated_at']) : null; |
| 262 | |
| 263 | $automation->meta = $data['meta'] ? Json::decode($data['meta']) : []; |
| 264 | return $automation; |
| 265 | } |
| 266 | } |
| 267 |