mailpoet
/
lib
/
Automation
/
Integrations
/
MailPoet
/
Analytics
/
Controller
/
AutomationTimeSpanController.php
AutomationTimeSpanController.php
2 months ago
OverviewStatisticsController.php
2 months ago
StepStatisticController.php
2 months ago
index.php
3 years ago
AutomationTimeSpanController.php
111 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Analytics\Controller; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 10 | use MailPoet\Automation\Integrations\MailPoet\Actions\SendEmailAction; |
| 11 | use MailPoet\Entities\NewsletterEntity; |
| 12 | use MailPoet\Newsletter\NewslettersRepository; |
| 13 | |
| 14 | class AutomationTimeSpanController { |
| 15 | |
| 16 | /** @var AutomationStorage */ |
| 17 | private $automationStorage; |
| 18 | |
| 19 | /** @var NewslettersRepository */ |
| 20 | private $newslettersRepository; |
| 21 | |
| 22 | public function __construct( |
| 23 | AutomationStorage $automationStorage, |
| 24 | NewslettersRepository $newslettersRepository |
| 25 | ) { |
| 26 | $this->automationStorage = $automationStorage; |
| 27 | $this->newslettersRepository = $newslettersRepository; |
| 28 | } |
| 29 | |
| 30 | public function getAutomationsInTimespan(Automation $automation, \DateTimeImmutable $after, \DateTimeImmutable $before, ?int $versionId = null): array { |
| 31 | if ($versionId !== null) { |
| 32 | return array_values(array_filter( |
| 33 | $this->automationStorage->getAutomationWithDifferentVersions([$versionId]), |
| 34 | function (Automation $versionedAutomation) use ($automation): bool { |
| 35 | return $versionedAutomation->getId() === $automation->getId(); |
| 36 | } |
| 37 | )); |
| 38 | } |
| 39 | |
| 40 | $automationVersions = $this->automationStorage->getAutomationVersionDates($automation->getId()); |
| 41 | usort( |
| 42 | $automationVersions, |
| 43 | function (array $a, array $b) { |
| 44 | return $a['created_at'] <=> $b['created_at']; |
| 45 | } |
| 46 | ); |
| 47 | |
| 48 | // Find all versions, which could have been active in the given time span |
| 49 | $versionIds = []; |
| 50 | foreach ($automationVersions as $automationVersion) { |
| 51 | if ($automationVersion['created_at'] > $before) { |
| 52 | // We are past the time span |
| 53 | break; |
| 54 | } |
| 55 | if (!$versionIds || $automationVersion['created_at'] <= $after) { |
| 56 | // This is the first version in the time span |
| 57 | $versionIds = [(int)$automationVersion['id']]; |
| 58 | continue; |
| 59 | } |
| 60 | $versionIds[] = (int)$automationVersion['id']; |
| 61 | } |
| 62 | |
| 63 | return count($versionIds) > 0 ? $this->automationStorage->getAutomationWithDifferentVersions($versionIds) : []; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param Automation $automation |
| 68 | * @param \DateTimeImmutable $after |
| 69 | * @param \DateTimeImmutable $before |
| 70 | * @return NewsletterEntity[] |
| 71 | */ |
| 72 | public function getAutomationEmailsInTimeSpan(Automation $automation, \DateTimeImmutable $after, \DateTimeImmutable $before, ?int $versionId = null): array { |
| 73 | $automations = $this->getAutomationsInTimespan($automation, $after, $before, $versionId); |
| 74 | return count($automations) > 0 ? $this->getEmailsFromAutomations($automations) : []; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param Automation[] $automations |
| 79 | * @return NewsletterEntity[] |
| 80 | */ |
| 81 | public function getEmailsFromAutomations(array $automations): array { |
| 82 | $emailSteps = []; |
| 83 | foreach ($automations as $automation) { |
| 84 | $emailSteps = array_merge( |
| 85 | $emailSteps, |
| 86 | array_values( |
| 87 | array_filter( |
| 88 | $automation->getSteps(), |
| 89 | function($step) { |
| 90 | return $step->getKey() === SendEmailAction::KEY; |
| 91 | } |
| 92 | ) |
| 93 | ) |
| 94 | ); |
| 95 | } |
| 96 | $emailIds = array_unique( |
| 97 | array_filter( |
| 98 | array_map( |
| 99 | function($step) { |
| 100 | $args = $step->getArgs(); |
| 101 | return isset($args['email_id']) ? absint($args['email_id']) : null; |
| 102 | }, |
| 103 | $emailSteps |
| 104 | ) |
| 105 | ) |
| 106 | ); |
| 107 | |
| 108 | return $this->newslettersRepository->findByIds($emailIds); |
| 109 | } |
| 110 | } |
| 111 |