AutomationMapper.php
97 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Engine\Mappers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use DateTimeImmutable; |
| 9 | use MailPoet\Automation\Engine\Data\Automation; |
| 10 | use MailPoet\Automation\Engine\Data\AutomationRun; |
| 11 | use MailPoet\Automation\Engine\Data\AutomationStatistics; |
| 12 | use MailPoet\Automation\Engine\Data\NextStep; |
| 13 | use MailPoet\Automation\Engine\Data\Step; |
| 14 | use MailPoet\Automation\Engine\Storage\AutomationRunStorage; |
| 15 | use MailPoet\Automation\Engine\Storage\AutomationStatisticsStorage; |
| 16 | |
| 17 | class AutomationMapper { |
| 18 | /** @var AutomationStatisticsStorage */ |
| 19 | private $statisticsStorage; |
| 20 | |
| 21 | /** @var AutomationRunStorage */ |
| 22 | private $runStorage; |
| 23 | |
| 24 | public function __construct( |
| 25 | AutomationStatisticsStorage $statisticsStorage, |
| 26 | AutomationRunStorage $runStorage |
| 27 | ) { |
| 28 | $this->statisticsStorage = $statisticsStorage; |
| 29 | $this->runStorage = $runStorage; |
| 30 | } |
| 31 | |
| 32 | public function buildAutomation(Automation $automation, ?AutomationStatistics $statistics = null): array { |
| 33 | |
| 34 | return [ |
| 35 | 'id' => $automation->getId(), |
| 36 | 'name' => $automation->getName(), |
| 37 | 'status' => $automation->getStatus(), |
| 38 | 'created_at' => $automation->getCreatedAt()->format(DateTimeImmutable::W3C), |
| 39 | 'updated_at' => $automation->getUpdatedAt()->format(DateTimeImmutable::W3C), |
| 40 | 'activated_at' => $automation->getActivatedAt() ? $automation->getActivatedAt()->format(DateTimeImmutable::W3C) : null, |
| 41 | 'author' => [ |
| 42 | 'id' => $automation->getAuthor()->ID, |
| 43 | 'name' => $automation->getAuthor()->display_name, |
| 44 | ], |
| 45 | 'stats' => $statistics ? $statistics->toArray() : $this->statisticsStorage->getAutomationStats($automation->getId())->toArray(), |
| 46 | 'steps' => array_map(function (Step $step) { |
| 47 | return [ |
| 48 | 'id' => $step->getId(), |
| 49 | 'type' => $step->getType(), |
| 50 | 'key' => $step->getKey(), |
| 51 | 'args' => $step->getArgs(), |
| 52 | 'next_steps' => array_map(function (NextStep $nextStep) { |
| 53 | return $nextStep->toArray(); |
| 54 | }, $step->getNextSteps()), |
| 55 | 'filters' => $step->getFilters() ? $step->getFilters()->toArray() : null, |
| 56 | ]; |
| 57 | }, $automation->getSteps()), |
| 58 | 'meta' => (object)$automation->getAllMetas(), |
| 59 | ]; |
| 60 | } |
| 61 | |
| 62 | /** @param Automation[] $automations */ |
| 63 | public function buildAutomationList(array $automations): array { |
| 64 | $statistics = $this->statisticsStorage->getAutomationStatisticsForAutomations(...$automations); |
| 65 | $lastRuns = $this->runStorage->getLastAutomationRunsForAutomations(...$automations); |
| 66 | return array_map(function (Automation $automation) use ($statistics, $lastRuns) { |
| 67 | return $this->buildAutomationListItem( |
| 68 | $automation, |
| 69 | $statistics[$automation->getId()], |
| 70 | $lastRuns[$automation->getId()] ?? null |
| 71 | ); |
| 72 | }, $automations); |
| 73 | } |
| 74 | |
| 75 | private function buildAutomationListItem(Automation $automation, AutomationStatistics $statistics, ?AutomationRun $lastRun): array { |
| 76 | return [ |
| 77 | 'id' => $automation->getId(), |
| 78 | 'name' => $automation->getName(), |
| 79 | 'status' => $automation->getStatus(), |
| 80 | 'created_at' => $automation->getCreatedAt()->format(DateTimeImmutable::W3C), |
| 81 | 'updated_at' => $automation->getUpdatedAt()->format(DateTimeImmutable::W3C), |
| 82 | 'stats' => $statistics->toArray(), |
| 83 | 'activated_at' => $automation->getActivatedAt() ? $automation->getActivatedAt()->format(DateTimeImmutable::W3C) : null, |
| 84 | 'author' => [ |
| 85 | 'id' => $automation->getAuthor()->ID, |
| 86 | 'name' => $automation->getAuthor()->display_name, |
| 87 | ], |
| 88 | 'last_run' => $lastRun ? [ |
| 89 | 'id' => $lastRun->getId(), |
| 90 | 'status' => $lastRun->getStatus(), |
| 91 | 'created_at' => $lastRun->getCreatedAt()->format(DateTimeImmutable::W3C), |
| 92 | 'updated_at' => $lastRun->getUpdatedAt()->format(DateTimeImmutable::W3C), |
| 93 | ] : null, |
| 94 | ]; |
| 95 | } |
| 96 | } |
| 97 |