Persist all possible subject on run creation
[MAILPOET-4935]
This commit is contained in:
@@ -5,7 +5,7 @@ namespace MailPoet\Automation\Engine\Control;
|
|||||||
use MailPoet\Automation\Engine\Data\Automation;
|
use MailPoet\Automation\Engine\Data\Automation;
|
||||||
use MailPoet\Automation\Engine\Data\AutomationRun;
|
use MailPoet\Automation\Engine\Data\AutomationRun;
|
||||||
use MailPoet\Automation\Engine\Data\Step as StepData;
|
use MailPoet\Automation\Engine\Data\Step as StepData;
|
||||||
use MailPoet\Automation\Engine\Data\Subject as SubjectData;
|
use MailPoet\Automation\Engine\Data\Subject;
|
||||||
use MailPoet\Automation\Engine\Integration\Step;
|
use MailPoet\Automation\Engine\Integration\Step;
|
||||||
use MailPoet\Automation\Engine\Integration\SubjectTransformer;
|
use MailPoet\Automation\Engine\Integration\SubjectTransformer;
|
||||||
use MailPoet\Automation\Engine\Integration\Trigger;
|
use MailPoet\Automation\Engine\Integration\Trigger;
|
||||||
@@ -97,7 +97,42 @@ class SubjectTransformerHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return SubjectData[]|null
|
* @param Trigger $trigger
|
||||||
|
* @param Subject ...$subjects
|
||||||
|
* @return Subject[]
|
||||||
|
*/
|
||||||
|
public function provideAllSubjects(Trigger $trigger, Subject ...$subjects): array {
|
||||||
|
$allSubjectsKeys = $this->subjectKeysForTrigger($trigger);
|
||||||
|
$allSubjectKeyTargets = array_diff($allSubjectsKeys, array_map(
|
||||||
|
function(Subject $subject): string {
|
||||||
|
return $subject->getKey();
|
||||||
|
},
|
||||||
|
$subjects
|
||||||
|
));
|
||||||
|
|
||||||
|
$allSubjects = [];
|
||||||
|
foreach ($subjects as $existingSubject) {
|
||||||
|
$allSubjects[$existingSubject->getKey()] = $existingSubject;
|
||||||
|
}
|
||||||
|
foreach ($allSubjectKeyTargets as $target) {
|
||||||
|
if (isset($allSubjects[$target])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
foreach ($subjects as $subject) {
|
||||||
|
$transformedSubject = $this->transformSubjectTo($subject, $target);
|
||||||
|
if ($transformedSubject) {
|
||||||
|
$allSubjects[$transformedSubject->getKey()] = $transformedSubject;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (count($allSubjects) < count($allSubjectsKeys)) {
|
||||||
|
$allSubjects = $this->provideAllSubjects($trigger, ...array_values($allSubjects));
|
||||||
|
}
|
||||||
|
return array_values($allSubjects);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Subject[]|null
|
||||||
*/
|
*/
|
||||||
public function transformSubjectData(string $target, AutomationRun $automationRun): ?array {
|
public function transformSubjectData(string $target, AutomationRun $automationRun): ?array {
|
||||||
$automation = $this->automationStorage->getAutomation($automationRun->getAutomationId(), $automationRun->getVersionId());
|
$automation = $this->automationStorage->getAutomation($automationRun->getAutomationId(), $automationRun->getVersionId());
|
||||||
@@ -108,18 +143,26 @@ class SubjectTransformerHandler {
|
|||||||
$transformedSubjects = [];
|
$transformedSubjects = [];
|
||||||
$subjects = $automationRun->getSubjects();
|
$subjects = $automationRun->getSubjects();
|
||||||
foreach ($subjects as $subject) {
|
foreach ($subjects as $subject) {
|
||||||
$transformerChain = $this->getTransformerChain($subject->getKey(), $target);
|
$transformedSubject = $this->transformSubjectTo($subject, $target);
|
||||||
if (!$transformerChain) {
|
if (!$transformedSubject) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
foreach ($transformerChain as $transformer) {
|
|
||||||
$subject = $transformer->transform($subject);
|
|
||||||
}
|
|
||||||
$transformedSubjects[] = $subject;
|
$transformedSubjects[] = $subject;
|
||||||
}
|
}
|
||||||
return count($transformedSubjects) > 0 ? $transformedSubjects : null;
|
return count($transformedSubjects) > 0 ? $transformedSubjects : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function transformSubjectTo(Subject $subject, string $target): ?Subject {
|
||||||
|
$transformerChain = $this->getTransformerChain($subject->getKey(), $target);
|
||||||
|
if (!$transformerChain) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
foreach ($transformerChain as $transformer) {
|
||||||
|
$subject = $transformer->transform($subject);
|
||||||
|
}
|
||||||
|
return $subject;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return SubjectTransformer[]
|
* @return SubjectTransformer[]
|
||||||
*/
|
*/
|
||||||
|
@@ -29,15 +29,20 @@ class TriggerHandler {
|
|||||||
/** @var AutomationRunStorage */
|
/** @var AutomationRunStorage */
|
||||||
private $automationRunStorage;
|
private $automationRunStorage;
|
||||||
|
|
||||||
|
/** @var Functions */
|
||||||
private $wp;
|
private $wp;
|
||||||
|
|
||||||
|
/** @var SubjectTransformerHandler */
|
||||||
|
private $subjectTransformerHandler;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
ActionScheduler $actionScheduler,
|
ActionScheduler $actionScheduler,
|
||||||
SubjectLoader $subjectLoader,
|
SubjectLoader $subjectLoader,
|
||||||
WordPress $wordPress,
|
WordPress $wordPress,
|
||||||
AutomationStorage $automationStorage,
|
AutomationStorage $automationStorage,
|
||||||
AutomationRunStorage $automationRunStorage,
|
AutomationRunStorage $automationRunStorage,
|
||||||
Functions $wp
|
Functions $wp,
|
||||||
|
SubjectTransformerHandler $subjectTransformerHandler
|
||||||
) {
|
) {
|
||||||
$this->actionScheduler = $actionScheduler;
|
$this->actionScheduler = $actionScheduler;
|
||||||
$this->wordPress = $wordPress;
|
$this->wordPress = $wordPress;
|
||||||
@@ -45,6 +50,7 @@ class TriggerHandler {
|
|||||||
$this->automationRunStorage = $automationRunStorage;
|
$this->automationRunStorage = $automationRunStorage;
|
||||||
$this->subjectLoader = $subjectLoader;
|
$this->subjectLoader = $subjectLoader;
|
||||||
$this->wp = $wp;
|
$this->wp = $wp;
|
||||||
|
$this->subjectTransformerHandler = $subjectTransformerHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function initialize(): void {
|
public function initialize(): void {
|
||||||
@@ -53,6 +59,7 @@ class TriggerHandler {
|
|||||||
|
|
||||||
/** @param Subject[] $subjects */
|
/** @param Subject[] $subjects */
|
||||||
public function processTrigger(Trigger $trigger, array $subjects): void {
|
public function processTrigger(Trigger $trigger, array $subjects): void {
|
||||||
|
$subjects = $this->subjectTransformerHandler->provideAllSubjects($trigger, ...$subjects);
|
||||||
$automations = $this->automationStorage->getActiveAutomationsByTrigger($trigger);
|
$automations = $this->automationStorage->getActiveAutomationsByTrigger($trigger);
|
||||||
foreach ($automations as $automation) {
|
foreach ($automations as $automation) {
|
||||||
$step = $automation->getTrigger($trigger->getKey());
|
$step = $automation->getTrigger($trigger->getKey());
|
||||||
|
Reference in New Issue
Block a user