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
StepRunArgs.php
167 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Data; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Exceptions; |
| 9 | use MailPoet\Automation\Engine\Exceptions\InvalidStateException; |
| 10 | use MailPoet\Automation\Engine\Integration\Payload; |
| 11 | use MailPoet\Automation\Engine\Integration\Subject; |
| 12 | use Throwable; |
| 13 | |
| 14 | class StepRunArgs { |
| 15 | /** @var Automation */ |
| 16 | private $automation; |
| 17 | |
| 18 | /** @var AutomationRun */ |
| 19 | private $automationRun; |
| 20 | |
| 21 | /** @var Step */ |
| 22 | private $step; |
| 23 | |
| 24 | /** @var array<string, SubjectEntry<Subject<Payload>>[]> */ |
| 25 | private $subjectEntries = []; |
| 26 | |
| 27 | /** @var array<class-string, string> */ |
| 28 | private $subjectKeyClassMap = []; |
| 29 | |
| 30 | /** @var array<string, Field> */ |
| 31 | private $fields = []; |
| 32 | |
| 33 | /** @var array<string, string> */ |
| 34 | private $fieldToSubjectMap = []; |
| 35 | |
| 36 | /** @var int */ |
| 37 | private $runNumber; |
| 38 | |
| 39 | /** @param SubjectEntry<Subject<Payload>>[] $subjectsEntries */ |
| 40 | public function __construct( |
| 41 | Automation $automation, |
| 42 | AutomationRun $automationRun, |
| 43 | Step $step, |
| 44 | array $subjectsEntries, |
| 45 | int $runNumber |
| 46 | ) { |
| 47 | $this->automation = $automation; |
| 48 | $this->step = $step; |
| 49 | $this->automationRun = $automationRun; |
| 50 | $this->runNumber = $runNumber; |
| 51 | |
| 52 | foreach ($subjectsEntries as $entry) { |
| 53 | $subject = $entry->getSubject(); |
| 54 | $key = $subject->getKey(); |
| 55 | $this->subjectEntries[$key] = array_merge($this->subjectEntries[$key] ?? [], [$entry]); |
| 56 | $this->subjectKeyClassMap[get_class($subject)] = $key; |
| 57 | |
| 58 | foreach ($subject->getFields() as $field) { |
| 59 | $this->fields[$field->getKey()] = $field; |
| 60 | $this->fieldToSubjectMap[$field->getKey()] = $key; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public function getAutomation(): Automation { |
| 66 | return $this->automation; |
| 67 | } |
| 68 | |
| 69 | public function getAutomationRun(): AutomationRun { |
| 70 | return $this->automationRun; |
| 71 | } |
| 72 | |
| 73 | public function getStep(): Step { |
| 74 | return $this->step; |
| 75 | } |
| 76 | |
| 77 | /** @return array<string, SubjectEntry<Subject<Payload>>[]> */ |
| 78 | public function getSubjectEntries(): array { |
| 79 | return $this->subjectEntries; |
| 80 | } |
| 81 | |
| 82 | /** @return SubjectEntry<Subject<Payload>> */ |
| 83 | public function getSingleSubjectEntry(string $key): SubjectEntry { |
| 84 | $subjects = $this->subjectEntries[$key] ?? []; |
| 85 | if (count($subjects) === 0) { |
| 86 | throw Exceptions::subjectDataNotFound($key, $this->automationRun->getId()); |
| 87 | } |
| 88 | if (count($subjects) > 1) { |
| 89 | throw Exceptions::multipleSubjectsFound($key, $this->automationRun->getId()); |
| 90 | } |
| 91 | return $subjects[0]; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @template P of Payload |
| 96 | * @template S of Subject<P> |
| 97 | * @param class-string<S> $class |
| 98 | * @return SubjectEntry<S<P>> |
| 99 | */ |
| 100 | public function getSingleSubjectEntryByClass(string $class): SubjectEntry { |
| 101 | $key = $this->subjectKeyClassMap[$class] ?? null; |
| 102 | if (!$key) { |
| 103 | throw Exceptions::subjectClassNotFound($class); |
| 104 | } |
| 105 | |
| 106 | /** @var SubjectEntry<S<P>> $entry -- for PHPStan */ |
| 107 | $entry = $this->getSingleSubjectEntry($key); |
| 108 | return $entry; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @template P of Payload |
| 113 | * @param class-string<P> $class |
| 114 | * @return P |
| 115 | */ |
| 116 | public function getSinglePayloadByClass(string $class): Payload { |
| 117 | $payloads = []; |
| 118 | foreach ($this->subjectEntries as $entries) { |
| 119 | foreach ($entries as $entry) { |
| 120 | $payload = $entry->getPayload(); |
| 121 | if (get_class($payload) === $class) { |
| 122 | $payloads[] = $payload; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (count($payloads) === 0) { |
| 128 | throw Exceptions::payloadNotFound($class, $this->automationRun->getId()); |
| 129 | } |
| 130 | if (count($payloads) > 1) { |
| 131 | throw Exceptions::multiplePayloadsFound($class, $this->automationRun->getId()); |
| 132 | } |
| 133 | |
| 134 | // ensure PHPStan we're indeed returning an instance of $class |
| 135 | $payload = $payloads[0]; |
| 136 | if (!$payload instanceof $class) { |
| 137 | throw InvalidStateException::create(); |
| 138 | } |
| 139 | return $payload; |
| 140 | } |
| 141 | |
| 142 | /** @return mixed */ |
| 143 | public function getFieldValue(string $key, array $params = []) { |
| 144 | $field = $this->fields[$key] ?? null; |
| 145 | $subjectKey = $this->fieldToSubjectMap[$key] ?? null; |
| 146 | if (!$field || !$subjectKey) { |
| 147 | throw Exceptions::fieldNotFound($key); |
| 148 | } |
| 149 | |
| 150 | $entry = $this->getSingleSubjectEntry($subjectKey); |
| 151 | try { |
| 152 | $value = $field->getValue($entry->getPayload(), $params); |
| 153 | } catch (Throwable $e) { |
| 154 | throw Exceptions::fieldLoadFailed($field->getKey(), $field->getArgs()); |
| 155 | } |
| 156 | return $value; |
| 157 | } |
| 158 | |
| 159 | public function getRunNumber(): int { |
| 160 | return $this->runNumber; |
| 161 | } |
| 162 | |
| 163 | public function isFirstRun(): bool { |
| 164 | return $this->runNumber === 1; |
| 165 | } |
| 166 | } |
| 167 |