Refactor sending methods to use error mappers

We want to add some logic to error handling.
This commit extracts error handling code from sending methods classes,
which already do a lot of other stuff, to error mappers which are responsible
for creating proper error object (MailerError). This error object is a replacement
for assoc. array which already had some special keys for certain usecases and
can not be properly type hinted.

[MAILPOET-1154]
This commit is contained in:
Rostislav Wolny
2018-08-30 10:42:58 +02:00
parent 8cf5d17cfd
commit 0923c892c1
22 changed files with 517 additions and 211 deletions

View File

@ -3,6 +3,7 @@
namespace MailPoet\Mailer\Methods;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\Methods\ErrorMappers\SendGridMapper;
use MailPoet\WP\Functions as WPFunctions;
if(!defined('ABSPATH')) exit;
@ -13,10 +14,14 @@ class SendGrid {
public $sender;
public $reply_to;
function __construct($api_key, $sender, $reply_to) {
/** @var SendGridMapper */
private $error_mapper;
function __construct($api_key, $sender, $reply_to, SendGridMapper $error_mapper) {
$this->api_key = $api_key;
$this->sender = $sender;
$this->reply_to = $reply_to;
$this->error_mapper = $error_mapper;
}
function send($newsletter, $subscriber, $extra_params = array()) {
@ -25,17 +30,13 @@ class SendGrid {
$this->request($newsletter, $subscriber, $extra_params)
);
if(is_wp_error($result)) {
return Mailer::formatMailerConnectionErrorResult($result->get_error_message());
$error = $this->error_mapper->getConnectionError($result->get_error_message());
return Mailer::formatMailerErrorResult($error);
}
if(WPFunctions::wpRemoteRetrieveResponseCode($result) !== 200) {
$response = json_decode($result['body'], true);
$response = (!empty($response['errors'][0])) ?
$response['errors'][0] :
sprintf(__('%s has returned an unknown error.', 'mailpoet'), Mailer::METHOD_SENDGRID);
if(empty($extra_params['test_email'])) {
$response .= sprintf(' %s: %s', __('Unprocessed subscriber', 'mailpoet'), $subscriber);
}
return Mailer::formatMailerSendErrorResult($response);
$error = $this->error_mapper->getErrorFromResponse($response, $subscriber, $extra_params);
return Mailer::formatMailerErrorResult($error);
}
return Mailer::formatMailerSendSuccessResult();
}
@ -80,4 +81,4 @@ class SendGrid {
'body' => http_build_query($body, null, '&')
);
}
}
}