Save subject data in workflow runs

[MAILPOET-4136]
This commit is contained in:
Jan Jakes
2022-03-07 15:08:25 +01:00
committed by Veljko V
parent 50b889a1bd
commit 716050b7de
2 changed files with 23 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ 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;
@@ -36,10 +37,11 @@ class TriggerHandler {
} }
public function initialize(): void { public function initialize(): void {
$this->wordPress->addAction(Hooks::TRIGGER, [$this, 'processTrigger']); $this->wordPress->addAction(Hooks::TRIGGER, [$this, 'processTrigger'], 10, 2);
} }
public function processTrigger(Trigger $trigger): void { /** @param Subject[] $subjects */
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) {
$step = $workflow->getTrigger($trigger->getKey()); $step = $workflow->getTrigger($trigger->getKey());
@@ -47,7 +49,7 @@ class TriggerHandler {
throw Exceptions::workflowTriggerNotFound($workflow->getId(), $trigger->getKey()); throw Exceptions::workflowTriggerNotFound($workflow->getId(), $trigger->getKey());
} }
$workflowRun = new WorkflowRun($workflow->getId(), $trigger->getKey()); $workflowRun = new WorkflowRun($workflow->getId(), $trigger->getKey(), $subjects);
$workflowRunId = $this->workflowRunStorage->createWorkflowRun($workflowRun); $workflowRunId = $this->workflowRunStorage->createWorkflowRun($workflowRun);
$this->actionScheduler->enqueue(Hooks::WORKFLOW_STEP, [ $this->actionScheduler->enqueue(Hooks::WORKFLOW_STEP, [

View File

@@ -28,12 +28,17 @@ class WorkflowRun {
/** @var DateTimeImmutable */ /** @var DateTimeImmutable */
private $updatedAt; private $updatedAt;
/** @var Subject[] */
private $subjects;
public function __construct( public function __construct(
int $workflowId, int $workflowId,
string $triggerKey string $triggerKey,
array $subjects
) { ) {
$this->workflowId = $workflowId; $this->workflowId = $workflowId;
$this->triggerKey = $triggerKey; $this->triggerKey = $triggerKey;
$this->subjects = $subjects;
$now = new DateTimeImmutable(); $now = new DateTimeImmutable();
$this->createdAt = $now; $this->createdAt = $now;
@@ -64,6 +69,11 @@ class WorkflowRun {
return $this->updatedAt; return $this->updatedAt;
} }
/** @return Subject[] */
public function getSubjects(): array {
return $this->subjects;
}
public function toArray(): array { public function toArray(): array {
return [ return [
'workflow_id' => $this->workflowId, 'workflow_id' => $this->workflowId,
@@ -71,11 +81,17 @@ class WorkflowRun {
'status' => $this->status, 'status' => $this->status,
'created_at' => $this->createdAt->format(DateTimeImmutable::W3C), 'created_at' => $this->createdAt->format(DateTimeImmutable::W3C),
'updated_at' => $this->updatedAt->format(DateTimeImmutable::W3C), 'updated_at' => $this->updatedAt->format(DateTimeImmutable::W3C),
'subjects' => json_encode(
array_reduce($this->subjects, function (array $subjects, Subject $subject): array {
$subjects[$subject->getKey()] = $subject->pack();
return $subjects;
}, [])
),
]; ];
} }
public static function fromArray(array $data): self { public static function fromArray(array $data): self {
$workflowRun = new WorkflowRun((int)$data['workflow_id'], $data['trigger_key']); $workflowRun = new WorkflowRun((int)$data['workflow_id'], $data['trigger_key'], (array)json_decode($data['subjects'], true));
$workflowRun->id = (int)$data['id']; $workflowRun->id = (int)$data['id'];
$workflowRun->status = $data['status']; $workflowRun->status = $data['status'];
$workflowRun->createdAt = $data['created_at']; $workflowRun->createdAt = $data['created_at'];