Move getting segments to the new class
[MAILPOET-4292]
This commit is contained in:
@@ -24,6 +24,9 @@ class API {
|
||||
/** @var CustomFields */
|
||||
private $customFields;
|
||||
|
||||
/** @var Segments */
|
||||
private $segments;
|
||||
|
||||
/** @var Subscribers */
|
||||
private $subscribers;
|
||||
|
||||
@@ -33,11 +36,13 @@ class API {
|
||||
public function __construct(
|
||||
RequiredCustomFieldValidator $requiredCustomFieldValidator,
|
||||
CustomFields $customFields,
|
||||
Segments $segments,
|
||||
Subscribers $subscribers,
|
||||
Changelog $changelog
|
||||
) {
|
||||
$this->requiredCustomFieldValidator = $requiredCustomFieldValidator;
|
||||
$this->customFields = $customFields;
|
||||
$this->segments = $segments;
|
||||
$this->subscribers = $subscribers;
|
||||
$this->changelog = $changelog;
|
||||
}
|
||||
@@ -116,9 +121,8 @@ class API {
|
||||
return $subscriber->withCustomFields()->withSubscriptions()->asArray();
|
||||
}
|
||||
|
||||
public function getLists() {
|
||||
return Segment::where('type', Segment::TYPE_DEFAULT)
|
||||
->findArray();
|
||||
public function getLists(): array {
|
||||
return $this->segments->getAll();
|
||||
}
|
||||
|
||||
public function addSubscriber(array $subscriber, $listIds = [], $options = []) {
|
||||
|
44
mailpoet/lib/API/MP/v1/Segments.php
Normal file
44
mailpoet/lib/API/MP/v1/Segments.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\API\MP\v1;
|
||||
|
||||
use MailPoet\Entities\SegmentEntity;
|
||||
use MailPoet\Segments\SegmentsRepository;
|
||||
|
||||
class Segments {
|
||||
private const DATE_FORMAT = 'Y-m-d H:i:s';
|
||||
|
||||
/** @var SegmentsRepository */
|
||||
private $segmentsRepository;
|
||||
|
||||
public function __construct (
|
||||
SegmentsRepository $segmentsRepository
|
||||
) {
|
||||
$this->segmentsRepository = $segmentsRepository;
|
||||
}
|
||||
|
||||
public function getAll(): array {
|
||||
$segments = $this->segmentsRepository->findBy(['type' => SegmentEntity::TYPE_DEFAULT], ['id' => 'asc']);
|
||||
$result = [];
|
||||
foreach ($segments as $segment) {
|
||||
$result[] = $this->buildItem($segment);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SegmentEntity $segment
|
||||
* @return array
|
||||
*/
|
||||
private function buildItem(SegmentEntity $segment): array {
|
||||
return [
|
||||
'id' => (string)$segment->getId(), // (string) for BC
|
||||
'name' => $segment->getName(),
|
||||
'type' => $segment->getType(),
|
||||
'description' => $segment->getDescription(),
|
||||
'created_at' => ($createdAt = $segment->getCreatedAt()) ? $createdAt->format(self::DATE_FORMAT) : null,
|
||||
'updated_at' => $segment->getUpdatedAt()->format(self::DATE_FORMAT),
|
||||
'deleted_at' => ($deletedAt = $segment->getDeletedAt()) ? $deletedAt->format(self::DATE_FORMAT) : null,
|
||||
];
|
||||
}
|
||||
}
|
@@ -61,6 +61,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\API\MP\v1\CustomFields::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\MP\v1\API::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\MP\v1\Subscribers::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\MP\v1\Segments::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\Analytics::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\AutomatedLatestContent::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\API\JSON\v1\AutomaticEmails::class)->setPublic(true);
|
||||
|
Reference in New Issue
Block a user