Inject Listing Handler and Segments Subscriber Listing using DI
[MAILPOET-1689]
This commit is contained in:
@ -18,12 +18,19 @@ class Forms extends APIEndpoint {
|
|||||||
/** @var Listing\BulkActionController */
|
/** @var Listing\BulkActionController */
|
||||||
private $bulk_action;
|
private $bulk_action;
|
||||||
|
|
||||||
|
/** @var Listing\Handler */
|
||||||
|
private $listing_handler;
|
||||||
|
|
||||||
public $permissions = array(
|
public $permissions = array(
|
||||||
'global' => AccessControl::PERMISSION_MANAGE_FORMS
|
'global' => AccessControl::PERMISSION_MANAGE_FORMS
|
||||||
);
|
);
|
||||||
|
|
||||||
function __construct(Listing\BulkActionController $bulk_action) {
|
function __construct(
|
||||||
|
Listing\BulkActionController $bulk_action,
|
||||||
|
Listing\Handler $listing_handler
|
||||||
|
) {
|
||||||
$this->bulk_action = $bulk_action;
|
$this->bulk_action = $bulk_action;
|
||||||
|
$this->listing_handler = $listing_handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get($data = array()) {
|
function get($data = array()) {
|
||||||
@ -39,8 +46,7 @@ class Forms extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function listing($data = array()) {
|
function listing($data = array()) {
|
||||||
$listing = new Listing\Handler();
|
$listing_data = $this->listing_handler->get('\MailPoet\Models\Form', $data);
|
||||||
$listing_data = $listing->get('\MailPoet\Models\Form', $data);
|
|
||||||
|
|
||||||
$data = array();
|
$data = array();
|
||||||
foreach($listing_data['items'] as $form) {
|
foreach($listing_data['items'] as $form) {
|
||||||
|
@ -30,12 +30,19 @@ class Newsletters extends APIEndpoint {
|
|||||||
/** @var Listing\BulkActionController */
|
/** @var Listing\BulkActionController */
|
||||||
private $bulk_action;
|
private $bulk_action;
|
||||||
|
|
||||||
|
/** @var Listing\Handler */
|
||||||
|
private $listing_handler;
|
||||||
|
|
||||||
public $permissions = array(
|
public $permissions = array(
|
||||||
'global' => AccessControl::PERMISSION_MANAGE_EMAILS
|
'global' => AccessControl::PERMISSION_MANAGE_EMAILS
|
||||||
);
|
);
|
||||||
|
|
||||||
function __construct(Listing\BulkActionController $bulk_action) {
|
function __construct(
|
||||||
|
Listing\BulkActionController $bulk_action,
|
||||||
|
Listing\Handler $listing_handler
|
||||||
|
) {
|
||||||
$this->bulk_action = $bulk_action;
|
$this->bulk_action = $bulk_action;
|
||||||
|
$this->listing_handler = $listing_handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get($data = array()) {
|
function get($data = array()) {
|
||||||
@ -383,8 +390,7 @@ class Newsletters extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function listing($data = array()) {
|
function listing($data = array()) {
|
||||||
$listing = new Listing\Handler();
|
$listing_data = $this->listing_handler->get('\MailPoet\Models\Newsletter', $data);
|
||||||
$listing_data = $listing->get('\MailPoet\Models\Newsletter', $data);
|
|
||||||
|
|
||||||
$data = array();
|
$data = array();
|
||||||
foreach($listing_data['items'] as $newsletter) {
|
foreach($listing_data['items'] as $newsletter) {
|
||||||
|
@ -19,8 +19,15 @@ class Segments extends APIEndpoint {
|
|||||||
/** @var Listing\BulkActionController */
|
/** @var Listing\BulkActionController */
|
||||||
private $bulk_action;
|
private $bulk_action;
|
||||||
|
|
||||||
function __construct(Listing\BulkActionController $bulk_action) {
|
/** @var Listing\Handler */
|
||||||
|
private $listing_handler;
|
||||||
|
|
||||||
|
function __construct(
|
||||||
|
Listing\BulkActionController $bulk_action,
|
||||||
|
Listing\Handler $listing_handler
|
||||||
|
) {
|
||||||
$this->bulk_action = $bulk_action;
|
$this->bulk_action = $bulk_action;
|
||||||
|
$this->listing_handler = $listing_handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get($data = array()) {
|
function get($data = array()) {
|
||||||
@ -36,8 +43,7 @@ class Segments extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function listing($data = array()) {
|
function listing($data = array()) {
|
||||||
$listing = new Listing\Handler();
|
$listing_data = $this->listing_handler->get('\MailPoet\Models\Segment', $data);
|
||||||
$listing_data = $listing->get('\MailPoet\Models\Segment', $data);
|
|
||||||
|
|
||||||
$data = array();
|
$data = array();
|
||||||
foreach($listing_data['items'] as $segment) {
|
foreach($listing_data['items'] as $segment) {
|
||||||
|
@ -32,8 +32,25 @@ class Subscribers extends APIEndpoint {
|
|||||||
/** @var Listing\BulkActionController */
|
/** @var Listing\BulkActionController */
|
||||||
private $bulk_action_controller;
|
private $bulk_action_controller;
|
||||||
|
|
||||||
public function __construct(Listing\BulkActionController $bulk_action_controller) {
|
/** @var SubscribersListings */
|
||||||
|
private $subscribers_listings;
|
||||||
|
|
||||||
|
/** @var RequiredCustomFieldValidator */
|
||||||
|
private $required_custom_field_validator;
|
||||||
|
|
||||||
|
/** @var Listing\Handler */
|
||||||
|
private $listing_handler;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
Listing\BulkActionController $bulk_action_controller,
|
||||||
|
SubscribersListings $subscribers_listings,
|
||||||
|
RequiredCustomFieldValidator $required_custom_field_validator,
|
||||||
|
Listing\Handler $listing_handler
|
||||||
|
) {
|
||||||
$this->bulk_action_controller = $bulk_action_controller;
|
$this->bulk_action_controller = $bulk_action_controller;
|
||||||
|
$this->subscribers_listings = $subscribers_listings;
|
||||||
|
$this->required_custom_field_validator = $required_custom_field_validator;
|
||||||
|
$this->listing_handler = $listing_handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get($data = array()) {
|
function get($data = array()) {
|
||||||
@ -56,12 +73,9 @@ class Subscribers extends APIEndpoint {
|
|||||||
function listing($data = array()) {
|
function listing($data = array()) {
|
||||||
|
|
||||||
if(!isset($data['filter']['segment'])) {
|
if(!isset($data['filter']['segment'])) {
|
||||||
$listing = new Listing\Handler();
|
$listing_data = $this->listing_handler->get('\MailPoet\Models\Subscriber', $data);
|
||||||
|
|
||||||
$listing_data = $listing->get('\MailPoet\Models\Subscriber', $data);
|
|
||||||
} else {
|
} else {
|
||||||
$listings = new SubscribersListings();
|
$listing_data = $this->subscribers_listings->getListingsInSegment($data);
|
||||||
$listing_data = $listings->getListingsInSegment($data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = array();
|
$data = array();
|
||||||
@ -130,8 +144,7 @@ class Subscribers extends APIEndpoint {
|
|||||||
$data = $this->deobfuscateFormPayload($data);
|
$data = $this->deobfuscateFormPayload($data);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$validator = new RequiredCustomFieldValidator();
|
$this->required_custom_field_validator->validate($data);
|
||||||
$validator->validate($data);
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return $this->badRequest([APIError::BAD_REQUEST => $e->getMessage()]);
|
return $this->badRequest([APIError::BAD_REQUEST => $e->getMessage()]);
|
||||||
}
|
}
|
||||||
|
@ -59,6 +59,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Cron\DaemonHttpRunner::class)->setPublic(true);
|
$container->autowire(\MailPoet\Cron\DaemonHttpRunner::class)->setPublic(true);
|
||||||
// Listing
|
// Listing
|
||||||
$container->autowire(\MailPoet\Listing\BulkActionController::class)->setPublic(true);
|
$container->autowire(\MailPoet\Listing\BulkActionController::class)->setPublic(true);
|
||||||
|
$container->autowire(\MailPoet\Listing\Handler::class)->setPublic(true);
|
||||||
// Router
|
// Router
|
||||||
$container->autowire(\MailPoet\Router\Endpoints\CronDaemon::class)->setPublic(true);
|
$container->autowire(\MailPoet\Router\Endpoints\CronDaemon::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Router\Endpoints\Subscription::class)->setPublic(true);
|
$container->autowire(\MailPoet\Router\Endpoints\Subscription::class)->setPublic(true);
|
||||||
@ -68,6 +69,8 @@ class ContainerConfigurator implements IContainerConfigurator {
|
|||||||
$container->autowire(\MailPoet\Subscribers\NewSubscriberNotificationMailer::class)->setPublic(true);
|
$container->autowire(\MailPoet\Subscribers\NewSubscriberNotificationMailer::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Subscribers\ConfirmationEmailMailer::class)->setPublic(true);
|
$container->autowire(\MailPoet\Subscribers\ConfirmationEmailMailer::class)->setPublic(true);
|
||||||
$container->autowire(\MailPoet\Subscribers\RequiredCustomFieldValidator::class)->setPublic(true);
|
$container->autowire(\MailPoet\Subscribers\RequiredCustomFieldValidator::class)->setPublic(true);
|
||||||
|
// Segments
|
||||||
|
$container->autowire(\MailPoet\Segments\SubscribersListings::class)->setPublic(true);
|
||||||
// Subscription
|
// Subscription
|
||||||
$container->autowire(\MailPoet\Subscription\Form::class)->setPublic(true);
|
$container->autowire(\MailPoet\Subscription\Form::class)->setPublic(true);
|
||||||
// Newsletter
|
// Newsletter
|
||||||
|
@ -4,6 +4,13 @@ namespace MailPoet\Listing;
|
|||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class BulkActionController {
|
class BulkActionController {
|
||||||
|
/** @var Handler */
|
||||||
|
private $handler;
|
||||||
|
|
||||||
|
function __construct(Handler $handler) {
|
||||||
|
$this->handler = $handler;
|
||||||
|
}
|
||||||
|
|
||||||
function apply($model_class, array $data) {
|
function apply($model_class, array $data) {
|
||||||
$bulk_action_method = 'bulk'.ucfirst($data['action']);
|
$bulk_action_method = 'bulk'.ucfirst($data['action']);
|
||||||
unset($data['action']);
|
unset($data['action']);
|
||||||
@ -14,11 +21,9 @@ class BulkActionController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$listing_handler = new Handler();
|
|
||||||
|
|
||||||
return call_user_func_array(
|
return call_user_func_array(
|
||||||
array($model_class, $bulk_action_method),
|
array($model_class, $bulk_action_method),
|
||||||
array($listing_handler->getSelection($model_class, $data['listing']), $data)
|
array($this->handler->getSelection($model_class, $data['listing']), $data)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,13 @@ use MailPoet\WP\Hooks;
|
|||||||
|
|
||||||
class SubscribersListings {
|
class SubscribersListings {
|
||||||
|
|
||||||
|
/** @var Handler */
|
||||||
|
private $handler;
|
||||||
|
|
||||||
|
function __construct(Handler $handler) {
|
||||||
|
$this->handler = $handler;
|
||||||
|
}
|
||||||
|
|
||||||
function getListingsInSegment($data) {
|
function getListingsInSegment($data) {
|
||||||
if(!isset($data['filter']['segment'])) {
|
if(!isset($data['filter']['segment'])) {
|
||||||
throw new \InvalidArgumentException('Missing segment id');
|
throw new \InvalidArgumentException('Missing segment id');
|
||||||
@ -19,9 +26,8 @@ class SubscribersListings {
|
|||||||
|
|
||||||
private function getListings($data, Segment $segment = null) {
|
private function getListings($data, Segment $segment = null) {
|
||||||
if(!$segment || $segment->type === Segment::TYPE_DEFAULT || $segment->type === Segment::TYPE_WP_USERS) {
|
if(!$segment || $segment->type === Segment::TYPE_DEFAULT || $segment->type === Segment::TYPE_WP_USERS) {
|
||||||
$listing = new Handler();
|
|
||||||
|
|
||||||
return $listing_data = $listing->get('\MailPoet\Models\Subscriber', $data);
|
return $listing_data = $this->handler->get('\MailPoet\Models\Subscriber', $data);
|
||||||
}
|
}
|
||||||
$handlers = Hooks::applyFilters('mailpoet_get_subscribers_listings_in_segment_handlers', array());
|
$handlers = Hooks::applyFilters('mailpoet_get_subscribers_listings_in_segment_handlers', array());
|
||||||
foreach($handlers as $handler) {
|
foreach($handlers as $handler) {
|
||||||
|
@ -5,6 +5,7 @@ namespace MailPoet\Segments;
|
|||||||
require_once('DynamicListingsHandlerMock.php');
|
require_once('DynamicListingsHandlerMock.php');
|
||||||
|
|
||||||
use Codeception\Util\Stub;
|
use Codeception\Util\Stub;
|
||||||
|
use MailPoet\DI\ContainerWrapper;
|
||||||
use MailPoet\Models\Segment;
|
use MailPoet\Models\Segment;
|
||||||
use MailPoet\Models\Subscriber;
|
use MailPoet\Models\Subscriber;
|
||||||
use MailPoet\Models\SubscriberSegment;
|
use MailPoet\Models\SubscriberSegment;
|
||||||
@ -12,7 +13,11 @@ use MailPoet\WP\Hooks;
|
|||||||
|
|
||||||
class SubscribersListingsTest extends \MailPoetTest {
|
class SubscribersListingsTest extends \MailPoetTest {
|
||||||
|
|
||||||
|
/** @var SubscribersListings */
|
||||||
|
private $finder;
|
||||||
|
|
||||||
function _before() {
|
function _before() {
|
||||||
|
$this->finder = ContainerWrapper::getInstance()->get(SubscribersListings::class);
|
||||||
$this->cleanData();
|
$this->cleanData();
|
||||||
$this->segment_1 = Segment::createOrUpdate(array('name' => 'Segment 1', 'type' => 'default'));
|
$this->segment_1 = Segment::createOrUpdate(array('name' => 'Segment 1', 'type' => 'default'));
|
||||||
$this->segment_2 = Segment::createOrUpdate(array('name' => 'Segment 3', 'type' => 'not default'));
|
$this->segment_2 = Segment::createOrUpdate(array('name' => 'Segment 3', 'type' => 'not default'));
|
||||||
@ -49,20 +54,17 @@ class SubscribersListingsTest extends \MailPoetTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function testTryToGetListingsWithoutPassingSegment() {
|
function testTryToGetListingsWithoutPassingSegment() {
|
||||||
$finder = new SubscribersListings();
|
|
||||||
$this->setExpectedException('InvalidArgumentException');
|
$this->setExpectedException('InvalidArgumentException');
|
||||||
$finder->getListingsInSegment(array());
|
$this->finder->getListingsInSegment(array());
|
||||||
}
|
}
|
||||||
|
|
||||||
function testGetListingsForDefaultSegment() {
|
function testGetListingsForDefaultSegment() {
|
||||||
$finder = new SubscribersListings();
|
$listings = $this->finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_1->id)));
|
||||||
$listings = $finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_1->id)));
|
|
||||||
expect($listings['items'])->count(1);
|
expect($listings['items'])->count(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testGetListingsForNonExistingSegmen() {
|
function testGetListingsForNonExistingSegmen() {
|
||||||
$finder = new SubscribersListings();
|
$listings = $this->finder->getListingsInSegment(array('filter'=> array('segment' => 'non-existing-id')));
|
||||||
$listings = $finder->getListingsInSegment(array('filter'=> array('segment' => 'non-existing-id')));
|
|
||||||
expect($listings['items'])->notEmpty();
|
expect($listings['items'])->notEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,16 +80,14 @@ class SubscribersListingsTest extends \MailPoetTest {
|
|||||||
return array($mock);
|
return array($mock);
|
||||||
});
|
});
|
||||||
|
|
||||||
$finder = new SubscribersListings();
|
$listings = $this->finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_2->id)));
|
||||||
$listings = $finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_2->id)));
|
|
||||||
expect($listings)->equals('dynamic listings');
|
expect($listings)->equals('dynamic listings');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testTryToGetListingsForSegmentWithoutHandler() {
|
function testTryToGetListingsForSegmentWithoutHandler() {
|
||||||
$finder = new SubscribersListings();
|
|
||||||
$this->setExpectedException('InvalidArgumentException');
|
$this->setExpectedException('InvalidArgumentException');
|
||||||
remove_all_filters('mailpoet_get_subscribers_listings_in_segment_handlers');
|
remove_all_filters('mailpoet_get_subscribers_listings_in_segment_handlers');
|
||||||
$finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_2->id)));
|
$this->finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_2->id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user