ConfirmationEmailTemplate
1 month ago
ImportExport
2 weeks ago
RestApi
3 days ago
Statistics
1 month ago
BulkActionController.php
3 days ago
BulkActionException.php
1 month ago
BulkConfirmationEmailResender.php
2 months ago
ConfirmationEmailCustomizer.php
2 months ago
ConfirmationEmailMailer.php
2 months ago
ConfirmationEmailResolver.php
2 months ago
EngagementDataBackfiller.php
2 months ago
InactiveSubscribersController.php
5 days ago
LinkTokens.php
2 months ago
NewSubscriberNotificationMailer.php
2 months ago
RequiredCustomFieldValidator.php
2 months ago
SegmentsCountRecalculator.php
2 weeks ago
Source.php
2 months ago
SubscriberActions.php
2 months ago
SubscriberCustomFieldRepository.php
3 years ago
SubscriberIPsRepository.php
2 years ago
SubscriberLimitNotificationEvaluator.php
2 months ago
SubscriberLimitNotificationMailer.php
2 months ago
SubscriberLimitNotificationScheduler.php
2 months ago
SubscriberListingRepository.php
2 weeks ago
SubscriberPersonalDataEraser.php
2 months ago
SubscriberSaveController.php
3 days ago
SubscriberSegmentRepository.php
2 weeks ago
SubscriberSubscribeController.php
2 weeks ago
SubscriberTagRepository.php
4 years ago
SubscribersCountsController.php
2 weeks ago
SubscribersEmailCountsController.php
2 weeks ago
SubscribersRepository.php
3 days ago
TrackingConsentController.php
5 days ago
index.php
3 years ago
SubscribersCountsController.php
177 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Subscribers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Cache\TransientCache; |
| 9 | use MailPoet\Entities\SegmentEntity; |
| 10 | use MailPoet\Entities\SubscriberEntity; |
| 11 | use MailPoet\InvalidStateException; |
| 12 | use MailPoet\Segments\SegmentsRepository; |
| 13 | use MailPoet\Segments\SegmentSubscribersRepository; |
| 14 | use MailPoet\Tags\TagRepository; |
| 15 | use MailPoet\Util\License\Features\Subscribers as SubscribersFeature; |
| 16 | use MailPoetVendor\Carbon\Carbon; |
| 17 | |
| 18 | class SubscribersCountsController { |
| 19 | /** @var SegmentsRepository */ |
| 20 | private $segmentsRepository; |
| 21 | |
| 22 | /** @var SegmentSubscribersRepository */ |
| 23 | private $segmentSubscribersRepository; |
| 24 | |
| 25 | /** @var SubscribersRepository */ |
| 26 | private $subscribersRepository; |
| 27 | |
| 28 | /** @var TagRepository */ |
| 29 | private $tagRepository; |
| 30 | |
| 31 | /** @var TransientCache */ |
| 32 | private $transientCache; |
| 33 | |
| 34 | /** @var SubscribersFeature */ |
| 35 | private $subscribersFeature; |
| 36 | |
| 37 | public function __construct( |
| 38 | SegmentsRepository $segmentsRepository, |
| 39 | SegmentSubscribersRepository $segmentSubscribersRepository, |
| 40 | SubscribersRepository $subscribersRepository, |
| 41 | TagRepository $subscriberTagRepository, |
| 42 | TransientCache $transientCache, |
| 43 | SubscribersFeature $subscribersFeature |
| 44 | ) { |
| 45 | |
| 46 | $this->segmentSubscribersRepository = $segmentSubscribersRepository; |
| 47 | $this->transientCache = $transientCache; |
| 48 | $this->segmentsRepository = $segmentsRepository; |
| 49 | $this->subscribersRepository = $subscribersRepository; |
| 50 | $this->tagRepository = $subscriberTagRepository; |
| 51 | $this->subscribersFeature = $subscribersFeature; |
| 52 | } |
| 53 | |
| 54 | public function getSubscribersWithoutSegmentStatisticsCount(): array { |
| 55 | $result = $this->getCacheItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, 0)['item'] ?? null; |
| 56 | if (!$result) { |
| 57 | $result = $this->recalculateSubscribersWithoutSegmentStatisticsCache(); |
| 58 | } |
| 59 | return $result; |
| 60 | } |
| 61 | |
| 62 | public function getSegmentStatisticsCount(SegmentEntity $segment): array { |
| 63 | $result = $this->getCacheItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, (int)$segment->getId())['item'] ?? null; |
| 64 | if (!$result) { |
| 65 | $result = $this->recalculateSegmentStatisticsCache($segment); |
| 66 | } |
| 67 | return $result; |
| 68 | } |
| 69 | |
| 70 | public function getSegmentStatisticsCountById(int $segmentId): array { |
| 71 | $result = $this->getCacheItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $segmentId)['item'] ?? null; |
| 72 | if (!$result) { |
| 73 | $segment = $this->segmentsRepository->findOneById($segmentId); |
| 74 | if (!$segment) { |
| 75 | throw new InvalidStateException("Segment with ID $segmentId not found"); |
| 76 | } |
| 77 | $result = $this->recalculateSegmentStatisticsCache($segment); |
| 78 | } |
| 79 | return $result; |
| 80 | } |
| 81 | |
| 82 | public function getGlobalStatusStatisticsCount(): array { |
| 83 | $result = $this->getCacheItem(TransientCache::SUBSCRIBERS_GLOBAL_STATISTICS_COUNT_KEY, 0)['item'] ?? null; |
| 84 | if (!$result) { |
| 85 | $result = $this->recalculateGlobalStatusStatisticsCache(); |
| 86 | } |
| 87 | return $result; |
| 88 | } |
| 89 | |
| 90 | public function recalculateGlobalStatusStatisticsCache(): array { |
| 91 | $result = $this->subscribersRepository->getStatusStatisticsCount(); |
| 92 | $this->setCacheItem(TransientCache::SUBSCRIBERS_GLOBAL_STATISTICS_COUNT_KEY, $result, 0); |
| 93 | return $result; |
| 94 | } |
| 95 | |
| 96 | public function getHomepageStatistics(): array { |
| 97 | $result = $this->getCacheItem(TransientCache::SUBSCRIBERS_HOMEPAGE_STATISTICS_COUNT_KEY, 0)['item'] ?? []; |
| 98 | if (!$result) { |
| 99 | $result = $this->recalculateHomepageStatisticsCache(); |
| 100 | } |
| 101 | return $result; |
| 102 | } |
| 103 | |
| 104 | public function recalculateSegmentStatisticsCache(SegmentEntity $segment): array { |
| 105 | $result = $this->segmentSubscribersRepository->getSubscribersStatisticsCount($segment); |
| 106 | $this->setCacheItem( |
| 107 | TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, |
| 108 | $result, |
| 109 | (int)$segment->getId() |
| 110 | ); |
| 111 | return $result; |
| 112 | } |
| 113 | |
| 114 | public function recalculateSubscribersWithoutSegmentStatisticsCache(): array { |
| 115 | $result = $this->segmentSubscribersRepository->getSubscribersWithoutSegmentStatisticsCount(); |
| 116 | $this->setCacheItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $result, 0); |
| 117 | return $result; |
| 118 | } |
| 119 | |
| 120 | public function recalculateHomepageStatisticsCache(): array { |
| 121 | $thirtyDaysAgo = Carbon::now()->millisecond(0)->subDays(30); |
| 122 | $result = []; |
| 123 | $result['listsDataSubscribed'] = $this->subscribersRepository->getListLevelCountsOfSubscribedAfter($thirtyDaysAgo); |
| 124 | $result['listsDataUnsubscribed'] = $this->subscribersRepository->getListLevelCountsOfUnsubscribedAfter($thirtyDaysAgo); |
| 125 | $result['subscribedCount'] = $this->subscribersRepository->getCountOfLastSubscribedAfter($thirtyDaysAgo); |
| 126 | $result['unsubscribedCount'] = $this->subscribersRepository->getCountOfUnsubscribedAfter($thirtyDaysAgo); |
| 127 | $result['subscribedSubscribersCount'] = $this->subscribersRepository->getCountOfSubscribersForStates([SubscriberEntity::STATUS_SUBSCRIBED]); |
| 128 | $this->setCacheItem( |
| 129 | TransientCache::SUBSCRIBERS_HOMEPAGE_STATISTICS_COUNT_KEY, |
| 130 | $result, |
| 131 | 0 |
| 132 | ); |
| 133 | return $result; |
| 134 | } |
| 135 | |
| 136 | public function removeRedundancyFromStatisticsCache() { |
| 137 | $segments = $this->segmentsRepository->findAll(); |
| 138 | $segmentIds = array_map(function (SegmentEntity $segment): int { |
| 139 | return (int)$segment->getId(); |
| 140 | }, $segments); |
| 141 | foreach ($this->transientCache->getItems(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY) as $id => $item) { |
| 142 | // id 0 is the "subscribers without a list" entry, not an orphaned segment — |
| 143 | // keep it, otherwise every cache warm deletes it right after building it and |
| 144 | // the (expensive) count is recomputed live on the next page load. |
| 145 | if ($id === 0) { |
| 146 | continue; |
| 147 | } |
| 148 | if (!in_array($id, $segmentIds)) { |
| 149 | $this->transientCache->invalidateItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $id); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Use cache only if subscribers count is above minimum |
| 156 | */ |
| 157 | private function getCacheItem(string $key, int $id): ?array { |
| 158 | if ($this->subscribersFeature->isSubscribersCountEnoughForCache()) { |
| 159 | return $this->transientCache->getItem($key, $id); |
| 160 | } |
| 161 | return []; |
| 162 | } |
| 163 | |
| 164 | private function setCacheItem(string $key, array $item, int $id): void { |
| 165 | if ($this->subscribersFeature->isSubscribersCountEnoughForCache()) { |
| 166 | $this->transientCache->setItem($key, $item, $id); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @return array<int, array{id: int, name: string, subscribersCount: int}> |
| 172 | */ |
| 173 | public function getTagsStatisticsCount(?string $status, bool $isDeleted): array { |
| 174 | return $this->tagRepository->getSubscriberStatisticsCount($status, $isDeleted); |
| 175 | } |
| 176 | } |
| 177 |