mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2025-02-06 08:19:25 +03:00
Code cleanup!
This commit is contained in:
parent
24ddfa0e54
commit
4e43786949
@ -2,46 +2,52 @@
|
||||
|
||||
namespace Mailgun\Connection;
|
||||
|
||||
require dirname(__DIR__) . '/globals.php';
|
||||
require dirname(__DIR__) . '/Globals.php';
|
||||
|
||||
use Guzzle\Http\Client as Guzzle;
|
||||
use Mailgun\MailgunClient;
|
||||
|
||||
use Mailgun\Connection\Exceptions\NoDomainsConfigured;
|
||||
use Mailgun\Connection\Exceptions\InvalidCredentials;
|
||||
use Mailgun\Connection\Exceptions\MissingRequiredMIMEParameters;
|
||||
use Mailgun\Connection\Exceptions\GenericHTTPError;
|
||||
use Mailgun\Connection\Exceptions\InvalidCredentials;
|
||||
use Mailgun\Connection\Exceptions\NoDomainsConfigured;
|
||||
use Mailgun\Connection\Exceptions\MissingRequiredMIMEParameters;
|
||||
|
||||
class HttpBroker{
|
||||
|
||||
private $apiKey;
|
||||
protected $workingDomain;
|
||||
protected $debug;
|
||||
protected $debugMode;
|
||||
protected $mgClient;
|
||||
|
||||
protected $apiEndpoint = API_ENDPOINT;
|
||||
protected $apiVersion = API_VERSION;
|
||||
protected $apiUser = API_USER;
|
||||
protected $sdkVersion = SDK_VERSION;
|
||||
protected $sdkUserAgent = SDK_USER_AGENT;
|
||||
public function __construct($apiKey, $workingDomain, $debugMode = false){
|
||||
|
||||
public function __construct($apiKey, $domain, $debug = false){
|
||||
$this->apiKey = $apiKey;
|
||||
$this->workingDomain = $domain;
|
||||
$this->debug = $debug;
|
||||
if($this->debug){
|
||||
$this->client = new Guzzle('https://api.ninomail.com/' . $this->apiVersion . '/', array('ssl.certificate_authority' => false));
|
||||
$this->workingDomain = $workingDomain;
|
||||
$this->debugMode = $debugMode;
|
||||
|
||||
/*
|
||||
* !!WARNING, REMOVE DEBUG CODE BEFORE GOING GA!!
|
||||
*/
|
||||
|
||||
if($this->debugMode){
|
||||
$this->mgClient = new Guzzle('https://api.ninomail.com/' . API_VERSION . '/', array('ssl.certificate_authority' => false));
|
||||
}
|
||||
else{
|
||||
$this->client = new Guzzle('https://' . $this->apiEndpoint . '/' . $this->apiVersion . '/');
|
||||
$this->mgClient = new Guzzle('https://' . API_ENDPOINT . '/' . API_VERSION . '/');
|
||||
}
|
||||
$this->client->setDefaultOption('curl.options', array('CURLOPT_FORBID_REUSE' => true));
|
||||
$this->client->setDefaultOption('auth', array ($this->apiUser, $this->apiKey));
|
||||
$this->client->setDefaultOption('exceptions', true);
|
||||
$this->client->setUserAgent($this->sdkUserAgent . '/' . $this->sdkVersion);
|
||||
|
||||
/*
|
||||
* !!WARNING, REMOVE DEBUG CODE BEFORE GOING GA!!
|
||||
*/
|
||||
|
||||
$this->mgClient->setDefaultOption('curl.options', array('CURLOPT_FORBID_REUSE' => true));
|
||||
$this->mgClient->setDefaultOption('auth', array (API_USER, $this->apiKey));
|
||||
$this->mgClient->setDefaultOption('exceptions', true);
|
||||
$this->mgClient->setUserAgent(SDK_USER_AGENT . '/' . SDK_VERSION);
|
||||
}
|
||||
|
||||
public function postRequest($endpointUrl, $postData = array(), $files = array()){
|
||||
$request = $this->client->post($endpointUrl, array(), $postData);
|
||||
$request = $this->mgClient->post($endpointUrl, array(), $postData);
|
||||
|
||||
if(isset($files["attachment"])){
|
||||
foreach($files["attachment"] as $attachment){
|
||||
@ -53,87 +59,57 @@ class HttpBroker{
|
||||
$request->addPostFile("inline", $attachment);
|
||||
}
|
||||
}
|
||||
|
||||
$response = $request->send();
|
||||
$httpResponeCode = $response->getStatusCode();
|
||||
if($httpResponeCode === 200){
|
||||
$jsonResponseData = $response->json();
|
||||
foreach ($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
elseif($httpStatusCode == 401){
|
||||
throw new InvalidCredentials("Your credentials are incorrect.");
|
||||
}
|
||||
else{
|
||||
throw new GenericHTTPError("A generic HTTP Error has occurred! Check your network connection and try again.");
|
||||
return false;
|
||||
}
|
||||
$result->http_response_code = $httpResponeCode;
|
||||
return $result;
|
||||
return $this->responseHandler($response);
|
||||
}
|
||||
|
||||
public function getRequest($endpointUrl, $queryString = array()){
|
||||
$request = $this->client->get($endpointUrl, $queryString);
|
||||
$request = $this->mgClient->get($endpointUrl, $queryString);
|
||||
$response = $request->send();
|
||||
$httpResponeCode = $response->getStatusCode();
|
||||
if($httpResponeCode === 200){
|
||||
$jsonResponseData = $response->json();
|
||||
foreach ($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
elseif($httpStatusCode == 401){
|
||||
throw new InvalidCredentials("Your credentials are incorrect.");
|
||||
}
|
||||
else{
|
||||
throw new GenericHTTPError("A generic HTTP Error has occurred! Check your network connection and try again.");
|
||||
return false;
|
||||
}
|
||||
$result->http_response_code = $httpResponeCode;
|
||||
return $result;
|
||||
return $this->responseHandler($response);
|
||||
}
|
||||
|
||||
public function deleteRequest($endpointUrl){
|
||||
$request = $this->client->delete($endpointUrl);
|
||||
$request = $this->mgClient->delete($endpointUrl);
|
||||
$response = $request->send();
|
||||
$httpResponeCode = $response->getStatusCode();
|
||||
if($httpResponeCode === 200){
|
||||
$jsonResponseData = $response->json();
|
||||
foreach ($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
return $this->responseHandler($response);
|
||||
}
|
||||
|
||||
public function putRequest($endpointUrl, $queryString){
|
||||
$request = $this->mgClient->put($endpointUrl, $queryString);
|
||||
$response = $request->send();
|
||||
return $this->responseHandler($response);
|
||||
}
|
||||
|
||||
public function responseHandler($responseObj){
|
||||
$httpResponeCode = $responseObj->getStatusCode();
|
||||
if($httpResponeCode === 200){
|
||||
$jsonResponseData = $responseObj->json();
|
||||
foreach ($jsonResponseData as $key => $value){
|
||||
$result->http_response_body->$key = $value;
|
||||
}
|
||||
}
|
||||
elseif($httpStatusCode == 400){
|
||||
throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
|
||||
}
|
||||
elseif($httpStatusCode == 401){
|
||||
throw new InvalidCredentials("Your credentials are incorrect.");
|
||||
throw new InvalidCredentials(EXCEPTION_INVALID_CREDENTIALS);
|
||||
}
|
||||
elseif($httpStatusCode == 401){
|
||||
throw new GenericHTTPError(EXCEPTION_INVALID_CREDENTIALS);
|
||||
}
|
||||
elseif($httpStatusCode == 404){
|
||||
throw new MissingEndpoint(EXCEPTION_MISSING_ENDPOINT);
|
||||
}
|
||||
else{
|
||||
throw new GenericHTTPError("A generic HTTP Error has occurred! Check your network connection and try again.");
|
||||
throw new GenericHTTPError(EXCEPTION_GENERIC_HTTP_ERROR);
|
||||
return false;
|
||||
}
|
||||
$result->http_response_code = $httpResponeCode;
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function putRequest($endpointUrl, $queryString){
|
||||
$request = $this->client->put($endpointUrl, $queryString);
|
||||
$response = $request->send();
|
||||
$httpResponeCode = $response->getStatusCode();
|
||||
if($httpResponeCode === 200){
|
||||
$jsonResponseData = $response->json();
|
||||
foreach ($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
elseif($httpStatusCode == 401){
|
||||
throw new InvalidCredentials("Your credentials are incorrect.");
|
||||
}
|
||||
else{
|
||||
throw new GenericHTTPError("A generic HTTP Error has occurred! Check your network connection and try again.");
|
||||
return false;
|
||||
}
|
||||
$result->http_response_code = $httpResponeCode;
|
||||
return $result;
|
||||
}
|
||||
public function returnWorkingDomain(){
|
||||
return $this->workingDomain;
|
||||
}
|
||||
|
@ -17,8 +17,8 @@ class MailgunClient{
|
||||
* classes created from here.
|
||||
*/
|
||||
|
||||
public function __construct($apiKey, $domain, $debug = false){
|
||||
$this->httpBroker = new HttpBroker($apiKey, $domain, $debug);
|
||||
public function __construct($apiKey, $workingDomain, $debugMode = false){
|
||||
$this->httpBroker = new HttpBroker($apiKey, $workingDomain, $debugMode);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -29,18 +29,23 @@ class MailgunClient{
|
||||
public function Messages(){
|
||||
return new Messages($this->httpBroker);
|
||||
}
|
||||
|
||||
public function Unsubscribes(){
|
||||
return new Unsubscribes($this->httpBroker);
|
||||
}
|
||||
|
||||
public function Complaints(){
|
||||
return new Complaints($this->httpBroker);
|
||||
}
|
||||
|
||||
public function Bounces(){
|
||||
return new Bounces($this->httpBroker);
|
||||
}
|
||||
|
||||
public function Stats(){
|
||||
return new Stats($this->httpBroker);
|
||||
}
|
||||
|
||||
public function Logs(){
|
||||
return new Logs($this->httpBroker);
|
||||
}
|
||||
|
@ -1,12 +1,9 @@
|
||||
<?PHP
|
||||
|
||||
//BatchMessage.php - Extends the Message class and provides continuous recipient addition.
|
||||
|
||||
namespace Mailgun\Messages;
|
||||
|
||||
use Mailgun\Exceptions\NoDomainsConfigured;
|
||||
use Mailgun\Exceptions\HTTPError;
|
||||
use Mailgun\Connection\Exceptions\MissingRequiredMIMEParameters;
|
||||
use Mailgun\Messages\Exceptions\TooManyParameters;
|
||||
use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters;
|
||||
|
||||
|
||||
class BatchMessage extends MessageBuilder{
|
||||
@ -25,7 +22,7 @@ class BatchMessage extends MessageBuilder{
|
||||
if($this->toRecipientCount == 1000){
|
||||
//If autoSend is off, do things here.
|
||||
if($this->autoSend == false){
|
||||
throw new HTTPError("Too many recipients for API");
|
||||
throw new TooManyParameters("You've exceeded the maximum recipient count (1,000) on the to field.");
|
||||
}
|
||||
else{
|
||||
$this->sendMessage();
|
||||
@ -38,13 +35,13 @@ class BatchMessage extends MessageBuilder{
|
||||
}
|
||||
}
|
||||
|
||||
$addr = $name . " <" . $address . ">";
|
||||
$compiledAddress = $name . " <" . $address . ">";
|
||||
|
||||
if(isset($this->message["to"])){
|
||||
array_push($this->message["to"], $addr);
|
||||
array_push($this->message["to"], $compiledAddress);
|
||||
}
|
||||
else{
|
||||
$this->message["to"] = array($addr);
|
||||
$this->message["to"] = array($compiledAddress);
|
||||
}
|
||||
$attributes["id"] = $this->toRecipientCount;
|
||||
$this->batchRecipientAttributes["$address"] = $attributes;
|
||||
@ -57,10 +54,19 @@ class BatchMessage extends MessageBuilder{
|
||||
$message = $this->message;
|
||||
$files = $this->files;
|
||||
}
|
||||
if(array_key_exists("from", $message) &&
|
||||
array_key_exists("to", $message) &&
|
||||
array_key_exists("subject", $message) &&
|
||||
(array_key_exists("text", $message) || array_key_exists("html", $message))){
|
||||
if(!array_key_exists("from", $message)){
|
||||
throw new MissingRequiredMIMEParameters("You are missing the from parameter for your message.");
|
||||
}
|
||||
elseif(!array_key_exists("to", $message)){
|
||||
throw new MissingRequiredMIMEParameters("You are missing a recipient for your message.");
|
||||
}
|
||||
elseif(!array_key_exists("subject", $message)){
|
||||
throw new MissingRequiredMIMEParameters("You are missing the subject of the message.");
|
||||
}
|
||||
elseif((!array_key_exists("text", $message) && !array_key_exists("html", $message))){
|
||||
throw new MissingRequiredMIMEParameters("You are missing the body of the message.");
|
||||
}
|
||||
else{
|
||||
$this->message["recipient-variables"] = json_encode($this->batchRecipientAttributes);
|
||||
$response = $this->httpBroker->postRequest($this->endpointUrl, $message, $files);
|
||||
$this->batchRecipientAttributes = array();
|
||||
@ -68,9 +74,6 @@ class BatchMessage extends MessageBuilder{
|
||||
unset($this->message["to"]);
|
||||
return $response;
|
||||
}
|
||||
else{
|
||||
throw new MissingRequiredMIMEParameters("You are missing the minimum parameters to send a message.");
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
@ -1,37 +1,26 @@
|
||||
<?PHP
|
||||
|
||||
/*
|
||||
* Message.php - Message builder for creating a message object. Pass the message object to the client to send the message.
|
||||
*/
|
||||
namespace Mailgun\Messages;
|
||||
|
||||
use Mailgun\Messages\Exceptions\TooManyParameters;
|
||||
use Mailgun\Messages\Expcetions\InvalidParameter;
|
||||
use Mailgun\Messages\Exceptions\TooManyParameters;
|
||||
use Mailgun\Messages\Expcetions\InvalidParameterType;
|
||||
|
||||
class MessageBuilder extends Messages{
|
||||
|
||||
protected $message;
|
||||
protected $files;
|
||||
protected $message = array();
|
||||
protected $files = array();
|
||||
protected $sanitized;
|
||||
protected $toRecipientCount;
|
||||
protected $ccRecipientCount;
|
||||
protected $bccRecipientCount;
|
||||
protected $attachmentCount;
|
||||
protected $campaignIdCount;
|
||||
protected $customOptionCount;
|
||||
protected $toRecipientCount = 0;
|
||||
protected $ccRecipientCount = 0;
|
||||
protected $bccRecipientCount = 0;
|
||||
protected $attachmentCount = 0;
|
||||
protected $campaignIdCount = 0;
|
||||
protected $customOptionCount = 0;
|
||||
protected $httpBroker;
|
||||
|
||||
public function __construct($httpBroker){
|
||||
parent::__construct($httpBroker);
|
||||
$this->message = array();
|
||||
$this->files = array();
|
||||
$this->toRecipientCount = 0;
|
||||
$this->ccRecipientCount = 0;
|
||||
$this->bccRecipientCount = 0;
|
||||
$this->attachmentCount = 0;
|
||||
$this->campaignIdCount = 0;
|
||||
$this->customOptionCount = 0;
|
||||
$this->httpBroker = $httpBroker;
|
||||
}
|
||||
|
||||
@ -46,16 +35,16 @@ class MessageBuilder extends Messages{
|
||||
}
|
||||
}
|
||||
if(isset($name)){
|
||||
$addr = $name . " <" . $address . ">";
|
||||
$compiledAddress = $name . " <" . $address . ">";
|
||||
}
|
||||
else{
|
||||
$addr = $address;
|
||||
$compiledAddress = $address;
|
||||
}
|
||||
if(isset($this->message["to"])){
|
||||
array_push($this->message["to"], $addr);
|
||||
array_push($this->message["to"], $compiledAddress);
|
||||
}
|
||||
else{
|
||||
$this->message["to"] = array($addr);
|
||||
$this->message["to"] = array($compiledAddress);
|
||||
}
|
||||
$this->toRecipientCount++;
|
||||
return true;
|
||||
@ -76,16 +65,16 @@ class MessageBuilder extends Messages{
|
||||
}
|
||||
}
|
||||
if(isset($name)){
|
||||
$addr = $name . " <" . $address . ">";
|
||||
$compiledAddress = $name . " <" . $address . ">";
|
||||
}
|
||||
else{
|
||||
$addr = $address;
|
||||
$compiledAddress = $address;
|
||||
}
|
||||
if(isset($this->message["cc"])){
|
||||
array_push($this->message["cc"], $addr);
|
||||
array_push($this->message["cc"], $compiledAddress);
|
||||
}
|
||||
else{
|
||||
$this->message["cc"] = array($addr);
|
||||
$this->message["cc"] = array($compiledAddress);
|
||||
}
|
||||
$this->ccRecipientCount++;
|
||||
return true;
|
||||
@ -94,6 +83,7 @@ class MessageBuilder extends Messages{
|
||||
throw new TooManyParameters("You've exceeded the maximum recipient count (1,000) on the cc field.");
|
||||
}
|
||||
}
|
||||
|
||||
public function addBccRecipient($address, $attributes){
|
||||
if($this->bccRecipientCount < 1000){
|
||||
if(is_array($attributes)){
|
||||
@ -105,16 +95,16 @@ class MessageBuilder extends Messages{
|
||||
}
|
||||
}
|
||||
if(isset($name)){
|
||||
$addr = $name . " <" . $address . ">";
|
||||
$compiledAddress = $name . " <" . $address . ">";
|
||||
}
|
||||
else{
|
||||
$addr = $address;
|
||||
$compiledAddress = $address;
|
||||
}
|
||||
if(isset($this->message["bcc"])){
|
||||
array_push($this->message["bcc"], $addr);
|
||||
array_push($this->message["bcc"], $compiledAddress);
|
||||
}
|
||||
else{
|
||||
$this->message["bcc"] = array($addr);
|
||||
$this->message["bcc"] = array($compiledAddress);
|
||||
}
|
||||
$this->bccRecipientCount++;
|
||||
return true;
|
||||
@ -123,6 +113,7 @@ class MessageBuilder extends Messages{
|
||||
throw new TooManyParameters("You've exceeded the maximum recipient count (1,000) on the bcc field.");
|
||||
}
|
||||
}
|
||||
|
||||
public function setFromAddress($address, $attributes){
|
||||
if(isset($attributes)){
|
||||
if(is_array($attributes)){
|
||||
@ -138,14 +129,15 @@ class MessageBuilder extends Messages{
|
||||
}
|
||||
}
|
||||
if(isset($name)){
|
||||
$addr = $name . " <" . $address . ">";
|
||||
$compiledAddress = $name . " <" . $address . ">";
|
||||
}
|
||||
else{
|
||||
$addr = $address;
|
||||
$compiledAddress = $address;
|
||||
}
|
||||
$this->message['from'] = $addr;
|
||||
$this->message['from'] = $compiledAddress;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setSubject($data = NULL){
|
||||
if($data == NULL || $data == ""){
|
||||
$data = " ";
|
||||
@ -153,6 +145,7 @@ class MessageBuilder extends Messages{
|
||||
$this->message['subject'] = $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function addCustomHeader($headerName, $data){
|
||||
if(!preg_match("/^h:/i", $headerName)){
|
||||
$headerName = "h:" . $headerName;
|
||||
@ -160,6 +153,7 @@ class MessageBuilder extends Messages{
|
||||
$this->message[$headerName] = array($data);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setTextBody($data){
|
||||
if($data == NULL || $data == ""){
|
||||
$data = " ";
|
||||
@ -167,6 +161,7 @@ class MessageBuilder extends Messages{
|
||||
$this->message['text'] = $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setHtmlBody($data){
|
||||
if($data == NULL || $data == ""){
|
||||
$data = " ";
|
||||
@ -174,6 +169,7 @@ class MessageBuilder extends Messages{
|
||||
$this->message['html'] = $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function addAttachment($data){
|
||||
if(preg_match("/^@/", $data)){
|
||||
if(isset($this->files["attachment"])){
|
||||
@ -188,6 +184,7 @@ class MessageBuilder extends Messages{
|
||||
throw new InvalidParameter("Attachments must be passed with an \"@\" preceding the file path. Web resources not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
public function addInlineImage($data){
|
||||
if(preg_match("/^@/", $data)){
|
||||
if(isset($this->files['inline'])){
|
||||
@ -203,6 +200,7 @@ class MessageBuilder extends Messages{
|
||||
throw new InvalidParameter("Inline images must be passed with an \"@\" preceding the file path. Web resources not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
public function setTestMode($data){
|
||||
if(filter_var($data, FILTER_VALIDATE_BOOLEAN)){
|
||||
$data = "yes";
|
||||
@ -213,6 +211,7 @@ class MessageBuilder extends Messages{
|
||||
$this->message['o:testmode'] = $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function addCampaignId($data){
|
||||
if($this->campaignIdCount < 3){
|
||||
if(isset($this->message['o:campaign'])){
|
||||
@ -228,6 +227,7 @@ class MessageBuilder extends Messages{
|
||||
throw new TooManyParameters("You've exceeded the maximum (3) campaigns for a single message.");
|
||||
}
|
||||
}
|
||||
|
||||
public function setDkim($data){
|
||||
if(filter_var($data, FILTER_VALIDATE_BOOLEAN)){
|
||||
$data = "yes";
|
||||
@ -238,6 +238,7 @@ class MessageBuilder extends Messages{
|
||||
$this->message["o:dkim"] = $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setOpenTracking($data){
|
||||
if(filter_var($data, FILTER_VALIDATE_BOOLEAN)){
|
||||
$data = "yes";
|
||||
@ -248,6 +249,7 @@ class MessageBuilder extends Messages{
|
||||
$this->message['o:tracking-opens'] = $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setClickTracking($data){
|
||||
if(filter_var($data, FILTER_VALIDATE_BOOLEAN)){
|
||||
$data = "yes";
|
||||
@ -258,6 +260,7 @@ class MessageBuilder extends Messages{
|
||||
$this->message['o:tracking-clicks'] = $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setDeliveryTime($timeDate, $timeZone = NULL){
|
||||
if(isset($timeZone)){
|
||||
$timeZoneObj = new \DateTimeZone("$timeZone");
|
||||
@ -271,6 +274,7 @@ class MessageBuilder extends Messages{
|
||||
$this->message['o:deliverytime'] = $formattedTimeDate;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function addCustomData($customName, $data){
|
||||
if(is_array($data)){
|
||||
$jsonArray = json_encode($data);
|
||||
@ -282,6 +286,7 @@ class MessageBuilder extends Messages{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function addCustomOption($optionName, $data){
|
||||
if(!preg_match("/^o:/i", $optionName)){
|
||||
$optionName = "o:" . $optionName;
|
||||
@ -295,9 +300,11 @@ class MessageBuilder extends Messages{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function getMessage(){
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
public function getFiles(){
|
||||
return $this->files;
|
||||
}
|
||||
|
@ -1,16 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Message.php - Message builder for creating a message object. Pass the message object to the client to send the message.
|
||||
*/
|
||||
namespace Mailgun\Messages;
|
||||
|
||||
use Mailgun\Messages\MessageBuilder;
|
||||
use Mailgun\Messages\BatchMessage;
|
||||
use Mailgun\Messages\MessageBuilder;
|
||||
|
||||
use Mailgun\Messages\Exceptions\TooManyParameters;
|
||||
use Mailgun\Messages\Expcetions\InvalidParameter;
|
||||
use Mailgun\Messages\Expcetions\InvalidParameterType;
|
||||
use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters;
|
||||
|
||||
class Messages{
|
||||
|
||||
@ -23,20 +18,27 @@ class Messages{
|
||||
$this->workingDomain = $this->httpBroker->returnWorkingDomain();
|
||||
$this->endpointUrl = $this->workingDomain . "/messages";
|
||||
}
|
||||
|
||||
public function sendMessage($message = array(), $files = array()){
|
||||
if(count($message) < 1){
|
||||
$message = $this->message;
|
||||
$files = $this->files;
|
||||
}
|
||||
if(array_key_exists("from", $message) &&
|
||||
array_key_exists("to", $message) &&
|
||||
array_key_exists("subject", $message) &&
|
||||
(array_key_exists("text", $message) || array_key_exists("html", $message))){
|
||||
$response = $this->httpBroker->postRequest($this->endpointUrl, $message, $files);
|
||||
return $response;
|
||||
if(!array_key_exists("from", $message)){
|
||||
throw new MissingRequiredMIMEParameters("You are missing the from parameter for your message.");
|
||||
}
|
||||
elseif(!array_key_exists("to", $message)){
|
||||
throw new MissingRequiredMIMEParameters("You are missing a recipient for your message.");
|
||||
}
|
||||
elseif(!array_key_exists("subject", $message)){
|
||||
throw new MissingRequiredMIMEParameters("You are missing the subject of the message.");
|
||||
}
|
||||
elseif((!array_key_exists("text", $message) && !array_key_exists("html", $message))){
|
||||
throw new MissingRequiredMIMEParameters("You are missing the body of the message.");
|
||||
}
|
||||
else{
|
||||
throw new MissingRequiredMIMEParameters("You are missing the minimum parameters to send a message.");
|
||||
$response = $this->httpBroker->postRequest($this->endpointUrl, $message, $files);
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
@ -48,6 +50,7 @@ class Messages{
|
||||
public function MessageBuilder(){
|
||||
return new MessageBuilder($this->httpBroker);
|
||||
}
|
||||
|
||||
public function BatchMessage($autoSend = true){
|
||||
return new BatchMessage($this->httpBroker, $autoSend);
|
||||
}
|
||||
|
@ -8,4 +8,11 @@ const SDK_VERSION = "0.1";
|
||||
const SDK_USER_AGENT = "mailgun-sdk-php";
|
||||
const DEFAULT_TIME_ZONE = "UTC";
|
||||
|
||||
//Exception Messages
|
||||
|
||||
const EXCEPTION_INVALID_CREDENTIALS = "Your credentials are incorrect.";
|
||||
const EXCEPTION_GENERIC_HTTP_ERROR = "An HTTP Error has occurred! Check your network connection and try again.";
|
||||
const EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS = "The parameters passed to the API were invalid. This might be a bug! Notify support@mailgun.com.";
|
||||
const EXCEPTION_MISSING_ENDPOINT = "The endpoint you've tried to access does not exist. This might be a bug! Notify support@mailgun.com.";
|
||||
|
||||
?>
|
@ -22,184 +22,106 @@ class TestBroker extends HttpBroker{
|
||||
|
||||
public function postRequest($endpointUrl, $postData = array(), $files = array()){
|
||||
if(preg_match("/\/messages$/", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
$jsonResponseData = json_encode('{"message": "Queued. Thank you.","id": "<20111114174239.25659.5817@samples.mailgun.org>"}');
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/unsubscribes$/", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
$jsonResponseData = json_encode('{"message": "Address has been added to the unsubscribes table","address": "ev@mailgun.net"}');
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/complaints$/", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
//$jsonResponseData = json_encode('{"message": "Address has been added to the unsubscribes table","address": "ev@mailgun.net"}');
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/bounces$/", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
//$jsonResponseData = json_encode('{"message": "Address has been added to the unsubscribes table","address": "ev@mailgun.net"}');
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
else{
|
||||
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
public function getRequest($endpointUrl, $queryString = array()){
|
||||
if($endpointUrl == "domains"){
|
||||
$httpResponseCode = 200;
|
||||
$jsonResponseData = json_decode('{"total_count": 1,"items": [{"created_at": "Wed, 10 Jul 2013 19:26:52 GMT","smtp_login": "postmaster@samples.mailgun.org","name": "samples.mailgun.org","smtp_password": "4rtqo4p6rrx9"}]}');
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/unsubscribes\//", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
$jsonResponseData = json_encode('{"message": "Unsubscribe event has been removed","address": "ev@mailgun.net"}');
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/unsubscribes/", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/complaints/", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/bounces/", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/stats/", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/log/", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
else{
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
public function deleteRequest($endpointUrl){
|
||||
if($endpointUrl == "domains"){
|
||||
$httpResponseCode = 200;
|
||||
$jsonResponseData = json_decode('asdfasdfasdf');
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/unsubscribes\//", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
$jsonResponseData = json_encode('{"message": "Unsubscribe event has been removed","address": "ev@mailgun.net"}');
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/complaints\//", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
$jsonResponseData = json_encode('{"message": "Unsubscribe event has been removed","address": "ev@mailgun.net"}');
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/bounces\//", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
$jsonResponseData = json_encode('{"message": "Unsubscribe event has been removed","address": "ev@mailgun.net"}');
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
elseif(preg_match("/\/tags\//", $endpointUrl)){
|
||||
$httpResponseCode = "200";
|
||||
$jsonResponseData = json_encode('{"message": "Unsubscribe event has been removed","address": "ev@mailgun.net"}');
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
else{
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
public function putRequest($endpointUrl, $queryString){
|
||||
if($endpointUrl == "domains"){
|
||||
$httpResponseCode = 200;
|
||||
$jsonResponseData = json_decode('asdfasdfasdf');
|
||||
if($httpResponseCode === 200){
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->$key = $value;
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
else{
|
||||
return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
public function responseHandler($endpointUrl, $httpResponseCode = 200){
|
||||
if($httpResponseCode === 200){
|
||||
$jsonResponseData = json_decode('{"message": "Some JSON Response Data"}');
|
||||
foreach($jsonResponseData as $key => $value){
|
||||
$result->http_response_body->$key = $value;
|
||||
}
|
||||
}
|
||||
elseif($httpStatusCode == 400){
|
||||
throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
|
||||
}
|
||||
elseif($httpStatusCode == 401){
|
||||
throw new InvalidCredentials(EXCEPTION_INVALID_CREDENTIALS);
|
||||
}
|
||||
elseif($httpStatusCode == 401){
|
||||
throw new GenericHTTPError(EXCEPTION_INVALID_CREDENTIALS);
|
||||
}
|
||||
elseif($httpStatusCode == 404){
|
||||
throw new MissingEndpoint(EXCEPTION_MISSING_ENDPOINT);
|
||||
}
|
||||
else{
|
||||
throw new GenericHTTPError(EXCEPTION_GENERIC_HTTP_ERROR);
|
||||
return false;
|
||||
}
|
||||
$result->http_response_code = $httpResponseCode;
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -4,12 +4,6 @@ namespace Mailgun\Tests;
|
||||
|
||||
use Guzzle\Tests\GuzzleTestCase;
|
||||
|
||||
|
||||
abstract class MailgunTestCase extends GuzzleTestCase
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
abstract class MailgunTestCase extends GuzzleTestCase{}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user