Refactor Statistics Entities to use association
[MAILPOET-3069]
This commit is contained in:
@ -102,7 +102,7 @@ class SubscribersResponseBuilder {
|
||||
|
||||
private function buildUnsubscribes(SubscriberEntity $subscriberEntity): array {
|
||||
$unsubscribes = $this->statisticsUnsubscribesRepository->findBy([
|
||||
'subscriberId' => $subscriberEntity->getId(),
|
||||
'subscriber' => $subscriberEntity,
|
||||
], [
|
||||
'createdAt' => 'desc',
|
||||
]);
|
||||
|
@ -33,10 +33,11 @@ class StatisticsClickEntity {
|
||||
private $queue;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="integer")
|
||||
* @var int
|
||||
* @ORM\ManyToOne(targetEntity="MailPoet\Entities\SubscriberEntity")
|
||||
* @ORM\JoinColumn(name="subscriber_id", referencedColumnName="id")
|
||||
* @var SubscriberEntity|null
|
||||
*/
|
||||
private $subscriberId;
|
||||
private $subscriber;
|
||||
|
||||
/**
|
||||
* @ORM\ManyToOne(targetEntity="MailPoet\Entities\NewsletterLinkEntity", inversedBy="clicks")
|
||||
@ -53,13 +54,13 @@ class StatisticsClickEntity {
|
||||
public function __construct(
|
||||
NewsletterEntity $newsletter,
|
||||
SendingQueueEntity $queue,
|
||||
int $subscriberId,
|
||||
SubscriberEntity $subscriber,
|
||||
NewsletterLinkEntity $link,
|
||||
int $count
|
||||
) {
|
||||
$this->newsletter = $newsletter;
|
||||
$this->queue = $queue;
|
||||
$this->subscriberId = $subscriberId;
|
||||
$this->subscriber = $subscriber;
|
||||
$this->link = $link;
|
||||
$this->count = $count;
|
||||
}
|
||||
@ -103,10 +104,10 @@ class StatisticsClickEntity {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $subscriberId
|
||||
* @param SubscriberEntity|null $subscriber
|
||||
*/
|
||||
public function setSubscriberId($subscriberId) {
|
||||
$this->subscriberId = $subscriberId;
|
||||
public function setSubscriber($subscriber) {
|
||||
$this->subscriber = $subscriber;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -31,19 +31,20 @@ class StatisticsOpenEntity {
|
||||
private $queue;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="integer")
|
||||
* @var int
|
||||
* @ORM\ManyToOne(targetEntity="MailPoet\Entities\SubscriberEntity")
|
||||
* @ORM\JoinColumn(name="subscriber_id", referencedColumnName="id")
|
||||
* @var SubscriberEntity|null
|
||||
*/
|
||||
private $subscriberId;
|
||||
private $subscriber;
|
||||
|
||||
public function __construct(
|
||||
NewsletterEntity $newsletter,
|
||||
SendingQueueEntity $queue,
|
||||
int $subscriberId
|
||||
SubscriberEntity $subscriber
|
||||
) {
|
||||
$this->newsletter = $newsletter;
|
||||
$this->queue = $queue;
|
||||
$this->subscriberId = $subscriberId;
|
||||
$this->subscriber = $subscriber;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -77,9 +78,9 @@ class StatisticsOpenEntity {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $subscriberId
|
||||
* @param SubscriberEntity|null $subscriber
|
||||
*/
|
||||
public function setSubscriberId($subscriberId) {
|
||||
$this->subscriberId = $subscriberId;
|
||||
public function setSubscriber($subscriber) {
|
||||
$this->subscriber = $subscriber;
|
||||
}
|
||||
}
|
||||
|
@ -35,10 +35,11 @@ class StatisticsUnsubscribeEntity {
|
||||
private $queue;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="integer")
|
||||
* @var int
|
||||
* @ORM\ManyToOne(targetEntity="MailPoet\Entities\SubscriberEntity")
|
||||
* @ORM\JoinColumn(name="subscriber_id", referencedColumnName="id")
|
||||
* @var SubscriberEntity|null
|
||||
*/
|
||||
private $subscriberId;
|
||||
private $subscriber;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="string")
|
||||
@ -55,11 +56,11 @@ class StatisticsUnsubscribeEntity {
|
||||
public function __construct(
|
||||
NewsletterEntity $newsletter = null,
|
||||
SendingQueueEntity $queue = null,
|
||||
int $subscriberId
|
||||
SubscriberEntity $subscriber
|
||||
) {
|
||||
$this->newsletter = $newsletter;
|
||||
$this->queue = $queue;
|
||||
$this->subscriberId = $subscriberId;
|
||||
$this->subscriber = $subscriber;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -132,7 +132,7 @@ class NewsletterStatisticsRepository extends Repository {
|
||||
|
||||
private function getStatisticCounts(string $statisticsEntityName, array $newsletters): array {
|
||||
$results = $this->entityManager->createQueryBuilder()
|
||||
->select('IDENTITY(stats.newsletter) AS id, COUNT(DISTINCT stats.subscriberId) as cnt')
|
||||
->select('IDENTITY(stats.newsletter) AS id, COUNT(DISTINCT stats.subscriber) as cnt')
|
||||
->from($statisticsEntityName, 'stats')
|
||||
->where('stats.newsletter IN (:newsletters)')
|
||||
->groupBy('stats.newsletter')
|
||||
|
@ -5,8 +5,10 @@ namespace MailPoet\Statistics\Track;
|
||||
use MailPoet\Entities\NewsletterEntity;
|
||||
use MailPoet\Entities\SendingQueueEntity;
|
||||
use MailPoet\Entities\StatisticsUnsubscribeEntity;
|
||||
use MailPoet\Entities\SubscriberEntity;
|
||||
use MailPoet\Newsletter\Sending\SendingQueuesRepository;
|
||||
use MailPoet\Statistics\StatisticsUnsubscribesRepository;
|
||||
use MailPoet\Subscribers\SubscribersRepository;
|
||||
|
||||
class Unsubscribes {
|
||||
/** @var SendingQueuesRepository */
|
||||
@ -15,12 +17,19 @@ class Unsubscribes {
|
||||
/** @var StatisticsUnsubscribesRepository */
|
||||
private $statisticsUnsubscribesRepository;
|
||||
|
||||
/**
|
||||
* @var SubscribersRepository
|
||||
*/
|
||||
private $subscribersRepository;
|
||||
|
||||
public function __construct(
|
||||
SendingQueuesRepository $sendingQueuesRepository,
|
||||
StatisticsUnsubscribesRepository $statisticsUnsubscribesRepository
|
||||
StatisticsUnsubscribesRepository $statisticsUnsubscribesRepository,
|
||||
SubscribersRepository $subscribersRepository
|
||||
) {
|
||||
$this->sendingQueuesRepository = $sendingQueuesRepository;
|
||||
$this->statisticsUnsubscribesRepository = $statisticsUnsubscribesRepository;
|
||||
$this->subscribersRepository = $subscribersRepository;
|
||||
}
|
||||
|
||||
public function track(int $subscriberId, string $source, int $queueId = null, string $meta = null) {
|
||||
@ -29,24 +38,28 @@ class Unsubscribes {
|
||||
if ($queueId) {
|
||||
$queue = $this->sendingQueuesRepository->findOneById($queueId);
|
||||
}
|
||||
if ($queue instanceof SendingQueueEntity) {
|
||||
$subscriber = $this->subscribersRepository->findOneById($subscriberId);
|
||||
if (!$subscriber instanceof SubscriberEntity) {
|
||||
return;
|
||||
}
|
||||
if (($queue instanceof SendingQueueEntity)) {
|
||||
$newsletter = $queue->getNewsletter();
|
||||
if ($newsletter instanceof NewsletterEntity) {
|
||||
$statistics = $this->statisticsUnsubscribesRepository->findOneBy(
|
||||
[
|
||||
'queue' => $queue,
|
||||
'newsletter' => $newsletter,
|
||||
'subscriberId' => $subscriberId,
|
||||
'subscriber' => $subscriber,
|
||||
]
|
||||
);
|
||||
if (!$statistics) {
|
||||
$statistics = new StatisticsUnsubscribeEntity($newsletter, $queue, $subscriberId);
|
||||
$statistics = new StatisticsUnsubscribeEntity($newsletter, $queue, $subscriber);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($statistics === null) {
|
||||
$statistics = new StatisticsUnsubscribeEntity(null, null, $subscriberId);
|
||||
$statistics = new StatisticsUnsubscribeEntity(null, null, $subscriber);
|
||||
}
|
||||
if ($meta !== null) {
|
||||
$statistics->setMeta($meta);
|
||||
|
@ -7,6 +7,7 @@ use MailPoet\Entities\NewsletterLinkEntity;
|
||||
use MailPoet\Entities\ScheduledTaskEntity;
|
||||
use MailPoet\Entities\SendingQueueEntity;
|
||||
use MailPoet\Entities\StatisticsClickEntity;
|
||||
use MailPoet\Entities\SubscriberEntity;
|
||||
use MailPoet\Tasks\Sending as SendingTask;
|
||||
|
||||
class NewsletterLinkRepositoryTest extends \MailPoetTest {
|
||||
@ -33,10 +34,14 @@ class NewsletterLinkRepositoryTest extends \MailPoetTest {
|
||||
$this->entityManager->persist($link1);
|
||||
$this->entityManager->persist($link2);
|
||||
|
||||
$subscriber = new SubscriberEntity();
|
||||
$subscriber->setEmail("sub{$newsletter->getId()}@mailpoet.com");
|
||||
$subscriber->setStatus(SubscriberEntity::STATUS_SUBSCRIBED);
|
||||
$this->entityManager->persist($subscriber);
|
||||
|
||||
$click1 = new StatisticsClickEntity($newsletter, $queue, (int)1, $link1, 1);
|
||||
$click2 = new StatisticsClickEntity($newsletter, $queue, (int)1, $link1, 1);
|
||||
$click3 = new StatisticsClickEntity($newsletter, $queue, (int)1, $link2, 1);
|
||||
$click1 = new StatisticsClickEntity($newsletter, $queue, $subscriber, $link1, 1);
|
||||
$click2 = new StatisticsClickEntity($newsletter, $queue, $subscriber, $link1, 1);
|
||||
$click3 = new StatisticsClickEntity($newsletter, $queue, $subscriber, $link2, 1);
|
||||
|
||||
$this->entityManager->persist($click1);
|
||||
$this->entityManager->persist($click2);
|
||||
|
@ -330,7 +330,7 @@ class NewsletterRepositoryTest extends \MailPoetTest {
|
||||
}
|
||||
|
||||
private function createOpenStatistics(NewsletterEntity $newsletter, SendingQueueEntity $queue, SubscriberEntity $subscriber): StatisticsOpenEntity {
|
||||
$statistics = new StatisticsOpenEntity($newsletter, $queue, (int)$subscriber->getId());
|
||||
$statistics = new StatisticsOpenEntity($newsletter, $queue, $subscriber);
|
||||
$this->entityManager->persist($statistics);
|
||||
$this->entityManager->flush();
|
||||
return $statistics;
|
||||
@ -342,7 +342,7 @@ class NewsletterRepositoryTest extends \MailPoetTest {
|
||||
SubscriberEntity $subscriber,
|
||||
NewsletterLinkEntity $link
|
||||
): StatisticsClickEntity {
|
||||
$statistics = new StatisticsClickEntity($newsletter, $queue, (int)$subscriber->getId(), $link, 1);
|
||||
$statistics = new StatisticsClickEntity($newsletter, $queue, $subscriber, $link, 1);
|
||||
$this->entityManager->persist($statistics);
|
||||
$this->entityManager->flush();
|
||||
return $statistics;
|
||||
|
Reference in New Issue
Block a user