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
CreateAutomationFromTemplateController.php
58 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\Exceptions; |
| 10 | use MailPoet\Automation\Engine\Exceptions\InvalidStateException; |
| 11 | use MailPoet\Automation\Engine\Hooks; |
| 12 | use MailPoet\Automation\Engine\Registry; |
| 13 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 14 | use MailPoet\Automation\Engine\Validation\AutomationValidator; |
| 15 | |
| 16 | class CreateAutomationFromTemplateController { |
| 17 | /** @var AutomationStorage */ |
| 18 | private $storage; |
| 19 | |
| 20 | /** @var AutomationValidator */ |
| 21 | private $automationValidator; |
| 22 | |
| 23 | /** @var Registry */ |
| 24 | private $registry; |
| 25 | |
| 26 | /** @var Hooks */ |
| 27 | private $hooks; |
| 28 | |
| 29 | public function __construct( |
| 30 | AutomationStorage $storage, |
| 31 | AutomationValidator $automationValidator, |
| 32 | Registry $registry, |
| 33 | Hooks $hooks |
| 34 | ) { |
| 35 | $this->storage = $storage; |
| 36 | $this->automationValidator = $automationValidator; |
| 37 | $this->registry = $registry; |
| 38 | $this->hooks = $hooks; |
| 39 | } |
| 40 | |
| 41 | public function createAutomation(string $slug): Automation { |
| 42 | $template = $this->registry->getTemplate($slug); |
| 43 | if (!$template) { |
| 44 | throw Exceptions::automationTemplateNotFound($slug); |
| 45 | } |
| 46 | |
| 47 | $automation = $template->createAutomation(); |
| 48 | $this->automationValidator->validate($automation); |
| 49 | $automationId = $this->storage->createAutomation($automation); |
| 50 | $savedAutomation = $this->storage->getAutomation($automationId); |
| 51 | if (!$savedAutomation) { |
| 52 | throw new InvalidStateException('Automation not found.'); |
| 53 | } |
| 54 | $this->hooks->doAutomationAfterCreateFromTemplate($savedAutomation, $slug); |
| 55 | return $savedAutomation; |
| 56 | } |
| 57 | } |
| 58 |