Refactor mailer blacklist from trait to DI [MAILPOET-2208]

This commit is contained in:
wxa
2019-07-17 13:07:57 +03:00
committed by M. Shull
parent 01bd3d036b
commit 160d3d0607
12 changed files with 52 additions and 36 deletions

View File

@ -99,6 +99,7 @@ class ContainerConfigurator implements IContainerConfigurator {
->setArgument('$container', new Reference(ContainerWrapper::class));
// Mailer
$container->autowire(\MailPoet\Mailer\Mailer::class);
$container->autowire(\MailPoet\Mailer\Methods\Common\BlacklistCheck::class);
// Subscribers
$container->autowire(\MailPoet\Subscribers\NewSubscriberNotificationMailer::class)->setPublic(true);
$container->autowire(\MailPoet\Subscribers\ConfirmationEmailMailer::class)->setPublic(true);

View File

@ -2,14 +2,13 @@
namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
use MailPoet\Mailer\Methods\ErrorMappers\AmazonSESMapper;
use MailPoet\WP\Functions as WPFunctions;
if (!defined('ABSPATH')) exit;
class AmazonSES {
use BlacklistTrait;
public $aws_access_key;
public $aws_secret_key;
public $aws_region;
@ -34,6 +33,9 @@ class AmazonSES {
/** @var AmazonSESMapper */
private $error_mapper;
/** @var BlacklistCheck */
private $blacklist;
private $wp;
function __construct(
@ -66,10 +68,11 @@ class AmazonSES {
$this->date_without_time = gmdate('Ymd');
$this->error_mapper = $error_mapper;
$this->wp = new WPFunctions();
$this->blacklist = new BlacklistCheck();
}
function send($newsletter, $subscriber, $extra_params = []) {
if ($this->isBlacklisted($subscriber)) {
if ($this->blacklist->isBlacklisted($subscriber)) {
$error = $this->error_mapper->getBlacklistError($subscriber);
return Mailer::formatMailerErrorResult($error);
}

View File

@ -1,15 +1,22 @@
<?php
namespace MailPoet\Mailer\Methods;
namespace MailPoet\Mailer\Methods\Common;
use MailPoet\Subscription\Blacklist;
trait BlacklistTrait {
class BlacklistCheck {
/** @var Blacklist */
private $blacklist;
public function __construct(Blacklist $blacklist = null) {
if (is_null($blacklist)) {
$blacklist = new Blacklist();
}
$this->blacklist = $blacklist;
}
function isBlacklisted($subscriber) {
$email = $this->getSubscriberEmailForBlacklistCheck($subscriber);
return $this->getBlacklist()->isBlacklisted($email);
return $this->blacklist->isBlacklisted($email);
}
private function getSubscriberEmailForBlacklistCheck($subscriber_string) {
@ -19,11 +26,4 @@ trait BlacklistTrait {
}
return $subscriber_data['email'];
}
private function getBlacklist() {
if (!$this->blacklist instanceof Blacklist) {
$this->blacklist = new Blacklist();
}
return $this->blacklist;
}
}

View File

@ -4,6 +4,7 @@ namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
use MailPoet\Config\ServicesChecker;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
use MailPoet\Mailer\Methods\ErrorMappers\MailPoetMapper;
use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge;
@ -12,8 +13,6 @@ use MailPoet\Services\Bridge\API;
if (!defined('ABSPATH')) exit;
class MailPoet {
use BlacklistTrait;
public $api;
public $sender;
public $reply_to;
@ -25,6 +24,9 @@ class MailPoet {
/** @var MailPoetMapper */
private $error_mapper;
/** @var BlacklistCheck */
private $blacklist;
function __construct($api_key, $sender, $reply_to, MailPoetMapper $error_mapper, AuthorizedEmailsController $authorized_emails_controller) {
$this->api = new API($api_key);
$this->sender = $sender;
@ -32,6 +34,7 @@ class MailPoet {
$this->services_checker = new ServicesChecker();
$this->error_mapper = $error_mapper;
$this->authorized_emails_controller = $authorized_emails_controller;
$this->blacklist = new BlacklistCheck();
}
function send($newsletter, $subscriber, $extra_params = []) {
@ -41,7 +44,7 @@ class MailPoet {
$subscribers_for_blacklist_check = is_array($subscriber) ? $subscriber : [$subscriber];
foreach ($subscribers_for_blacklist_check as $sub) {
if ($this->isBlacklisted($sub)) {
if ($this->blacklist->isBlacklisted($sub)) {
$error = $this->error_mapper->getBlacklistError($sub);
return Mailer::formatMailerErrorResult($error);
}

View File

@ -3,6 +3,7 @@
namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
use MailPoet\Mailer\Methods\ErrorMappers\PHPMailMapper;
if (!defined('ABSPATH')) exit;
@ -10,8 +11,6 @@ if (!defined('ABSPATH')) exit;
require_once ABSPATH . WPINC . '/class-phpmailer.php';
class PHPMail {
use BlacklistTrait;
public $sender;
public $reply_to;
public $return_path;
@ -20,6 +19,9 @@ class PHPMail {
/** @var PHPMailMapper */
private $error_mapper;
/** @var BlacklistCheck */
private $blacklist;
function __construct($sender, $reply_to, $return_path, PHPMailMapper $error_mapper) {
$this->sender = $sender;
$this->reply_to = $reply_to;
@ -28,10 +30,11 @@ class PHPMail {
$this->sender['from_email'];
$this->mailer = $this->buildMailer();
$this->error_mapper = $error_mapper;
$this->blacklist = new BlacklistCheck();
}
function send($newsletter, $subscriber, $extra_params = []) {
if ($this->isBlacklisted($subscriber)) {
if ($this->blacklist->isBlacklisted($subscriber)) {
$error = $this->error_mapper->getBlacklistError($subscriber);
return Mailer::formatMailerErrorResult($error);
}

View File

@ -2,14 +2,13 @@
namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
use MailPoet\WP\Functions as WPFunctions;
if (!defined('ABSPATH')) exit;
class SMTP {
use BlacklistTrait;
public $host;
public $port;
public $authentication;
@ -26,6 +25,9 @@ class SMTP {
/** @var SMTPMapper */
private $error_mapper;
/** @var BlacklistCheck */
private $blacklist;
private $wp;
function __construct(
@ -47,10 +49,11 @@ class SMTP {
$this->mailer_logger = new \Swift_Plugins_Loggers_ArrayLogger();
$this->mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($this->mailer_logger));
$this->error_mapper = $error_mapper;
$this->blacklist = new BlacklistCheck();
}
function send($newsletter, $subscriber, $extra_params = []) {
if ($this->isBlacklisted($subscriber)) {
if ($this->blacklist->isBlacklisted($subscriber)) {
$error = $this->error_mapper->getBlacklistError($subscriber);
return Mailer::formatMailerErrorResult($error);
}

View File

@ -3,14 +3,13 @@
namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
use MailPoet\Mailer\Methods\ErrorMappers\SendGridMapper;
use MailPoet\WP\Functions as WPFunctions;
if (!defined('ABSPATH')) exit;
class SendGrid {
use BlacklistTrait;
public $url = 'https://api.sendgrid.com/api/mail.send.json';
public $api_key;
public $sender;
@ -19,6 +18,9 @@ class SendGrid {
/** @var SendGridMapper */
private $error_mapper;
/** @var BlacklistCheck */
private $blacklist;
private $wp;
function __construct($api_key, $sender, $reply_to, SendGridMapper $error_mapper) {
@ -27,10 +29,11 @@ class SendGrid {
$this->reply_to = $reply_to;
$this->error_mapper = $error_mapper;
$this->wp = new WPFunctions();
$this->blacklist = new BlacklistCheck();
}
function send($newsletter, $subscriber, $extra_params = []) {
if ($this->isBlacklisted($subscriber)) {
if ($this->blacklist->isBlacklisted($subscriber)) {
$error = $this->error_mapper->getBlacklistError($subscriber);
return Mailer::formatMailerErrorResult($error);
}

View File

@ -5,7 +5,7 @@ use Codeception\Stub;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\AmazonSES;
use MailPoet\Mailer\Methods\ErrorMappers\AmazonSESMapper;
use MailPoet\Subscription\Blacklist;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
class AmazonSESTest extends \MailPoetTest {
function _before() {
@ -237,7 +237,7 @@ class AmazonSESTest extends \MailPoetTest {
function testItChecksBlacklistBeforeSending() {
$blacklisted_subscriber = 'blacklist_test@example.com';
$blacklist = Stub::make(new Blacklist(), ['isBlacklisted' => true], $this);
$blacklist = Stub::make(new BlacklistCheck(), ['isBlacklisted' => true], $this);
$mailer = Stub::make(
$this->mailer,
['blacklist' => $blacklist, 'error_mapper' => new AmazonSESMapper()],

View File

@ -9,7 +9,7 @@ use MailPoet\Mailer\Methods\ErrorMappers\MailPoetMapper;
use MailPoet\Mailer\Methods\MailPoet;
use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge\API;
use MailPoet\Subscription\Blacklist;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
class MailPoetAPITest extends \MailPoetTest {
function _before() {
@ -259,7 +259,7 @@ class MailPoetAPITest extends \MailPoetTest {
function testItChecksBlacklistBeforeSendingToASingleSubscriber() {
$blacklisted_subscriber = 'blacklist_test@example.com';
$blacklist = Stub::make(new Blacklist(), ['isBlacklisted' => true], $this);
$blacklist = Stub::make(new BlacklistCheck(), ['isBlacklisted' => true], $this);
$mailer = Stub::make(
$this->mailer,
[
@ -285,7 +285,7 @@ class MailPoetAPITest extends \MailPoetTest {
function testItChecksBlacklistBeforeSendingToMultipleSubscribers() {
$blacklisted_subscriber = 'blacklist_test@example.com';
$blacklist = Stub::make(new Blacklist(), ['isBlacklisted' => true], $this);
$blacklist = Stub::make(new BlacklistCheck(), ['isBlacklisted' => true], $this);
$mailer = Stub::make(
$this->mailer,
[

View File

@ -5,7 +5,7 @@ use Codeception\Stub;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\ErrorMappers\PHPMailMapper;
use MailPoet\Mailer\Methods\PHPMail;
use MailPoet\Subscription\Blacklist;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
class PHPMailTest extends \MailPoetTest {
function _before() {
@ -117,7 +117,7 @@ class PHPMailTest extends \MailPoetTest {
function testItChecksBlacklistBeforeSending() {
$blacklisted_subscriber = 'blacklist_test@example.com';
$blacklist = Stub::make(new Blacklist(), ['isBlacklisted' => true], $this);
$blacklist = Stub::make(new BlacklistCheck(), ['isBlacklisted' => true], $this);
$mailer = Stub::make(
$this->mailer,
['blacklist' => $blacklist, 'error_mapper' => new PHPMailMapper()],

View File

@ -5,7 +5,7 @@ use Codeception\Stub;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\ErrorMappers\SMTPMapper;
use MailPoet\Mailer\Methods\SMTP;
use MailPoet\Subscription\Blacklist;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
use MailPoet\WP\Functions as WPFunctions;
class SMTPTest extends \MailPoetTest {
@ -176,7 +176,7 @@ class SMTPTest extends \MailPoetTest {
function testItChecksBlacklistBeforeSending() {
$blacklisted_subscriber = 'blacklist_test@example.com';
$blacklist = Stub::make(new Blacklist(), ['isBlacklisted' => true], $this);
$blacklist = Stub::make(new BlacklistCheck(), ['isBlacklisted' => true], $this);
$mailer = Stub::make(
$this->mailer,
['blacklist' => $blacklist, 'error_mapper' => new SMTPMapper()],

View File

@ -5,7 +5,7 @@ use Codeception\Stub;
use MailPoet\Mailer\MailerError;
use MailPoet\Mailer\Methods\ErrorMappers\SendGridMapper;
use MailPoet\Mailer\Methods\SendGrid;
use MailPoet\Subscription\Blacklist;
use MailPoet\Mailer\Methods\Common\BlacklistCheck;
class SendGridTest extends \MailPoetTest {
function _before() {
@ -87,7 +87,7 @@ class SendGridTest extends \MailPoetTest {
function testItChecksBlacklistBeforeSending() {
$blacklisted_subscriber = 'blacklist_test@example.com';
$blacklist = Stub::make(new Blacklist(), ['isBlacklisted' => true], $this);
$blacklist = Stub::make(new BlacklistCheck(), ['isBlacklisted' => true], $this);
$mailer = Stub::make(
$this->mailer,
['blacklist' => $blacklist, 'error_mapper' => new SendGridMapper()],