mailpoet
/
lib
/
Automation
/
Engine
/
Endpoints
/
Automations
/
AutomationTemplateEmailPreviewEndpoint.php
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
AutomationTemplateEmailPreviewEndpoint.php
52 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\Integrations\MailPoet\Templates\TemplateEmailPreviewRenderer; |
| 13 | use MailPoet\Validator\Builder; |
| 14 | |
| 15 | class AutomationTemplateEmailPreviewEndpoint extends Endpoint { |
| 16 | /** @var TemplateEmailPreviewRenderer */ |
| 17 | private $previewRenderer; |
| 18 | |
| 19 | public function __construct( |
| 20 | TemplateEmailPreviewRenderer $previewRenderer |
| 21 | ) { |
| 22 | $this->previewRenderer = $previewRenderer; |
| 23 | } |
| 24 | |
| 25 | public function handle(Request $request): Response { |
| 26 | /** @var string|null $patternParam - for PHPStan because strval() doesn't accept a value of mixed */ |
| 27 | $patternParam = $request->getParam('pattern'); |
| 28 | $pattern = strval($patternParam); |
| 29 | /** @var string|null $subjectParam */ |
| 30 | $subjectParam = $request->getParam('subject'); |
| 31 | $subject = strval($subjectParam ?? ''); |
| 32 | /** @var string|null $preheaderParam */ |
| 33 | $preheaderParam = $request->getParam('preheader'); |
| 34 | $preheader = strval($preheaderParam ?? ''); |
| 35 | |
| 36 | $html = $this->previewRenderer->render($pattern, $subject, $preheader); |
| 37 | if ($html === null) { |
| 38 | throw Exceptions::automationTemplateEmailPatternNotFound($pattern); |
| 39 | } |
| 40 | |
| 41 | return new Response(['html' => $html]); |
| 42 | } |
| 43 | |
| 44 | public static function getRequestSchema(): array { |
| 45 | return [ |
| 46 | 'pattern' => Builder::string()->required(), |
| 47 | 'subject' => Builder::string(), |
| 48 | 'preheader' => Builder::string(), |
| 49 | ]; |
| 50 | } |
| 51 | } |
| 52 |