mailpoet
/
lib
/
Automation
/
Integrations
/
MailPoet
/
Analytics
/
Endpoints
/
AutomationFlowEndpoint.php
AutomationFlowEndpoint.php
2 months ago
OverviewEndpoint.php
1 year ago
UpdateRunStatusEndpoint.php
2 months ago
index.php
3 years ago
AutomationFlowEndpoint.php
125 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\Data\Automation; |
| 12 | use MailPoet\Automation\Engine\Exceptions; |
| 13 | use MailPoet\Automation\Engine\Mappers\AutomationMapper; |
| 14 | use MailPoet\Automation\Engine\Storage\AutomationStatisticsStorage; |
| 15 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 16 | use MailPoet\Automation\Integrations\MailPoet\Analytics\Controller\AutomationTimeSpanController; |
| 17 | use MailPoet\Automation\Integrations\MailPoet\Analytics\Controller\StepStatisticController; |
| 18 | use MailPoet\Automation\Integrations\MailPoet\Analytics\Entities\Query; |
| 19 | use MailPoet\Validator\Builder; |
| 20 | |
| 21 | class AutomationFlowEndpoint extends Endpoint { |
| 22 | |
| 23 | /** @var AutomationStorage */ |
| 24 | private $automationStorage; |
| 25 | |
| 26 | /** @var AutomationStatisticsStorage */ |
| 27 | private $automationStatisticsStorage; |
| 28 | |
| 29 | /** @var AutomationMapper */ |
| 30 | private $automationMapper; |
| 31 | |
| 32 | /** @var AutomationTimeSpanController */ |
| 33 | private $automationTimeSpanController; |
| 34 | |
| 35 | /** @var StepStatisticController */ |
| 36 | private $stepStatisticController; |
| 37 | |
| 38 | public function __construct( |
| 39 | AutomationStorage $automationStorage, |
| 40 | AutomationStatisticsStorage $automationStatisticsStorage, |
| 41 | AutomationMapper $automationMapper, |
| 42 | AutomationTimeSpanController $automationTimeSpanController, |
| 43 | StepStatisticController $stepStatisticController |
| 44 | ) { |
| 45 | $this->automationStorage = $automationStorage; |
| 46 | $this->automationStatisticsStorage = $automationStatisticsStorage; |
| 47 | $this->automationMapper = $automationMapper; |
| 48 | $this->automationTimeSpanController = $automationTimeSpanController; |
| 49 | $this->stepStatisticController = $stepStatisticController; |
| 50 | } |
| 51 | |
| 52 | public function handle(Request $request): Response { |
| 53 | $id = absint(is_numeric($request->getParam('id')) ? $request->getParam('id') : 0); |
| 54 | $automation = $this->automationStorage->getAutomation($id); |
| 55 | if (!$automation) { |
| 56 | throw Exceptions::automationNotFound($id); |
| 57 | } |
| 58 | $query = Query::fromRequest($request); |
| 59 | $automations = $this->automationTimeSpanController->getAutomationsInTimespan($automation, $query->getAfter(), $query->getBefore(), $query->getVersionId()); |
| 60 | if (!count($automations)) { |
| 61 | throw Exceptions::automationNotFoundInTimeSpan($id); |
| 62 | } |
| 63 | $automation = current($automations); |
| 64 | $shortStatistics = $this->automationStatisticsStorage->getAutomationStats( |
| 65 | $automation->getId(), |
| 66 | $query->getVersionId(), |
| 67 | $query->getAfter(), |
| 68 | $query->getBefore() |
| 69 | ); |
| 70 | |
| 71 | $waitingData = $this->stepStatisticController->getWaitingStatistics($automation, $query); |
| 72 | $failedData = $this->stepStatisticController->getFailedStatistics($automation, $query); |
| 73 | try { |
| 74 | $completedData = $this->stepStatisticController->getCompletedStatistics($automation, $query); |
| 75 | } catch (\Throwable $e) { |
| 76 | return new Response([$e->getMessage()], 500); |
| 77 | } |
| 78 | $stepData = [ |
| 79 | 'total' => $shortStatistics->getEntered(), |
| 80 | ]; |
| 81 | if ($waitingData) { |
| 82 | $stepData['waiting'] = $waitingData; |
| 83 | } |
| 84 | if ($failedData) { |
| 85 | $stepData['failed'] = $failedData; |
| 86 | } |
| 87 | if ($completedData) { |
| 88 | $stepData['completed'] = $completedData; |
| 89 | } |
| 90 | |
| 91 | $data = [ |
| 92 | 'automation' => $this->automationMapper->buildAutomation($automation, $shortStatistics), |
| 93 | 'step_data' => $stepData, |
| 94 | 'tree_is_inconsistent' => !$this->isTreeConsistent(...$automations), |
| 95 | ]; |
| 96 | return new Response($data); |
| 97 | } |
| 98 | |
| 99 | private function isTreeConsistent(Automation ...$automations): bool { |
| 100 | if (count($automations) === 1) { |
| 101 | return true; |
| 102 | } |
| 103 | $stepIds = array_map(function (Automation $automation) { |
| 104 | return array_keys($automation->getSteps()); |
| 105 | }, $automations); |
| 106 | $compareTo = array_shift($stepIds); |
| 107 | if (!$compareTo) { |
| 108 | return true; |
| 109 | } |
| 110 | foreach ($stepIds as $stepId) { |
| 111 | if (count(array_diff($stepId, $compareTo)) !== 0) { |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | public static function getRequestSchema(): array { |
| 119 | return [ |
| 120 | 'id' => Builder::integer()->required(), |
| 121 | 'query' => Query::getRequestSchema(), |
| 122 | ]; |
| 123 | } |
| 124 | } |
| 125 |