Clicks.php
4 days ago
Opens.php
4 days ago
PageViewCookie.php
2 months ago
SubscriberActivityTracker.php
4 days ago
SubscriberCookie.php
2 months ago
SubscriberHandler.php
1 year ago
Unsubscribes.php
2 months ago
WooCommercePurchases.php
2 years ago
index.php
3 years ago
Unsubscribes.php
77 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Statistics\Track; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\NewsletterEntity; |
| 9 | use MailPoet\Entities\SendingQueueEntity; |
| 10 | use MailPoet\Entities\StatisticsUnsubscribeEntity; |
| 11 | use MailPoet\Entities\SubscriberEntity; |
| 12 | use MailPoet\Newsletter\Sending\SendingQueuesRepository; |
| 13 | use MailPoet\Statistics\StatisticsUnsubscribesRepository; |
| 14 | use MailPoet\Subscribers\SubscribersRepository; |
| 15 | |
| 16 | class Unsubscribes { |
| 17 | /** @var SendingQueuesRepository */ |
| 18 | private $sendingQueuesRepository; |
| 19 | |
| 20 | /** @var StatisticsUnsubscribesRepository */ |
| 21 | private $statisticsUnsubscribesRepository; |
| 22 | |
| 23 | /** |
| 24 | * @var SubscribersRepository |
| 25 | */ |
| 26 | private $subscribersRepository; |
| 27 | |
| 28 | public function __construct( |
| 29 | SendingQueuesRepository $sendingQueuesRepository, |
| 30 | StatisticsUnsubscribesRepository $statisticsUnsubscribesRepository, |
| 31 | SubscribersRepository $subscribersRepository |
| 32 | ) { |
| 33 | $this->sendingQueuesRepository = $sendingQueuesRepository; |
| 34 | $this->statisticsUnsubscribesRepository = $statisticsUnsubscribesRepository; |
| 35 | $this->subscribersRepository = $subscribersRepository; |
| 36 | } |
| 37 | |
| 38 | public function track( |
| 39 | int $subscriberId, |
| 40 | string $source, |
| 41 | ?int $queueId = null, |
| 42 | ?string $meta = null, |
| 43 | string $method = StatisticsUnsubscribeEntity::METHOD_UNKNOWN |
| 44 | ): ?StatisticsUnsubscribeEntity { |
| 45 | $queue = null; |
| 46 | $statistics = null; |
| 47 | if ($queueId) { |
| 48 | $queue = $this->sendingQueuesRepository->findOneById($queueId); |
| 49 | } |
| 50 | $subscriber = $this->subscribersRepository->findOneById($subscriberId); |
| 51 | if (!$subscriber instanceof SubscriberEntity) { |
| 52 | return null; |
| 53 | } |
| 54 | if (($queue instanceof SendingQueueEntity)) { |
| 55 | $newsletter = $queue->getNewsletter(); |
| 56 | if ($newsletter instanceof NewsletterEntity) { |
| 57 | $statistics = $this->statisticsUnsubscribesRepository->findOneBySubscriberAndQueue($subscriber, $queue, $newsletter); |
| 58 | if (!$statistics) { |
| 59 | $statistics = new StatisticsUnsubscribeEntity($newsletter, $queue, $subscriber); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if ($statistics === null) { |
| 65 | $statistics = new StatisticsUnsubscribeEntity(null, null, $subscriber); |
| 66 | } |
| 67 | if ($meta !== null) { |
| 68 | $statistics->setMeta($meta); |
| 69 | } |
| 70 | $statistics->setSource($source); |
| 71 | $statistics->setMethod($method); |
| 72 | $this->statisticsUnsubscribesRepository->persist($statistics); |
| 73 | $this->statisticsUnsubscribesRepository->flush(); |
| 74 | return $statistics; |
| 75 | } |
| 76 | } |
| 77 |