Use subject loader in trigger handler
[MAILPOET-4454]
This commit is contained in:
@@ -7,7 +7,6 @@ use MailPoet\Automation\Engine\Hooks;
|
|||||||
use MailPoet\Automation\Engine\Storage\WorkflowRunStorage;
|
use MailPoet\Automation\Engine\Storage\WorkflowRunStorage;
|
||||||
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
||||||
use MailPoet\Automation\Engine\WordPress;
|
use MailPoet\Automation\Engine\WordPress;
|
||||||
use MailPoet\Automation\Engine\Workflows\Subject;
|
|
||||||
use MailPoet\Automation\Engine\Workflows\Trigger;
|
use MailPoet\Automation\Engine\Workflows\Trigger;
|
||||||
use MailPoet\Automation\Engine\Workflows\WorkflowRun;
|
use MailPoet\Automation\Engine\Workflows\WorkflowRun;
|
||||||
|
|
||||||
@@ -15,6 +14,9 @@ class TriggerHandler {
|
|||||||
/** @var ActionScheduler */
|
/** @var ActionScheduler */
|
||||||
private $actionScheduler;
|
private $actionScheduler;
|
||||||
|
|
||||||
|
/** @var SubjectLoader */
|
||||||
|
private $subjectLoader;
|
||||||
|
|
||||||
/** @var WordPress */
|
/** @var WordPress */
|
||||||
private $wordPress;
|
private $wordPress;
|
||||||
|
|
||||||
@@ -26,6 +28,7 @@ class TriggerHandler {
|
|||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
ActionScheduler $actionScheduler,
|
ActionScheduler $actionScheduler,
|
||||||
|
SubjectLoader $subjectLoader,
|
||||||
WordPress $wordPress,
|
WordPress $wordPress,
|
||||||
WorkflowStorage $workflowStorage,
|
WorkflowStorage $workflowStorage,
|
||||||
WorkflowRunStorage $workflowRunStorage
|
WorkflowRunStorage $workflowRunStorage
|
||||||
@@ -34,13 +37,14 @@ class TriggerHandler {
|
|||||||
$this->wordPress = $wordPress;
|
$this->wordPress = $wordPress;
|
||||||
$this->workflowStorage = $workflowStorage;
|
$this->workflowStorage = $workflowStorage;
|
||||||
$this->workflowRunStorage = $workflowRunStorage;
|
$this->workflowRunStorage = $workflowRunStorage;
|
||||||
|
$this->subjectLoader = $subjectLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function initialize(): void {
|
public function initialize(): void {
|
||||||
$this->wordPress->addAction(Hooks::TRIGGER, [$this, 'processTrigger'], 10, 2);
|
$this->wordPress->addAction(Hooks::TRIGGER, [$this, 'processTrigger'], 10, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param Subject[] $subjects */
|
/** @param array<string, array> $subjects */
|
||||||
public function processTrigger(Trigger $trigger, array $subjects): void {
|
public function processTrigger(Trigger $trigger, array $subjects): void {
|
||||||
$workflows = $this->workflowStorage->getActiveWorkflowsByTrigger($trigger);
|
$workflows = $this->workflowStorage->getActiveWorkflowsByTrigger($trigger);
|
||||||
foreach ($workflows as $workflow) {
|
foreach ($workflows as $workflow) {
|
||||||
@@ -49,7 +53,13 @@ class TriggerHandler {
|
|||||||
throw Exceptions::workflowTriggerNotFound($workflow->getId(), $trigger->getKey());
|
throw Exceptions::workflowTriggerNotFound($workflow->getId(), $trigger->getKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
$workflowRun = new WorkflowRun($workflow->getId(), $trigger->getKey(), $subjects);
|
// ensure subjects are registered and loadable
|
||||||
|
$loadedSubjects = [];
|
||||||
|
foreach ($subjects as $key => $args) {
|
||||||
|
$loadedSubjects[] = $this->subjectLoader->loadSubject($key, $args);
|
||||||
|
}
|
||||||
|
|
||||||
|
$workflowRun = new WorkflowRun($workflow->getId(), $trigger->getKey(), $loadedSubjects);
|
||||||
$workflowRunId = $this->workflowRunStorage->createWorkflowRun($workflowRun);
|
$workflowRunId = $this->workflowRunStorage->createWorkflowRun($workflowRun);
|
||||||
|
|
||||||
$this->actionScheduler->enqueue(Hooks::WORKFLOW_STEP, [
|
$this->actionScheduler->enqueue(Hooks::WORKFLOW_STEP, [
|
||||||
|
@@ -10,6 +10,8 @@ use MailPoet\NotFoundException;
|
|||||||
use MailPoet\Segments\SegmentsRepository;
|
use MailPoet\Segments\SegmentsRepository;
|
||||||
|
|
||||||
class SegmentSubject implements Subject {
|
class SegmentSubject implements Subject {
|
||||||
|
const KEY = 'mailpoet:segment';
|
||||||
|
|
||||||
/** @var Field[] */
|
/** @var Field[] */
|
||||||
private $fields;
|
private $fields;
|
||||||
|
|
||||||
@@ -47,7 +49,7 @@ class SegmentSubject implements Subject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getKey(): string {
|
public function getKey(): string {
|
||||||
return 'mailpoet:segment';
|
return self::KEY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFields(): array {
|
public function getFields(): array {
|
||||||
|
@@ -10,6 +10,8 @@ use MailPoet\NotFoundException;
|
|||||||
use MailPoet\Subscribers\SubscribersRepository;
|
use MailPoet\Subscribers\SubscribersRepository;
|
||||||
|
|
||||||
class SubscriberSubject implements Subject {
|
class SubscriberSubject implements Subject {
|
||||||
|
const KEY = 'mailpoet:subscriber';
|
||||||
|
|
||||||
/** @var Field[] */
|
/** @var Field[] */
|
||||||
private $fields;
|
private $fields;
|
||||||
|
|
||||||
@@ -62,7 +64,7 @@ class SubscriberSubject implements Subject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getKey(): string {
|
public function getKey(): string {
|
||||||
return 'mailpoet:subscriber';
|
return self::KEY;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getFields(): array {
|
public function getFields(): array {
|
||||||
|
@@ -11,22 +11,12 @@ use MailPoet\InvalidStateException;
|
|||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
|
|
||||||
class SegmentSubscribedTrigger implements Trigger {
|
class SegmentSubscribedTrigger implements Trigger {
|
||||||
/** @var SegmentSubject */
|
|
||||||
private $segmentSubject;
|
|
||||||
|
|
||||||
/** @var SubscriberSubject */
|
|
||||||
private $subscriberSubject;
|
|
||||||
|
|
||||||
/** @var WPFunctions */
|
/** @var WPFunctions */
|
||||||
private $wp;
|
private $wp;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
SegmentSubject $segmentSubject,
|
|
||||||
SubscriberSubject $subscriberSubject,
|
|
||||||
WPFunctions $wp
|
WPFunctions $wp
|
||||||
) {
|
) {
|
||||||
$this->segmentSubject = $segmentSubject;
|
|
||||||
$this->subscriberSubject = $subscriberSubject;
|
|
||||||
$this->wp = $wp;
|
$this->wp = $wp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,13 +28,6 @@ class SegmentSubscribedTrigger implements Trigger {
|
|||||||
return __('Subscribed to segment');
|
return __('Subscribed to segment');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSubjects(): array {
|
|
||||||
return [
|
|
||||||
$this->segmentSubject,
|
|
||||||
$this->subscriberSubject,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function registerHooks(): void {
|
public function registerHooks(): void {
|
||||||
$this->wp->addAction('mailpoet_segment_subscribed', [$this, 'handleSubscription'], 10, 2);
|
$this->wp->addAction('mailpoet_segment_subscribed', [$this, 'handleSubscription'], 10, 2);
|
||||||
}
|
}
|
||||||
@@ -57,12 +40,9 @@ class SegmentSubscribedTrigger implements Trigger {
|
|||||||
throw new InvalidStateException();
|
throw new InvalidStateException();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->segmentSubject->load(['segment_id' => $segment->getId()]);
|
|
||||||
$this->subscriberSubject->load(['subscriber_id' => $subscriber->getId()]);
|
|
||||||
|
|
||||||
$this->wp->doAction(Hooks::TRIGGER, $this, [
|
$this->wp->doAction(Hooks::TRIGGER, $this, [
|
||||||
$this->segmentSubject,
|
SegmentSubject::KEY => ['segment_id' => $segment->getId()],
|
||||||
$this->subscriberSubject,
|
SubscriberSubject::KEY => ['subscriber_id' => $subscriber->getId()],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user