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
StepValidationArgs.php
80 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\Integration\Payload; |
| 10 | use MailPoet\Automation\Engine\Integration\Subject; |
| 11 | |
| 12 | class StepValidationArgs { |
| 13 | /** @var Automation */ |
| 14 | private $automation; |
| 15 | |
| 16 | /** @var Step */ |
| 17 | private $step; |
| 18 | |
| 19 | /** @var array<string, Subject<Payload>> */ |
| 20 | private $subjects = []; |
| 21 | |
| 22 | /** @var array<class-string, string> */ |
| 23 | private $subjectKeyClassMap = []; |
| 24 | |
| 25 | /** @param Subject<Payload>[] $subjects */ |
| 26 | public function __construct( |
| 27 | Automation $automation, |
| 28 | Step $step, |
| 29 | array $subjects |
| 30 | ) { |
| 31 | $this->automation = $automation; |
| 32 | $this->step = $step; |
| 33 | |
| 34 | foreach ($subjects as $subject) { |
| 35 | $key = $subject->getKey(); |
| 36 | $this->subjects[$key] = $subject; |
| 37 | $this->subjectKeyClassMap[get_class($subject)] = $key; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public function getAutomation(): Automation { |
| 42 | return $this->automation; |
| 43 | } |
| 44 | |
| 45 | public function getStep(): Step { |
| 46 | return $this->step; |
| 47 | } |
| 48 | |
| 49 | /** @return Subject<Payload>[] */ |
| 50 | public function getSubjects(): array { |
| 51 | return array_values($this->subjects); |
| 52 | } |
| 53 | |
| 54 | /** @return Subject<Payload> */ |
| 55 | public function getSingleSubject(string $key): Subject { |
| 56 | $subject = $this->subjects[$key] ?? null; |
| 57 | if (!$subject) { |
| 58 | throw Exceptions::subjectNotFound($key); |
| 59 | } |
| 60 | return $subject; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @template P of Payload |
| 65 | * @template S of Subject<P> |
| 66 | * @param class-string<S> $class |
| 67 | * @return S<P> |
| 68 | */ |
| 69 | public function getSingleSubjectByClass(string $class): Subject { |
| 70 | $key = $this->subjectKeyClassMap[$class] ?? null; |
| 71 | if (!$key) { |
| 72 | throw Exceptions::subjectClassNotFound($class); |
| 73 | } |
| 74 | |
| 75 | /** @var S<P> $subject -- for PHPStan */ |
| 76 | $subject = $this->getSingleSubject($key); |
| 77 | return $subject; |
| 78 | } |
| 79 | } |
| 80 |