ActionScheduler.php
3 weeks ago
AutomationController.php
2 years ago
FilterHandler.php
1 month ago
RootStep.php
2 years ago
StepHandler.php
2 months ago
StepRunController.php
1 year ago
StepRunControllerFactory.php
1 year ago
StepRunLogger.php
2 months ago
StepRunLoggerFactory.php
2 years ago
StepScheduler.php
1 year ago
SubjectLoader.php
3 years ago
SubjectTransformerHandler.php
1 year ago
TriggerHandler.php
2 years ago
index.php
3 years ago
TriggerHandler.php
120 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Control; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\AutomationRun; |
| 9 | use MailPoet\Automation\Engine\Data\AutomationRunLog; |
| 10 | use MailPoet\Automation\Engine\Data\StepRunArgs; |
| 11 | use MailPoet\Automation\Engine\Data\Subject; |
| 12 | use MailPoet\Automation\Engine\Exceptions; |
| 13 | use MailPoet\Automation\Engine\Hooks; |
| 14 | use MailPoet\Automation\Engine\Integration\Trigger; |
| 15 | use MailPoet\Automation\Engine\Storage\AutomationRunStorage; |
| 16 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 17 | use MailPoet\Automation\Engine\WordPress; |
| 18 | |
| 19 | class TriggerHandler { |
| 20 | /** @var AutomationStorage */ |
| 21 | private $automationStorage; |
| 22 | |
| 23 | /** @var AutomationRunStorage */ |
| 24 | private $automationRunStorage; |
| 25 | |
| 26 | /** @var SubjectLoader */ |
| 27 | private $subjectLoader; |
| 28 | |
| 29 | /** @var SubjectTransformerHandler */ |
| 30 | private $subjectTransformerHandler; |
| 31 | |
| 32 | /** @var FilterHandler */ |
| 33 | private $filterHandler; |
| 34 | |
| 35 | /** @var StepScheduler */ |
| 36 | private $stepScheduler; |
| 37 | |
| 38 | /** @var StepRunLoggerFactory */ |
| 39 | private $stepRunLoggerFactory; |
| 40 | |
| 41 | /** @var WordPress */ |
| 42 | private $wordPress; |
| 43 | |
| 44 | public function __construct( |
| 45 | AutomationStorage $automationStorage, |
| 46 | AutomationRunStorage $automationRunStorage, |
| 47 | SubjectLoader $subjectLoader, |
| 48 | SubjectTransformerHandler $subjectTransformerHandler, |
| 49 | FilterHandler $filterHandler, |
| 50 | StepScheduler $stepScheduler, |
| 51 | StepRunLoggerFactory $stepRunLoggerFactory, |
| 52 | WordPress $wordPress |
| 53 | ) { |
| 54 | $this->automationStorage = $automationStorage; |
| 55 | $this->automationRunStorage = $automationRunStorage; |
| 56 | $this->subjectLoader = $subjectLoader; |
| 57 | $this->subjectTransformerHandler = $subjectTransformerHandler; |
| 58 | $this->filterHandler = $filterHandler; |
| 59 | $this->stepScheduler = $stepScheduler; |
| 60 | $this->stepRunLoggerFactory = $stepRunLoggerFactory; |
| 61 | $this->wordPress = $wordPress; |
| 62 | } |
| 63 | |
| 64 | public function initialize(): void { |
| 65 | $this->wordPress->addAction(Hooks::TRIGGER, [$this, 'processTrigger'], 10, 2); |
| 66 | } |
| 67 | |
| 68 | /** @param Subject[] $subjects */ |
| 69 | public function processTrigger(Trigger $trigger, array $subjects): void { |
| 70 | $automations = $this->automationStorage->getActiveAutomationsByTrigger($trigger); |
| 71 | if (!$automations) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // expand all subject transformations and load subject entries |
| 76 | $subjects = $this->subjectTransformerHandler->getAllSubjects($subjects); |
| 77 | $subjectEntries = $this->subjectLoader->getSubjectsEntries($subjects); |
| 78 | |
| 79 | foreach ($automations as $automation) { |
| 80 | $step = $automation->getTrigger($trigger->getKey()); |
| 81 | if (!$step) { |
| 82 | throw Exceptions::automationTriggerNotFound($automation->getId(), $trigger->getKey()); |
| 83 | } |
| 84 | |
| 85 | $automationRun = new AutomationRun($automation->getId(), $automation->getVersionId(), $trigger->getKey(), $subjects); |
| 86 | $stepRunArgs = new StepRunArgs($automation, $automationRun, $step, $subjectEntries, 1); |
| 87 | |
| 88 | $match = false; |
| 89 | try { |
| 90 | $match = $this->filterHandler->matchesFilters($stepRunArgs); |
| 91 | } catch (Exceptions\Exception $e) { |
| 92 | // failed filter evaluation won't match |
| 93 | ; |
| 94 | } |
| 95 | if (!$match) { |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | $createAutomationRun = $trigger->isTriggeredBy($stepRunArgs); |
| 100 | $createAutomationRun = $this->wordPress->applyFilters( |
| 101 | Hooks::AUTOMATION_RUN_CREATE, |
| 102 | $createAutomationRun, |
| 103 | $stepRunArgs |
| 104 | ); |
| 105 | if (!$createAutomationRun) { |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | $automationRunId = $this->automationRunStorage->createAutomationRun($automationRun); |
| 111 | $automationRun->setId($automationRunId); |
| 112 | $this->stepScheduler->scheduleNextStep($stepRunArgs); |
| 113 | |
| 114 | $logger = $this->stepRunLoggerFactory->createLogger($automationRunId, $step->getId(), AutomationRunLog::TYPE_TRIGGER, 1); |
| 115 | $logger->logStepData($step); |
| 116 | $logger->logSuccess(); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 |