CreateAutomationFromTemplateController.php
2 months ago
DeleteAutomationController.php
2 months ago
DuplicateAutomationController.php
2 months ago
UpdateAutomationController.php
2 months ago
UpdateStepsController.php
11 months ago
index.php
3 years ago
UpdateStepsController.php
72 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Builder; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Engine\Data\Step; |
| 10 | use MailPoet\Automation\Engine\Exceptions; |
| 11 | use MailPoet\Automation\Engine\Registry; |
| 12 | |
| 13 | class UpdateStepsController { |
| 14 | /** @var Registry */ |
| 15 | private $registry; |
| 16 | |
| 17 | public function __construct( |
| 18 | Registry $registry |
| 19 | ) { |
| 20 | $this->registry = $registry; |
| 21 | } |
| 22 | |
| 23 | public function updateSteps(Automation $automation, array $data): Automation { |
| 24 | $steps = []; |
| 25 | foreach ($data as $index => $stepData) { |
| 26 | $step = $this->processStep($stepData, $automation->getStep($stepData['id'])); |
| 27 | $updatedStep = $this->maybeRunOnDuplicate($step); |
| 28 | $steps[$index] = $updatedStep; |
| 29 | } |
| 30 | $automation->setSteps($steps); |
| 31 | return $automation; |
| 32 | } |
| 33 | |
| 34 | private function maybeRunOnDuplicate(Step $step): Step { |
| 35 | if ($step->getType() !== 'action') { |
| 36 | return $step; |
| 37 | } |
| 38 | |
| 39 | $args = $step->getArgs(); |
| 40 | if (empty($args['stepDuplicated'])) { |
| 41 | return $step; |
| 42 | } |
| 43 | |
| 44 | $action = $this->registry->getAction($step->getKey()); |
| 45 | if (!$action) { |
| 46 | return $step; |
| 47 | } |
| 48 | |
| 49 | $duplicatedStep = $action->onDuplicate($step); |
| 50 | $dupArgs = $duplicatedStep->getArgs(); |
| 51 | unset($dupArgs['stepDuplicated']); |
| 52 | |
| 53 | return new Step( |
| 54 | $duplicatedStep->getId(), |
| 55 | $duplicatedStep->getType(), |
| 56 | $duplicatedStep->getKey(), |
| 57 | $dupArgs, |
| 58 | $duplicatedStep->getNextSteps(), |
| 59 | $duplicatedStep->getFilters() |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | private function processStep(array $data, ?Step $existingStep): Step { |
| 64 | $key = $data['key']; |
| 65 | $step = $this->registry->getStep($key); |
| 66 | if (!$step && $existingStep && $data !== $existingStep->toArray()) { |
| 67 | throw Exceptions::automationStepNotFound($key); |
| 68 | } |
| 69 | return Step::fromArray($data); |
| 70 | } |
| 71 | } |
| 72 |