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
AutomationsGetEndpoint.php
79 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\Mappers\AutomationMapper; |
| 12 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 13 | use MailPoet\Validator\Builder; |
| 14 | |
| 15 | class AutomationsGetEndpoint extends Endpoint { |
| 16 | /** @var AutomationMapper */ |
| 17 | private $automationMapper; |
| 18 | |
| 19 | /** @var AutomationStorage */ |
| 20 | private $automationStorage; |
| 21 | |
| 22 | public function __construct( |
| 23 | AutomationMapper $automationMapper, |
| 24 | AutomationStorage $automationStorage |
| 25 | ) { |
| 26 | $this->automationMapper = $automationMapper; |
| 27 | $this->automationStorage = $automationStorage; |
| 28 | } |
| 29 | |
| 30 | public function handle(Request $request): Response { |
| 31 | $status = $request->getParam('status') ? (array)$request->getParam('status') : null; |
| 32 | |
| 33 | $orderByParam = $request->getParam('orderby'); |
| 34 | $orderBy = is_string($orderByParam) ? $orderByParam : null; |
| 35 | |
| 36 | $orderParam = $request->getParam('order'); |
| 37 | $order = is_string($orderParam) ? $orderParam : null; |
| 38 | |
| 39 | $pageParam = $request->getParam('page'); |
| 40 | $page = is_numeric($pageParam) ? max(1, (int)$pageParam) : 1; |
| 41 | |
| 42 | $perPageParam = $request->getParam('per_page'); |
| 43 | $perPage = is_numeric($perPageParam) ? max(1, (int)$perPageParam) : null; |
| 44 | |
| 45 | $searchParam = $request->getParam('search'); |
| 46 | $search = is_string($searchParam) ? $searchParam : null; |
| 47 | |
| 48 | $automations = $this->automationStorage->getAutomations($status, $orderBy, $order, $page, $perPage, $search); |
| 49 | $automationCount = $this->automationStorage->getAutomationCount($status, $search); |
| 50 | |
| 51 | if ($automationCount === 0) { |
| 52 | $pages = 0; |
| 53 | } elseif ($perPage !== null) { |
| 54 | $pages = (int)ceil($automationCount / $perPage); |
| 55 | } else { |
| 56 | $pages = 1; |
| 57 | } |
| 58 | |
| 59 | return new Response([ |
| 60 | 'items' => $this->automationMapper->buildAutomationList($automations), |
| 61 | 'meta' => [ |
| 62 | 'pages' => $pages, |
| 63 | 'count' => $automationCount, |
| 64 | ], |
| 65 | ]); |
| 66 | } |
| 67 | |
| 68 | public static function getRequestSchema(): array { |
| 69 | return [ |
| 70 | 'status' => Builder::array(Builder::string()), |
| 71 | 'orderby' => Builder::string(), |
| 72 | 'order' => Builder::string(), |
| 73 | 'page' => Builder::integer(), |
| 74 | 'per_page' => Builder::integer(), |
| 75 | 'search' => Builder::string(), |
| 76 | ]; |
| 77 | } |
| 78 | } |
| 79 |