AtLeastOneTriggerRule.php
3 years ago
ConsistentStepMapRule.php
3 years ago
NoCycleRule.php
2 years ago
NoDuplicateEdgesRule.php
2 years ago
NoJoinRule.php
2 years ago
NoUnreachableStepsRule.php
2 years ago
TriggerNeedsToBeFollowedByActionRule.php
2 years ago
TriggersUnderRootRule.php
2 years ago
UnknownStepRule.php
3 years ago
ValidStepArgsRule.php
3 years ago
ValidStepFiltersRule.php
2 years ago
ValidStepOrderRule.php
3 years ago
ValidStepRule.php
2 months ago
ValidStepValidationRule.php
3 years ago
index.php
3 years ago
ValidStepValidationRule.php
75 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Validation\AutomationRules; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Engine\Data\Step; |
| 10 | use MailPoet\Automation\Engine\Data\StepValidationArgs; |
| 11 | use MailPoet\Automation\Engine\Exceptions; |
| 12 | use MailPoet\Automation\Engine\Integration\Payload; |
| 13 | use MailPoet\Automation\Engine\Integration\Subject; |
| 14 | use MailPoet\Automation\Engine\Registry; |
| 15 | use MailPoet\Automation\Engine\Validation\AutomationGraph\AutomationNode; |
| 16 | use MailPoet\Automation\Engine\Validation\AutomationGraph\AutomationNodeVisitor; |
| 17 | |
| 18 | class ValidStepValidationRule implements AutomationNodeVisitor { |
| 19 | /** @var Registry */ |
| 20 | private $registry; |
| 21 | |
| 22 | public function __construct( |
| 23 | Registry $registry |
| 24 | ) { |
| 25 | $this->registry = $registry; |
| 26 | } |
| 27 | |
| 28 | public function initialize(Automation $automation): void { |
| 29 | } |
| 30 | |
| 31 | public function visitNode(Automation $automation, AutomationNode $node): void { |
| 32 | $step = $node->getStep(); |
| 33 | $registryStep = $this->registry->getStep($step->getKey()); |
| 34 | if (!$registryStep) { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | $subjects = $this->collectSubjects($automation, $node->getParents()); |
| 39 | $args = new StepValidationArgs($automation, $step, $subjects); |
| 40 | $registryStep->validate($args); |
| 41 | } |
| 42 | |
| 43 | public function complete(Automation $automation): void { |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param Step[] $parents |
| 48 | * @return Subject<Payload>[] |
| 49 | */ |
| 50 | private function collectSubjects(Automation $automation, array $parents): array { |
| 51 | $triggers = array_filter($parents, function (Step $step) { |
| 52 | return $step->getType() === Step::TYPE_TRIGGER; |
| 53 | }); |
| 54 | |
| 55 | $subjectKeys = []; |
| 56 | foreach ($triggers as $trigger) { |
| 57 | $registryTrigger = $this->registry->getTrigger($trigger->getKey()); |
| 58 | if (!$registryTrigger) { |
| 59 | throw Exceptions::automationTriggerNotFound($automation->getId(), $trigger->getKey()); |
| 60 | } |
| 61 | $subjectKeys = array_merge($subjectKeys, $registryTrigger->getSubjectKeys()); |
| 62 | } |
| 63 | |
| 64 | $subjects = []; |
| 65 | foreach (array_unique($subjectKeys) as $key) { |
| 66 | $subject = $this->registry->getSubject($key); |
| 67 | if (!$subject) { |
| 68 | throw Exceptions::subjectNotFound($key); |
| 69 | } |
| 70 | $subjects[] = $subject; |
| 71 | } |
| 72 | return $subjects; |
| 73 | } |
| 74 | } |
| 75 |