Add unsubscriptions to the response
[MAILPOET-2653]
This commit is contained in:
@ -4,7 +4,9 @@ namespace MailPoet\API\JSON\ResponseBuilders;
|
||||
|
||||
use MailPoet\Entities\SegmentEntity;
|
||||
use MailPoet\Entities\SubscriberCustomFieldEntity;
|
||||
use MailPoet\Entities\NewsletterEntity;
|
||||
use MailPoet\Entities\SubscriberEntity;
|
||||
use MailPoet\Statistics\StatisticsUnsubscribesRepository;
|
||||
use MailPoet\Subscribers\SubscriberSegmentRepository;
|
||||
|
||||
class SubscribersResponseBuilder {
|
||||
@ -12,8 +14,15 @@ class SubscribersResponseBuilder {
|
||||
/** @var SubscriberSegmentRepository */
|
||||
private $subscriberSegmentRepository;
|
||||
|
||||
public function __construct(SubscriberSegmentRepository $subscriberSegmentRepository) {
|
||||
/** @var StatisticsUnsubscribesRepository */
|
||||
private $statisticsUnsubscribesRepository;
|
||||
|
||||
public function __construct(
|
||||
SubscriberSegmentRepository $subscriberSegmentRepository,
|
||||
StatisticsUnsubscribesRepository $statisticsUnsubscribesRepository
|
||||
) {
|
||||
$this->subscriberSegmentRepository = $subscriberSegmentRepository;
|
||||
$this->statisticsUnsubscribesRepository = $statisticsUnsubscribesRepository;
|
||||
}
|
||||
|
||||
public function build(SubscriberEntity $subscriberEntity): array {
|
||||
@ -22,7 +31,7 @@ class SubscribersResponseBuilder {
|
||||
'wp_user_id' => $subscriberEntity->getWpUserId(),
|
||||
'is_woocommerce_user' => $subscriberEntity->getIsWoocommerceUser(),
|
||||
'subscriptions' => $this->buildSubscriptions($subscriberEntity),
|
||||
'unsubscribes' => [],// TODO
|
||||
'unsubscribes' => $this->buildUnsubscribes($subscriberEntity),
|
||||
// TODO custom fields
|
||||
'status' => $subscriberEntity->getStatus(),
|
||||
'last_name' => $subscriberEntity->getLastName(),
|
||||
@ -35,7 +44,7 @@ class SubscribersResponseBuilder {
|
||||
|
||||
private function buildSubscriptions(SubscriberEntity $subscriberEntity): array {
|
||||
$result = [];
|
||||
$subscriptions = $this->subscriberSegmentRepository->findAll(['subscriber' => $subscriberEntity]);
|
||||
$subscriptions = $this->subscriberSegmentRepository->findBy(['subscriber' => $subscriberEntity]);
|
||||
foreach ($subscriptions as $subscription) {
|
||||
$segment = $subscription->getSegment();
|
||||
if ($segment instanceof SegmentEntity) {
|
||||
@ -48,4 +57,27 @@ class SubscribersResponseBuilder {
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function buildUnsubscribes(SubscriberEntity $subscriberEntity): array {
|
||||
$unsubscribes = $this->statisticsUnsubscribesRepository->findBy([
|
||||
'subscriberId' => $subscriberEntity->getId(),
|
||||
], [
|
||||
'createdAt' => 'desc',
|
||||
]);
|
||||
$result = [];
|
||||
foreach ($unsubscribes as $unsubscribe) {
|
||||
$mapped = [
|
||||
'source' => $unsubscribe->getSource(),
|
||||
'meta' => $unsubscribe->getMeta(),
|
||||
'createdAt' => $unsubscribe->getCreatedAt(),
|
||||
];
|
||||
$newsletter = $unsubscribe->getNewsletter();
|
||||
if ($newsletter instanceof NewsletterEntity) {
|
||||
$mapped['newsletterId'] = $newsletter->getId();
|
||||
$mapped['newsletterSubject'] = $newsletter->getSubject();
|
||||
}
|
||||
$result[] = $mapped;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user