Refactor WPFunctions to be injectable with DI

This commit is contained in:
Amine Ben hammou
2018-12-12 14:41:44 +01:00
parent e059ee7364
commit a46d98ec44
23 changed files with 146 additions and 97 deletions

View File

@@ -32,6 +32,8 @@ class AmazonSES {
/** @var AmazonSESMapper */
private $error_mapper;
private $wp;
function __construct(
$region,
$access_key,
@@ -61,11 +63,12 @@ class AmazonSES {
$this->date = gmdate('Ymd\THis\Z');
$this->date_without_time = gmdate('Ymd');
$this->error_mapper = $error_mapper;
$this->wp = new WPFunctions();
}
function send($newsletter, $subscriber, $extra_params = array()) {
try {
$result = WPFunctions::wpRemotePost(
$result = $this->wp->wpRemotePost(
$this->url,
$this->request($newsletter, $subscriber, $extra_params)
);
@@ -77,8 +80,8 @@ class AmazonSES {
$error = $this->error_mapper->getConnectionError($result->get_error_message());
return Mailer::formatMailerErrorResult($error);
}
if(WPFunctions::wpRemoteRetrieveResponseCode($result) !== 200) {
$response = simplexml_load_string(WPFunctions::wpRemoteRetrieveBody($result));
if($this->wp->wpRemoteRetrieveResponseCode($result) !== 200) {
$response = simplexml_load_string($this->wp->wpRemoteRetrieveBody($result));
$error = $this->error_mapper->getErrorFromResponse($response, $subscriber);
return Mailer::formatMailerErrorResult($error);
}

View File

@@ -17,15 +17,18 @@ class SendGrid {
/** @var SendGridMapper */
private $error_mapper;
private $wp;
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;
$this->wp = new WPFunctions();
}
function send($newsletter, $subscriber, $extra_params = array()) {
$result = WPFunctions::wpRemotePost(
$result = $this->wp->wpRemotePost(
$this->url,
$this->request($newsletter, $subscriber, $extra_params)
);
@@ -33,7 +36,7 @@ class SendGrid {
$error = $this->error_mapper->getConnectionError($result->get_error_message());
return Mailer::formatMailerErrorResult($error);
}
if(WPFunctions::wpRemoteRetrieveResponseCode($result) !== 200) {
if($this->wp->wpRemoteRetrieveResponseCode($result) !== 200) {
$response = json_decode($result['body'], true);
$error = $this->error_mapper->getErrorFromResponse($response, $subscriber);
return Mailer::formatMailerErrorResult($error);