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