Add subscriptions to the response
[MAILPOET-2653]
This commit is contained in:
@ -2,16 +2,26 @@
|
||||
|
||||
namespace MailPoet\API\JSON\ResponseBuilders;
|
||||
|
||||
use MailPoet\Entities\SegmentEntity;
|
||||
use MailPoet\Entities\SubscriberCustomFieldEntity;
|
||||
use MailPoet\Entities\SubscriberEntity;
|
||||
use MailPoet\Subscribers\SubscriberSegmentRepository;
|
||||
|
||||
class SubscribersResponseBuilder {
|
||||
|
||||
/** @var SubscriberSegmentRepository */
|
||||
private $subscriberSegmentRepository;
|
||||
|
||||
public function __construct(SubscriberSegmentRepository $subscriberSegmentRepository) {
|
||||
$this->subscriberSegmentRepository = $subscriberSegmentRepository;
|
||||
}
|
||||
|
||||
public function build(SubscriberEntity $subscriberEntity): array {
|
||||
$data = [
|
||||
'id' => $subscriberEntity->getId(),
|
||||
'wp_user_id' => $subscriberEntity->getWpUserId(),
|
||||
'is_woocommerce_user' => $subscriberEntity->getIsWoocommerceUser(),
|
||||
'subscriptions' => [],// TODO
|
||||
'subscriptions' => $this->buildSubscriptions($subscriberEntity),
|
||||
'unsubscribes' => [],// TODO
|
||||
// TODO custom fields
|
||||
'status' => $subscriberEntity->getStatus(),
|
||||
@ -22,4 +32,20 @@ class SubscribersResponseBuilder {
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function buildSubscriptions(SubscriberEntity $subscriberEntity): array {
|
||||
$result = [];
|
||||
$subscriptions = $this->subscriberSegmentRepository->findAll(['subscriber' => $subscriberEntity]);
|
||||
foreach ($subscriptions as $subscription) {
|
||||
$segment = $subscription->getSegment();
|
||||
if ($segment instanceof SegmentEntity) {
|
||||
$result[] = [
|
||||
'segment_id' => $segment->getId(),
|
||||
'status' => $subscription->getStatus(),
|
||||
'updated_at' => $subscription->getUpdatedAt(),
|
||||
];
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user