Refactor dynamic segments listings to doctrine

[MAILPOET-3361]
This commit is contained in:
Pavel Dohnal
2021-01-08 10:44:02 +01:00
committed by Veljko V
parent 11c9c3cd93
commit 822550f3c0
6 changed files with 110 additions and 39 deletions

View File

@ -0,0 +1,49 @@
<?php
namespace MailPoet\API\JSON\ResponseBuilders;
use MailPoet\Entities\SegmentEntity;
use MailPoet\Entities\SubscriberEntity;
use MailPoet\Segments\SegmentSubscribersRepository;
use MailPoet\WP\Functions;
class DynamicSegmentsResponseBuilder {
const DATE_FORMAT = 'Y-m-d H:i:s';
/** @var SegmentsResponseBuilder */
private $segmentsResponseBuilder;
/** @var Functions */
private $wp;
/** @var SegmentSubscribersRepository */
private $segmentSubscriberRepository;
public function __construct(
Functions $wp,
SegmentSubscribersRepository $segmentSubscriberRepository,
SegmentsResponseBuilder $segmentsResponseBuilder
) {
$this->segmentsResponseBuilder = $segmentsResponseBuilder;
$this->segmentSubscriberRepository = $segmentSubscriberRepository;
$this->wp = $wp;
}
public function buildForListing(array $segments): array {
$data = [];
foreach ($segments as $segment) {
$data[] = $this->buildListingItem($segment);
}
return $data;
}
private function buildListingItem(SegmentEntity $segment): array {
$data = $this->segmentsResponseBuilder->build($segment);
$data['subscribers_url'] = $this->wp->adminUrl(
'admin.php?page=mailpoet-subscribers#/filter[segment=' . $segment->getId() . ']'
);
$data['count_all'] = $this->segmentSubscriberRepository->getSubscribersCount((int)$segment->getId());
$data['count_subscribed'] = $this->segmentSubscriberRepository->getSubscribersCount((int)$segment->getId(), SubscriberEntity::STATUS_SUBSCRIBED);
return $data;
}
}

View File

@ -5,6 +5,7 @@ namespace MailPoet\API\JSON\v1;
use MailPoet\API\JSON\Endpoint as APIEndpoint;
use MailPoet\API\JSON\Error;
use MailPoet\API\JSON\Response;
use MailPoet\API\JSON\ResponseBuilders\DynamicSegmentsResponseBuilder;
use MailPoet\Config\AccessControl;
use MailPoet\DynamicSegments\Exceptions\ErrorSavingException;
use MailPoet\DynamicSegments\Exceptions\InvalidSegmentTypeException;
@ -12,11 +13,10 @@ use MailPoet\DynamicSegments\Mappers\DBMapper;
use MailPoet\DynamicSegments\Mappers\FormDataMapper;
use MailPoet\DynamicSegments\Persistence\Loading\SingleSegmentLoader;
use MailPoet\DynamicSegments\Persistence\Saver;
use MailPoet\Entities\SubscriberEntity;
use MailPoet\Listing\BulkActionController;
use MailPoet\Listing\Handler;
use MailPoet\Models\Model;
use MailPoet\Segments\SegmentSubscribersRepository;
use MailPoet\Segments\DynamicSegments\DynamicSegmentsListingRepository;
use MailPoet\WP\Functions as WPFunctions;
class DynamicSegments extends APIEndpoint {
@ -40,13 +40,17 @@ class DynamicSegments extends APIEndpoint {
/** @var Handler */
private $listingHandler;
/** @var SegmentSubscribersRepository */
private $segmentSubscriberRepository;
/** @var DynamicSegmentsListingRepository */
private $dynamicSegmentsListingRepository;
/** @var DynamicSegmentsResponseBuilder */
private $segmentsResponseBuilder;
public function __construct(
BulkActionController $bulkAction,
Handler $handler,
SegmentSubscribersRepository $segmentSubscriberRepository,
DynamicSegmentsListingRepository $dynamicSegmentsListingRepository,
DynamicSegmentsResponseBuilder $segmentsResponseBuilder,
$mapper = null,
$saver = null,
$dynamicSegmentsLoader = null
@ -56,7 +60,8 @@ class DynamicSegments extends APIEndpoint {
$this->mapper = $mapper ?: new FormDataMapper();
$this->saver = $saver ?: new Saver();
$this->dynamicSegmentsLoader = $dynamicSegmentsLoader ?: new SingleSegmentLoader(new DBMapper());
$this->segmentSubscriberRepository = $segmentSubscriberRepository;
$this->dynamicSegmentsListingRepository = $dynamicSegmentsListingRepository;
$this->segmentsResponseBuilder = $segmentsResponseBuilder;
}
public function get($data = []) {
@ -192,26 +197,18 @@ class DynamicSegments extends APIEndpoint {
}
public function listing($data = []) {
$listingData = $this->listingHandler->get('\MailPoet\Models\DynamicSegment', $data);
$definition = $this->listingHandler->getListingDefinition($data);
$items = $this->dynamicSegmentsListingRepository->getData($definition);
$count = $this->dynamicSegmentsListingRepository->getCount($definition);
$filters = $this->dynamicSegmentsListingRepository->getFilters($definition);
$groups = $this->dynamicSegmentsListingRepository->getGroups($definition);
$segments = $this->segmentsResponseBuilder->buildForListing($items);
$data = [];
foreach ($listingData['items'] as $segment) {
$segment->subscribersUrl = WPFunctions::get()->adminUrl(
'admin.php?page=mailpoet-subscribers#/filter[segment=' . $segment->id . ']'
);
$row = $segment->asArray();
$row['count_all'] = $this->segmentSubscriberRepository->getSubscribersCount($segment->id);
$row['count_subscribed'] = $this->segmentSubscriberRepository->getSubscribersCount($segment->id, SubscriberEntity::STATUS_SUBSCRIBED);
$data[] = $row;
}
return $this->successResponse($data, [
'count' => $listingData['count'],
'filters' => $listingData['filters'],
'groups' => $listingData['groups'],
return $this->successResponse($segments, [
'count' => $count,
'filters' => $filters,
'groups' => $groups,
]);
}
public function bulkAction($data = []) {

View File

@ -87,6 +87,7 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\SubscribersResponseBuilder::class)->setPublic(true);
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\FormsResponseBuilder::class);
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\SegmentsResponseBuilder::class)->setPublic(true);
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\DynamicSegmentsResponseBuilder::class)->setPublic(true);
// Automatic emails
$container->autowire(\MailPoet\AutomaticEmails\AutomaticEmails::class);
// Config
@ -257,6 +258,7 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\Segments\SegmentSubscribersRepository::class)->setPublic(true);
$container->autowire(\MailPoet\Segments\SegmentListingRepository::class)->setPublic(true);
$container->autowire(\MailPoet\Segments\SegmentSaveController::class)->setPublic(true);
$container->autowire(\MailPoet\Segments\DynamicSegments\DynamicSegmentsListingRepository::class)->setPublic(true);
$container->autowire(\MailPoet\Segments\DynamicSegments\FilterHandler::class)->setPublic(true);
$container->autowire(\MailPoet\Segments\DynamicSegments\Filters\EmailAction::class)->setPublic(true);
$container->autowire(\MailPoet\Segments\DynamicSegments\Filters\UserRole::class)->setPublic(true);

View File

@ -0,0 +1,10 @@
<?php
namespace MailPoet\Segments\DynamicSegments;
use MailPoet\Entities\SegmentEntity;
use MailPoet\Segments\SegmentListingRepository;
class DynamicSegmentsListingRepository extends SegmentListingRepository {
protected $types = [SegmentEntity::TYPE_DYNAMIC];
}

View File

@ -15,6 +15,8 @@ class SegmentListingRepository extends ListingRepository {
/** @var WooCommerce */
private $wooCommerce;
protected $types = [SegmentEntity::TYPE_DEFAULT, SegmentEntity::TYPE_WP_USERS];
public function __construct(
EntityManager $entityManager,
WooCommerce $wooCommerce
@ -50,7 +52,7 @@ class SegmentListingRepository extends ListingRepository {
}
protected function applyParameters(QueryBuilder $queryBuilder, array $parameters) {
$types = [SegmentEntity::TYPE_DEFAULT, SegmentEntity::TYPE_WP_USERS];
$types = $this->types;
if ($this->wooCommerce->shouldShowWooCommerceSegment()) {
$types[] = SegmentEntity::TYPE_WC_USERS;
}

View File

@ -4,6 +4,7 @@ namespace MailPoet\API\JSON\v1;
use Codeception\Stub;
use Codeception\Stub\Expected;
use MailPoet\API\JSON\ResponseBuilders\DynamicSegmentsResponseBuilder;
use MailPoet\DI\ContainerWrapper;
use MailPoet\DynamicSegments\Exceptions\ErrorSavingException;
use MailPoet\DynamicSegments\Exceptions\InvalidSegmentTypeException;
@ -13,7 +14,7 @@ use MailPoet\Listing\Handler;
use MailPoet\Models\DynamicSegment;
use MailPoet\Models\DynamicSegmentFilter;
use MailPoet\Models\Model;
use MailPoet\Segments\SegmentSubscribersRepository;
use MailPoet\Segments\DynamicSegments\DynamicSegmentsListingRepository;
class DynamicSegmentsTest extends \MailPoetTest {
@ -27,14 +28,16 @@ class DynamicSegmentsTest extends \MailPoetTest {
/** @var Handler */
private $listingHandler;
/** @var SegmentSubscribersRepository */
private $segmentSubscribersRepository;
/** @var DynamicSegmentsListingRepository */
private $listingRepository;
/** @var DynamicSegmentsResponseBuilder */
private $responseBuilder;
public function _before() {
$this->bulkAction = ContainerWrapper::getInstance()->get(BulkActionController::class);
$this->listingHandler = ContainerWrapper::getInstance()->get(Handler::class);
$this->segmentSubscribersRepository = ContainerWrapper::getInstance()->get(SegmentSubscribersRepository::class);
$this->listingRepository = ContainerWrapper::getInstance()->get(DynamicSegmentsListingRepository::class);
$this->responseBuilder = ContainerWrapper::getInstance()->get(DynamicSegmentsResponseBuilder::class);
}
public function testGetReturnsResponse() {
@ -49,7 +52,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
return $dynamicSegment;
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, null, null, $loader);
$response = $endpoint->get(['id' => 5]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\SuccessResponse');
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@ -69,7 +72,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
throw new \InvalidArgumentException('segment not found');
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, null, null, $loader);
$response = $endpoint->get(['id' => 5]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse');
expect($response->status)->equals(self::SEGMENT_NOT_FOUND_RESPONSE_CODE);
@ -86,7 +89,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
})]);
$saver = Stub::makeEmpty('\MailPoet\DynamicSegments\Persistence\Saver', ['save' => Expected::once()]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, $mapper, $saver);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $mapper, $saver);
$response = $endpoint->save([]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\SuccessResponse');
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@ -98,7 +101,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
})]);
$saver = Stub::makeEmpty('\MailPoet\DynamicSegments\Persistence\Saver', ['save' => Expected::never()]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, $mapper, $saver);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $mapper, $saver);
$response = $endpoint->save([]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse');
expect($response->status)->equals(self::INVALID_DATA_RESPONSE_CODE);
@ -117,7 +120,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
throw new ErrorSavingException('Error saving data', Model::DUPLICATE_RECORD);
})]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, $mapper, $saver);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, $mapper, $saver);
$response = $endpoint->save([]);
expect($response)->isInstanceOf('\MailPoet\API\JSON\ErrorResponse');
expect($response->status)->equals(self::SERVER_ERROR_RESPONSE_CODE);
@ -136,7 +139,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, null, null, $loader);
$response = $endpoint->trash(['id' => $dynamicSegment->id]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@ -161,7 +164,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, null, null, $loader);
$response = $endpoint->restore(['id' => $dynamicSegment->id]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@ -189,7 +192,7 @@ class DynamicSegmentsTest extends \MailPoetTest {
},
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, null, null, $loader);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->listingRepository, $this->responseBuilder, null, null, $loader);
$response = $endpoint->delete(['id' => $dynamicSegment->id]);
expect($response->status)->equals(self::SUCCESS_RESPONSE_CODE);
@ -214,7 +217,15 @@ class DynamicSegmentsTest extends \MailPoetTest {
'segment_id' => $dynamicSegment1->id,
]);
$endpoint = new DynamicSegments($this->bulkAction, $this->listingHandler, $this->segmentSubscribersRepository, null, null, null);
$endpoint = new DynamicSegments(
$this->bulkAction,
$this->listingHandler,
$this->listingRepository,
$this->responseBuilder,
null,
null,
null
);
$response = $endpoint->bulkAction([
'action' => 'trash',
'listing' => ['group' => 'all'],