AutomationTemplateEmailPreviewEndpoint.php
1 month ago
AutomationTemplateGetEndpoint.php
1 year ago
AutomationTemplatesGetEndpoint.php
2 years ago
AutomationVersionsGetEndpoint.php
2 months ago
AutomationsCreateFromTemplateEndpoint.php
2 months ago
AutomationsDeleteEndpoint.php
2 years ago
AutomationsDuplicateEndpoint.php
2 years ago
AutomationsGetEndpoint.php
9 months ago
AutomationsPutEndpoint.php
2 months ago
index.php
3 years ago
AutomationTemplateGetEndpoint.php
63 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Endpoints\Automations; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\REST\Request; |
| 9 | use MailPoet\API\REST\Response; |
| 10 | use MailPoet\Automation\Engine\API\Endpoint; |
| 11 | use MailPoet\Automation\Engine\Exceptions; |
| 12 | use MailPoet\Automation\Engine\Mappers\AutomationMapper; |
| 13 | use MailPoet\Automation\Engine\Registry; |
| 14 | use MailPoet\Automation\Engine\Validation\AutomationValidator; |
| 15 | use MailPoet\Validator\Builder; |
| 16 | |
| 17 | class AutomationTemplateGetEndpoint extends Endpoint { |
| 18 | /** @var AutomationMapper */ |
| 19 | private $automationMapper; |
| 20 | |
| 21 | /** @var AutomationValidator */ |
| 22 | private $automationValidator; |
| 23 | |
| 24 | /** @var Registry */ |
| 25 | private $registry; |
| 26 | |
| 27 | public function __construct( |
| 28 | AutomationMapper $automationMapper, |
| 29 | AutomationValidator $automationValidator, |
| 30 | Registry $registry |
| 31 | ) { |
| 32 | $this->registry = $registry; |
| 33 | $this->automationValidator = $automationValidator; |
| 34 | $this->automationMapper = $automationMapper; |
| 35 | } |
| 36 | |
| 37 | public function handle(Request $request): Response { |
| 38 | /** @var string|null $slug - for PHPStan because strval() doesn't accept a value of mixed */ |
| 39 | $slug = $request->getParam('slug'); |
| 40 | $slug = strval($slug); |
| 41 | $template = $this->registry->getTemplate($slug); |
| 42 | if (!$template) { |
| 43 | throw Exceptions::automationTemplateNotFound($slug); |
| 44 | } |
| 45 | |
| 46 | $automation = $template->createAutomation((bool)$request->getParam('preview')); |
| 47 | $automation->setId(0); |
| 48 | $this->automationValidator->validate($automation); |
| 49 | |
| 50 | $data = $template->toArray() + [ |
| 51 | 'automation' => $this->automationMapper->buildAutomation($automation), |
| 52 | ]; |
| 53 | return new Response($data); |
| 54 | } |
| 55 | |
| 56 | public static function getRequestSchema(): array { |
| 57 | return [ |
| 58 | 'slug' => Builder::string()->required(), |
| 59 | 'preview' => Builder::boolean(), |
| 60 | ]; |
| 61 | } |
| 62 | } |
| 63 |