diff --git a/lib/API/JSON/v1/Forms.php b/lib/API/JSON/v1/Forms.php index 64be673ae0..e420f19638 100644 --- a/lib/API/JSON/v1/Forms.php +++ b/lib/API/JSON/v1/Forms.php @@ -18,12 +18,19 @@ class Forms extends APIEndpoint { /** @var Listing\BulkActionController */ private $bulk_action; + /** @var Listing\Handler */ + private $listing_handler; + public $permissions = array( '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->listing_handler = $listing_handler; } function get($data = array()) { @@ -39,8 +46,7 @@ class Forms extends APIEndpoint { } function listing($data = array()) { - $listing = new Listing\Handler(); - $listing_data = $listing->get('\MailPoet\Models\Form', $data); + $listing_data = $this->listing_handler->get('\MailPoet\Models\Form', $data); $data = array(); foreach($listing_data['items'] as $form) { diff --git a/lib/API/JSON/v1/Newsletters.php b/lib/API/JSON/v1/Newsletters.php index 81c97ef5d8..8d2918ce9c 100644 --- a/lib/API/JSON/v1/Newsletters.php +++ b/lib/API/JSON/v1/Newsletters.php @@ -30,12 +30,19 @@ class Newsletters extends APIEndpoint { /** @var Listing\BulkActionController */ private $bulk_action; + /** @var Listing\Handler */ + private $listing_handler; + public $permissions = array( '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->listing_handler = $listing_handler; } function get($data = array()) { @@ -383,8 +390,7 @@ class Newsletters extends APIEndpoint { } function listing($data = array()) { - $listing = new Listing\Handler(); - $listing_data = $listing->get('\MailPoet\Models\Newsletter', $data); + $listing_data = $this->listing_handler->get('\MailPoet\Models\Newsletter', $data); $data = array(); foreach($listing_data['items'] as $newsletter) { diff --git a/lib/API/JSON/v1/Segments.php b/lib/API/JSON/v1/Segments.php index ae43129cb7..1ff2fb5cc9 100644 --- a/lib/API/JSON/v1/Segments.php +++ b/lib/API/JSON/v1/Segments.php @@ -19,8 +19,15 @@ class Segments extends APIEndpoint { /** @var Listing\BulkActionController */ 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->listing_handler = $listing_handler; } function get($data = array()) { @@ -36,8 +43,7 @@ class Segments extends APIEndpoint { } function listing($data = array()) { - $listing = new Listing\Handler(); - $listing_data = $listing->get('\MailPoet\Models\Segment', $data); + $listing_data = $this->listing_handler->get('\MailPoet\Models\Segment', $data); $data = array(); foreach($listing_data['items'] as $segment) { diff --git a/lib/API/JSON/v1/Subscribers.php b/lib/API/JSON/v1/Subscribers.php index 49c2df580a..77df7e62a4 100644 --- a/lib/API/JSON/v1/Subscribers.php +++ b/lib/API/JSON/v1/Subscribers.php @@ -32,8 +32,25 @@ class Subscribers extends APIEndpoint { /** @var Listing\BulkActionController */ 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->subscribers_listings = $subscribers_listings; + $this->required_custom_field_validator = $required_custom_field_validator; + $this->listing_handler = $listing_handler; } function get($data = array()) { @@ -56,12 +73,9 @@ class Subscribers extends APIEndpoint { function listing($data = array()) { if(!isset($data['filter']['segment'])) { - $listing = new Listing\Handler(); - - $listing_data = $listing->get('\MailPoet\Models\Subscriber', $data); + $listing_data = $this->listing_handler->get('\MailPoet\Models\Subscriber', $data); } else { - $listings = new SubscribersListings(); - $listing_data = $listings->getListingsInSegment($data); + $listing_data = $this->subscribers_listings->getListingsInSegment($data); } $data = array(); @@ -130,8 +144,7 @@ class Subscribers extends APIEndpoint { $data = $this->deobfuscateFormPayload($data); try { - $validator = new RequiredCustomFieldValidator(); - $validator->validate($data); + $this->required_custom_field_validator->validate($data); } catch (\Exception $e) { return $this->badRequest([APIError::BAD_REQUEST => $e->getMessage()]); } diff --git a/lib/DI/ContainerConfigurator.php b/lib/DI/ContainerConfigurator.php index 5c63cbdecf..a7cec44c08 100644 --- a/lib/DI/ContainerConfigurator.php +++ b/lib/DI/ContainerConfigurator.php @@ -59,6 +59,7 @@ class ContainerConfigurator implements IContainerConfigurator { $container->autowire(\MailPoet\Cron\DaemonHttpRunner::class)->setPublic(true); // Listing $container->autowire(\MailPoet\Listing\BulkActionController::class)->setPublic(true); + $container->autowire(\MailPoet\Listing\Handler::class)->setPublic(true); // Router $container->autowire(\MailPoet\Router\Endpoints\CronDaemon::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\ConfirmationEmailMailer::class)->setPublic(true); $container->autowire(\MailPoet\Subscribers\RequiredCustomFieldValidator::class)->setPublic(true); + // Segments + $container->autowire(\MailPoet\Segments\SubscribersListings::class)->setPublic(true); // Subscription $container->autowire(\MailPoet\Subscription\Form::class)->setPublic(true); // Newsletter diff --git a/lib/Listing/BulkActionController.php b/lib/Listing/BulkActionController.php index b1241230ed..e934bcecae 100644 --- a/lib/Listing/BulkActionController.php +++ b/lib/Listing/BulkActionController.php @@ -4,6 +4,13 @@ namespace MailPoet\Listing; if(!defined('ABSPATH')) exit; class BulkActionController { + /** @var Handler */ + private $handler; + + function __construct(Handler $handler) { + $this->handler = $handler; + } + function apply($model_class, array $data) { $bulk_action_method = 'bulk'.ucfirst($data['action']); unset($data['action']); @@ -14,11 +21,9 @@ class BulkActionController { ); } - $listing_handler = new Handler(); - return call_user_func_array( array($model_class, $bulk_action_method), - array($listing_handler->getSelection($model_class, $data['listing']), $data) + array($this->handler->getSelection($model_class, $data['listing']), $data) ); } } diff --git a/lib/Segments/SubscribersListings.php b/lib/Segments/SubscribersListings.php index c1f592d8cf..38dbfd4424 100644 --- a/lib/Segments/SubscribersListings.php +++ b/lib/Segments/SubscribersListings.php @@ -8,6 +8,13 @@ use MailPoet\WP\Hooks; class SubscribersListings { + /** @var Handler */ + private $handler; + + function __construct(Handler $handler) { + $this->handler = $handler; + } + function getListingsInSegment($data) { if(!isset($data['filter']['segment'])) { throw new \InvalidArgumentException('Missing segment id'); @@ -19,9 +26,8 @@ class SubscribersListings { private function getListings($data, Segment $segment = null) { 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()); foreach($handlers as $handler) { diff --git a/tests/integration/Segments/SubscribersListingsTest.php b/tests/integration/Segments/SubscribersListingsTest.php index 9ab0f8bc3e..23c3ff6fb9 100644 --- a/tests/integration/Segments/SubscribersListingsTest.php +++ b/tests/integration/Segments/SubscribersListingsTest.php @@ -5,6 +5,7 @@ namespace MailPoet\Segments; require_once('DynamicListingsHandlerMock.php'); use Codeception\Util\Stub; +use MailPoet\DI\ContainerWrapper; use MailPoet\Models\Segment; use MailPoet\Models\Subscriber; use MailPoet\Models\SubscriberSegment; @@ -12,7 +13,11 @@ use MailPoet\WP\Hooks; class SubscribersListingsTest extends \MailPoetTest { + /** @var SubscribersListings */ + private $finder; + function _before() { + $this->finder = ContainerWrapper::getInstance()->get(SubscribersListings::class); $this->cleanData(); $this->segment_1 = Segment::createOrUpdate(array('name' => 'Segment 1', 'type' => 'default')); $this->segment_2 = Segment::createOrUpdate(array('name' => 'Segment 3', 'type' => 'not default')); @@ -49,20 +54,17 @@ class SubscribersListingsTest extends \MailPoetTest { } function testTryToGetListingsWithoutPassingSegment() { - $finder = new SubscribersListings(); $this->setExpectedException('InvalidArgumentException'); - $finder->getListingsInSegment(array()); + $this->finder->getListingsInSegment(array()); } function testGetListingsForDefaultSegment() { - $finder = new SubscribersListings(); - $listings = $finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_1->id))); + $listings = $this->finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_1->id))); expect($listings['items'])->count(1); } function testGetListingsForNonExistingSegmen() { - $finder = new SubscribersListings(); - $listings = $finder->getListingsInSegment(array('filter'=> array('segment' => 'non-existing-id'))); + $listings = $this->finder->getListingsInSegment(array('filter'=> array('segment' => 'non-existing-id'))); expect($listings['items'])->notEmpty(); } @@ -78,16 +80,14 @@ class SubscribersListingsTest extends \MailPoetTest { return array($mock); }); - $finder = new SubscribersListings(); - $listings = $finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_2->id))); + $listings = $this->finder->getListingsInSegment(array('filter'=> array('segment' => $this->segment_2->id))); expect($listings)->equals('dynamic listings'); } function testTryToGetListingsForSegmentWithoutHandler() { - $finder = new SubscribersListings(); $this->setExpectedException('InvalidArgumentException'); 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))); } }