AutomationBuilder.php
143 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Templates; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Engine\Data\Filter; |
| 10 | use MailPoet\Automation\Engine\Data\FilterGroup; |
| 11 | use MailPoet\Automation\Engine\Data\Filters; |
| 12 | use MailPoet\Automation\Engine\Data\NextStep; |
| 13 | use MailPoet\Automation\Engine\Data\Step; |
| 14 | use MailPoet\Automation\Engine\Exceptions\InvalidStateException; |
| 15 | use MailPoet\Automation\Engine\Integration\Trigger; |
| 16 | use MailPoet\Automation\Engine\Registry; |
| 17 | use MailPoet\Util\Security; |
| 18 | use MailPoet\Validator\Schema\ObjectSchema; |
| 19 | |
| 20 | class AutomationBuilder { |
| 21 | /** @var Registry */ |
| 22 | private $registry; |
| 23 | |
| 24 | public function __construct( |
| 25 | Registry $registry |
| 26 | ) { |
| 27 | $this->registry = $registry; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param string $name |
| 32 | * @param array< |
| 33 | * array{ |
| 34 | * key: string, |
| 35 | * args?: array<string, mixed>, |
| 36 | * filters?: array{ |
| 37 | * operator: 'and' | 'or', |
| 38 | * groups: array{ |
| 39 | * operator: 'and' | 'or', |
| 40 | * filters: array{ |
| 41 | * field: string, |
| 42 | * condition: string, |
| 43 | * value: mixed, |
| 44 | * }[], |
| 45 | * }[], |
| 46 | * }, |
| 47 | * } |
| 48 | * > $sequence |
| 49 | * @param array<string, mixed> $meta |
| 50 | * @return Automation |
| 51 | */ |
| 52 | public function createFromSequence(string $name, array $sequence, array $meta = []): Automation { |
| 53 | $steps = []; |
| 54 | $nextSteps = []; |
| 55 | foreach (array_reverse($sequence) as $data) { |
| 56 | $stepKey = $data['key']; |
| 57 | $automationStep = $this->registry->getStep($stepKey); |
| 58 | if (!$automationStep) { |
| 59 | continue; |
| 60 | } |
| 61 | $args = array_merge($this->getDefaultArgs($automationStep->getArgsSchema()), $data['args'] ?? []); |
| 62 | $filters = isset($data['filters']) ? $this->getFilters($data['filters']) : null; |
| 63 | $step = new Step( |
| 64 | $this->uniqueId(), |
| 65 | in_array(Trigger::class, (array)class_implements($automationStep)) ? Step::TYPE_TRIGGER : Step::TYPE_ACTION, |
| 66 | $stepKey, |
| 67 | $args, |
| 68 | $nextSteps, |
| 69 | $filters |
| 70 | ); |
| 71 | $nextSteps = [new NextStep($step->getId())]; |
| 72 | $steps[$step->getId()] = $step; |
| 73 | } |
| 74 | $steps['root'] = new Step('root', 'root', 'core:root', [], $nextSteps); |
| 75 | $steps = array_reverse($steps); |
| 76 | $automation = new Automation( |
| 77 | $name, |
| 78 | $steps, |
| 79 | wp_get_current_user() |
| 80 | ); |
| 81 | foreach ($meta as $key => $value) { |
| 82 | $automation->setMeta($key, $value); |
| 83 | } |
| 84 | return $automation; |
| 85 | } |
| 86 | |
| 87 | private function uniqueId(): string { |
| 88 | return Security::generateRandomString(16); |
| 89 | } |
| 90 | |
| 91 | private function getDefaultArgs(ObjectSchema $argsSchema): array { |
| 92 | $args = []; |
| 93 | foreach ($argsSchema->toArray()['properties'] ?? [] as $name => $schema) { |
| 94 | if (array_key_exists('default', $schema)) { |
| 95 | $args[$name] = $schema['default']; |
| 96 | } |
| 97 | } |
| 98 | return $args; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @param array{ |
| 103 | * operator: 'and' | 'or', |
| 104 | * groups: array{ |
| 105 | * operator: 'and' | 'or', |
| 106 | * filters: array{ |
| 107 | * field: string, |
| 108 | * condition: string, |
| 109 | * value: mixed, |
| 110 | * params?: array<string, mixed>, |
| 111 | * }[], |
| 112 | * }[], |
| 113 | * } $filters |
| 114 | * @return Filters |
| 115 | */ |
| 116 | private function getFilters(array $filters): Filters { |
| 117 | $groups = []; |
| 118 | foreach ($filters['groups'] as $group) { |
| 119 | $groups[] = new FilterGroup( |
| 120 | $this->uniqueId(), |
| 121 | $group['operator'], |
| 122 | array_map( |
| 123 | function (array $filter): Filter { |
| 124 | $field = $this->registry->getField($filter['field']); |
| 125 | if (!$field) { |
| 126 | throw new InvalidStateException(sprintf("Field with key '%s' not found", $filter['field'])); |
| 127 | } |
| 128 | return new Filter( |
| 129 | $this->uniqueId(), |
| 130 | $field->getType(), |
| 131 | $filter['field'], |
| 132 | $filter['condition'], |
| 133 | array_merge(['value' => $filter['value']], isset($filter['params']) ? ['params' => $filter['params']] : []), |
| 134 | ); |
| 135 | }, |
| 136 | $group['filters'] |
| 137 | ) |
| 138 | ); |
| 139 | } |
| 140 | return new Filters($filters['operator'], $groups); |
| 141 | } |
| 142 | } |
| 143 |