diff --git a/lib/DI/ContainerConfigurator.php b/lib/DI/ContainerConfigurator.php index cbc8a8cb05..97eae8f109 100644 --- a/lib/DI/ContainerConfigurator.php +++ b/lib/DI/ContainerConfigurator.php @@ -264,6 +264,7 @@ class ContainerConfigurator implements IContainerConfigurator { $container->autowire(\MailPoet\Subscribers\ImportExport\ImportExportRepository::class)->setPublic(true); $container->autowire(\MailPoet\Subscribers\ImportExport\PersonalDataExporters\NewslettersExporter::class)->setPublic(true); $container->autowire(\MailPoet\Subscribers\Statistics\SubscriberStatisticsRepository::class); + $container->autowire(\MailPoet\Subscribers\SubscribersCountsController::class)->setPublic(true); // Segments $container->autowire(\MailPoet\Segments\WooCommerce::class)->setPublic(true); $container->autowire(\MailPoet\Segments\WP::class)->setPublic(true); diff --git a/lib/Subscribers/SubscribersCountsController.php b/lib/Subscribers/SubscribersCountsController.php new file mode 100644 index 0000000000..dc76bb1583 --- /dev/null +++ b/lib/Subscribers/SubscribersCountsController.php @@ -0,0 +1,93 @@ +segmentSubscribersRepository = $segmentSubscribersRepository; + $this->transientCache = $transientCache; + $this->segmentsRepository = $segmentsRepository; + } + + public function getSubscribersWithoutSegmentStatisticsCount(): array { + $result = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, 0); + if (!$result) { + $result = $this->recalculateSubscribersWithoutSegmentStatisticsCache(); + } + return $result; + } + + public function getSegmentStatisticsCount(SegmentEntity $segment): array { + $result = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, (int)$segment->getId()); + if (!$result) { + $result = $this->recalculateSegmentStatisticsCache($segment); + } + return $result; + } + + public function getSegmentGlobalStatusStatisticsCount(SegmentEntity $segment): array { + $result = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_GLOBAL_STATUS_STATISTICS_COUNT_KEY, (int)$segment->getId()); + if (!$result) { + $result = $this->recalculateSegmentGlobalStatusStatisticsCache($segment); + } + return $result; + } + + public function getSegmentStatisticsCountById(int $segmentId): array { + $result = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $segmentId); + if (!$result) { + $segment = $this->segmentsRepository->findOneById($segmentId); + if (!$segment) { + throw new InvalidStateException(); + } + $result = $this->recalculateSegmentStatisticsCache($segment); + } + return $result; + } + + public function recalculateSegmentGlobalStatusStatisticsCache(SegmentEntity $segment): array { + $result = $this->segmentSubscribersRepository->getSubscribersGlobalStatusStatisticsCount($segment); + $this->transientCache->setItem( + TransientCache::SUBSCRIBERS_GLOBAL_STATUS_STATISTICS_COUNT_KEY, + $result, + (int)$segment->getId() + ); + return $result; + } + + public function recalculateSegmentStatisticsCache(SegmentEntity $segment): array { + $result = $this->segmentSubscribersRepository->getSubscribersStatisticsCount($segment); + $this->transientCache->setItem( + TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, + $result, + (int)$segment->getId() + ); + return $result; + } + + public function recalculateSubscribersWithoutSegmentStatisticsCache(): array { + $result = $this->segmentSubscribersRepository->getSubscribersWithoutSegmentStatisticsCount(); + $this->transientCache->setItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $result, 0); + return $result; + } +}