| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php declare(strict_types = 1);
- namespace MailPoet\Subscribers;
- if (!defined('ABSPATH')) exit;
- use MailPoet\Cache\TransientCache;
- use MailPoet\Entities\SegmentEntity;
- use MailPoet\InvalidStateException;
- use MailPoet\Segments\SegmentsRepository;
- use MailPoet\Segments\SegmentSubscribersRepository;
- class SubscribersCountsController {
- /** @var SegmentsRepository */
- private $segmentsRepository;
- /** @var SegmentSubscribersRepository */
- private $segmentSubscribersRepository;
- /** @var TransientCache */
- private $transientCache;
- public function __construct(
- SegmentsRepository $segmentsRepository,
- SegmentSubscribersRepository $segmentSubscribersRepository,
- TransientCache $transientCache
- ) {
- $this->segmentSubscribersRepository = $segmentSubscribersRepository;
- $this->transientCache = $transientCache;
- $this->segmentsRepository = $segmentsRepository;
- }
- public function getSubscribersWithoutSegmentStatisticsCount(): array {
- $result = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, 0)['item'] ?? null;
- 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())['item'] ?? null;
- 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())['item'] ?? null;
- if (!$result) {
- $result = $this->recalculateSegmentGlobalStatusStatisticsCache($segment);
- }
- return $result;
- }
- public function getSegmentStatisticsCountById(int $segmentId): array {
- $result = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $segmentId)['item'] ?? null;
- 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;
- }
- public function removeRedundancyFromStatisticsCache() {
- $segments = $this->segmentsRepository->findAll();
- $segmentIds = array_map(function (SegmentEntity $segment): int {
- return (int)$segment->getId();
- }, $segments);
- foreach ($this->transientCache->getItems(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY) as $id => $item) {
- if (!in_array($id, $segmentIds)) {
- $this->transientCache->invalidateItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $id);
- }
- }
- foreach ($this->transientCache->getItems(TransientCache::SUBSCRIBERS_GLOBAL_STATUS_STATISTICS_COUNT_KEY) as $id => $item) {
- if (!in_array($id, $segmentIds)) {
- $this->transientCache->invalidateItem(TransientCache::SUBSCRIBERS_GLOBAL_STATUS_STATISTICS_COUNT_KEY, $id);
- }
- }
- }
- }
|