Add hook and basic handler for automation triggers
For now trigger keys are stored in the workflow as a JSON array. This is not optimal in case someone has many workflows but as workflows are user-created it's unlinkely someone will have thousands of them. We can consider adding a workflow_triggers table as well. [MAILPOET-4136]
This commit is contained in:
46
mailpoet/lib/Automation/Engine/Control/TriggerHandler.php
Normal file
46
mailpoet/lib/Automation/Engine/Control/TriggerHandler.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\Control;
|
||||
|
||||
use MailPoet\Automation\Engine\Exceptions;
|
||||
use MailPoet\Automation\Engine\Hooks;
|
||||
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
||||
use MailPoet\Automation\Engine\WordPress;
|
||||
use MailPoet\Automation\Engine\Workflows\Trigger;
|
||||
|
||||
class TriggerHandler {
|
||||
/** @var ActionScheduler */
|
||||
private $actionScheduler;
|
||||
|
||||
/** @var WordPress */
|
||||
private $wordPress;
|
||||
|
||||
/** @var WorkflowStorage */
|
||||
private $workflowStorage;
|
||||
|
||||
public function __construct(
|
||||
ActionScheduler $actionScheduler,
|
||||
WordPress $wordPress,
|
||||
WorkflowStorage $workflowStorage
|
||||
) {
|
||||
$this->actionScheduler = $actionScheduler;
|
||||
$this->wordPress = $wordPress;
|
||||
$this->workflowStorage = $workflowStorage;
|
||||
}
|
||||
|
||||
public function initialize(): void {
|
||||
$this->wordPress->addAction(Hooks::TRIGGER, [$this, 'processTrigger']);
|
||||
}
|
||||
|
||||
public function processTrigger(Trigger $trigger): void {
|
||||
$workflows = $this->workflowStorage->getActiveWorkflowsByTrigger($trigger);
|
||||
foreach ($workflows as $workflow) {
|
||||
$step = $workflow->getTrigger($trigger->getKey());
|
||||
if (!$step) {
|
||||
throw Exceptions::workflowTriggerNotFound($workflow->getId(), $trigger->getKey());
|
||||
}
|
||||
|
||||
// TODO: create new workflow run
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user