mailpoet
/
lib
/
Automation
/
Integrations
/
MailPoet
/
Analytics
/
Endpoints
/
OverviewEndpoint.php
AutomationFlowEndpoint.php
1 month ago
OverviewEndpoint.php
1 year ago
UpdateRunStatusEndpoint.php
2 months ago
index.php
3 years ago
OverviewEndpoint.php
52 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Analytics\Endpoints; |
| 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\Automation\Integrations\MailPoet\Analytics\Controller\OverviewStatisticsController; |
| 14 | use MailPoet\Automation\Integrations\MailPoet\Analytics\Entities\QueryWithCompare; |
| 15 | use MailPoet\Validator\Builder; |
| 16 | |
| 17 | class OverviewEndpoint extends Endpoint { |
| 18 | |
| 19 | /** @var AutomationStorage */ |
| 20 | private $automationStorage; |
| 21 | |
| 22 | /** @var OverviewStatisticsController */ |
| 23 | private $overviewStatisticsController; |
| 24 | |
| 25 | public function __construct( |
| 26 | AutomationStorage $automationStorage, |
| 27 | OverviewStatisticsController $overviewStatisticsController |
| 28 | ) { |
| 29 | $this->automationStorage = $automationStorage; |
| 30 | $this->overviewStatisticsController = $overviewStatisticsController; |
| 31 | } |
| 32 | |
| 33 | public function handle(Request $request): Response { |
| 34 | $id = absint(is_numeric($request->getParam('id')) ? $request->getParam('id') : 0); |
| 35 | $automation = $this->automationStorage->getAutomation($id); |
| 36 | if (!$automation) { |
| 37 | throw Exceptions::automationNotFound($id); |
| 38 | } |
| 39 | $query = QueryWithCompare::fromRequest($request); |
| 40 | |
| 41 | $result = $this->overviewStatisticsController->getStatisticsForAutomation($automation, $query); |
| 42 | return new Response($result); |
| 43 | } |
| 44 | |
| 45 | public static function getRequestSchema(): array { |
| 46 | return [ |
| 47 | 'id' => Builder::integer()->required(), |
| 48 | 'query' => QueryWithCompare::getRequestSchema(), |
| 49 | ]; |
| 50 | } |
| 51 | } |
| 52 |