Nessuna descrizione

SubscribersCountsController.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php declare(strict_types = 1);
  2. namespace MailPoet\Subscribers;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Cache\TransientCache;
  5. use MailPoet\Entities\SegmentEntity;
  6. use MailPoet\InvalidStateException;
  7. use MailPoet\Segments\SegmentsRepository;
  8. use MailPoet\Segments\SegmentSubscribersRepository;
  9. class SubscribersCountsController {
  10. /** @var SegmentsRepository */
  11. private $segmentsRepository;
  12. /** @var SegmentSubscribersRepository */
  13. private $segmentSubscribersRepository;
  14. /** @var TransientCache */
  15. private $transientCache;
  16. public function __construct(
  17. SegmentsRepository $segmentsRepository,
  18. SegmentSubscribersRepository $segmentSubscribersRepository,
  19. TransientCache $transientCache
  20. ) {
  21. $this->segmentSubscribersRepository = $segmentSubscribersRepository;
  22. $this->transientCache = $transientCache;
  23. $this->segmentsRepository = $segmentsRepository;
  24. }
  25. public function getSubscribersWithoutSegmentStatisticsCount(): array {
  26. $result = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, 0)['item'] ?? null;
  27. if (!$result) {
  28. $result = $this->recalculateSubscribersWithoutSegmentStatisticsCache();
  29. }
  30. return $result;
  31. }
  32. public function getSegmentStatisticsCount(SegmentEntity $segment): array {
  33. $result = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, (int)$segment->getId())['item'] ?? null;
  34. if (!$result) {
  35. $result = $this->recalculateSegmentStatisticsCache($segment);
  36. }
  37. return $result;
  38. }
  39. public function getSegmentGlobalStatusStatisticsCount(SegmentEntity $segment): array {
  40. $result = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_GLOBAL_STATUS_STATISTICS_COUNT_KEY, (int)$segment->getId())['item'] ?? null;
  41. if (!$result) {
  42. $result = $this->recalculateSegmentGlobalStatusStatisticsCache($segment);
  43. }
  44. return $result;
  45. }
  46. public function getSegmentStatisticsCountById(int $segmentId): array {
  47. $result = $this->transientCache->getItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $segmentId)['item'] ?? null;
  48. if (!$result) {
  49. $segment = $this->segmentsRepository->findOneById($segmentId);
  50. if (!$segment) {
  51. throw new InvalidStateException();
  52. }
  53. $result = $this->recalculateSegmentStatisticsCache($segment);
  54. }
  55. return $result;
  56. }
  57. public function recalculateSegmentGlobalStatusStatisticsCache(SegmentEntity $segment): array {
  58. $result = $this->segmentSubscribersRepository->getSubscribersGlobalStatusStatisticsCount($segment);
  59. $this->transientCache->setItem(
  60. TransientCache::SUBSCRIBERS_GLOBAL_STATUS_STATISTICS_COUNT_KEY,
  61. $result,
  62. (int)$segment->getId()
  63. );
  64. return $result;
  65. }
  66. public function recalculateSegmentStatisticsCache(SegmentEntity $segment): array {
  67. $result = $this->segmentSubscribersRepository->getSubscribersStatisticsCount($segment);
  68. $this->transientCache->setItem(
  69. TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY,
  70. $result,
  71. (int)$segment->getId()
  72. );
  73. return $result;
  74. }
  75. public function recalculateSubscribersWithoutSegmentStatisticsCache(): array {
  76. $result = $this->segmentSubscribersRepository->getSubscribersWithoutSegmentStatisticsCount();
  77. $this->transientCache->setItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $result, 0);
  78. return $result;
  79. }
  80. public function removeRedundancyFromStatisticsCache() {
  81. $segments = $this->segmentsRepository->findAll();
  82. $segmentIds = array_map(function (SegmentEntity $segment): int {
  83. return (int)$segment->getId();
  84. }, $segments);
  85. foreach ($this->transientCache->getItems(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY) as $id => $item) {
  86. if (!in_array($id, $segmentIds)) {
  87. $this->transientCache->invalidateItem(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY, $id);
  88. }
  89. }
  90. foreach ($this->transientCache->getItems(TransientCache::SUBSCRIBERS_GLOBAL_STATUS_STATISTICS_COUNT_KEY) as $id => $item) {
  91. if (!in_array($id, $segmentIds)) {
  92. $this->transientCache->invalidateItem(TransientCache::SUBSCRIBERS_GLOBAL_STATUS_STATISTICS_COUNT_KEY, $id);
  93. }
  94. }
  95. }
  96. }