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
AutomationVersionsGetEndpoint.php
57 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\Storage\AutomationStorage; |
| 13 | use MailPoet\Validator\Builder; |
| 14 | |
| 15 | class AutomationVersionsGetEndpoint extends Endpoint { |
| 16 | /** @var AutomationStorage */ |
| 17 | private $automationStorage; |
| 18 | |
| 19 | public function __construct( |
| 20 | AutomationStorage $automationStorage |
| 21 | ) { |
| 22 | $this->automationStorage = $automationStorage; |
| 23 | } |
| 24 | |
| 25 | public function handle(Request $request): Response { |
| 26 | /** @var int $automationId */ |
| 27 | $automationId = $request->getParam('id'); |
| 28 | $automationId = intval($automationId); |
| 29 | $automation = $this->automationStorage->getAutomation($automationId); |
| 30 | if (!$automation) { |
| 31 | throw Exceptions::automationNotFound($automationId); |
| 32 | } |
| 33 | |
| 34 | $versions = $this->automationStorage->getAutomationVersionDates($automationId); |
| 35 | $currentVersionId = $automation->getVersionId(); |
| 36 | |
| 37 | $items = array_map( |
| 38 | function (array $version) use ($currentVersionId): array { |
| 39 | return [ |
| 40 | 'id' => $version['id'], |
| 41 | 'created_at' => $version['created_at']->format(\DateTimeInterface::ATOM), |
| 42 | 'is_current' => $version['id'] === $currentVersionId, |
| 43 | ]; |
| 44 | }, |
| 45 | $versions |
| 46 | ); |
| 47 | |
| 48 | return new Response(['items' => $items]); |
| 49 | } |
| 50 | |
| 51 | public static function getRequestSchema(): array { |
| 52 | return [ |
| 53 | 'id' => Builder::integer()->required(), |
| 54 | ]; |
| 55 | } |
| 56 | } |
| 57 |