Refactor MP v1 API to obtain services via constructor
[MAILPOET-1637]
This commit is contained in:
@ -3,19 +3,37 @@
|
||||
namespace MailPoet\API;
|
||||
|
||||
use MailPoet\Config\AccessControl;
|
||||
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Container;
|
||||
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class API {
|
||||
|
||||
/** @var Container */
|
||||
private static $container;
|
||||
|
||||
static function injectContainer(Container $container) {
|
||||
self::$container = $container;
|
||||
}
|
||||
|
||||
static function JSON(AccessControl $access_control) {
|
||||
return new \MailPoet\API\JSON\API($access_control);
|
||||
}
|
||||
|
||||
static function MP($version) {
|
||||
self::checkContainer();
|
||||
$api_class = sprintf('%s\MP\%s\API', __NAMESPACE__, $version);
|
||||
if(class_exists($api_class)) {
|
||||
return new $api_class();
|
||||
try {
|
||||
return self::$container->get($api_class);
|
||||
} catch (ServiceNotFoundException $e) {
|
||||
throw new \Exception(__('Invalid API version.', 'mailpoet'));
|
||||
}
|
||||
throw new \Exception(__('Invalid API version.', 'mailpoet'));
|
||||
}
|
||||
}
|
||||
|
||||
private static function checkContainer() {
|
||||
if(!self::$container) {
|
||||
throw new \Exception(__('Api was not initialized properly. Is MailPoet plugin active?.', 'mailpoet'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,26 @@ use MailPoet\Tasks\Sending;
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class API {
|
||||
|
||||
/** @var NewSubscriberNotificationMailer */
|
||||
private $new_subscribe_notification_mailer;
|
||||
|
||||
/** @var ConfirmationEmailMailer */
|
||||
private $confirmation_email_mailer;
|
||||
|
||||
/** @var RequiredCustomFieldValidator */
|
||||
private $required_custom_field_validator;
|
||||
|
||||
public function __construct(
|
||||
NewSubscriberNotificationMailer $new_subscribe_notification_mailer,
|
||||
ConfirmationEmailMailer $confirmation_email_mailer,
|
||||
RequiredCustomFieldValidator $required_custom_field_validator
|
||||
) {
|
||||
$this->new_subscribe_notification_mailer = $new_subscribe_notification_mailer;
|
||||
$this->confirmation_email_mailer = $confirmation_email_mailer;
|
||||
$this->required_custom_field_validator = $required_custom_field_validator;
|
||||
}
|
||||
|
||||
function getSubscriberFields() {
|
||||
$data = array(
|
||||
array(
|
||||
@ -168,8 +188,7 @@ class API {
|
||||
// if some required default fields are missing, set their values
|
||||
$default_fields = Subscriber::setRequiredFieldsDefaultValues($default_fields);
|
||||
|
||||
$validator = new RequiredCustomFieldValidator();
|
||||
$validator->validate($custom_fields);
|
||||
$this->required_custom_field_validator->validate($custom_fields);
|
||||
|
||||
// add subscriber
|
||||
$new_subscriber = Subscriber::create();
|
||||
@ -255,8 +274,7 @@ class API {
|
||||
}
|
||||
|
||||
protected function _sendConfirmationEmail(Subscriber $subscriber) {
|
||||
$sender = new ConfirmationEmailMailer();
|
||||
return $sender->sendConfirmationEmail($subscriber);
|
||||
return $this->confirmation_email_mailer->sendConfirmationEmail($subscriber);
|
||||
}
|
||||
|
||||
protected function _scheduleWelcomeNotification(Subscriber $subscriber, array $segments) {
|
||||
@ -274,7 +292,6 @@ class API {
|
||||
}
|
||||
|
||||
private function sendSubscriberNotification(Subscriber $subscriber, array $segment_ids) {
|
||||
$sender = new NewSubscriberNotificationMailer();
|
||||
$sender->send($subscriber, Segment::whereIn('id', $segment_ids)->findMany());
|
||||
$this->new_subscribe_notification_mailer->send($subscriber, Segment::whereIn('id', $segment_ids)->findMany());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user