From 1b3720a339c7012814ccaef626a1650c4db4623e Mon Sep 17 00:00:00 2001 From: Travis Swientek Date: Wed, 7 Aug 2013 17:41:14 -0700 Subject: [PATCH] Removal of all classes. No OOP. :( --- src/Mailgun/Connection/RestClient.php | 12 +- src/Mailgun/Mailgun.php | 179 ++++-------------------- src/Mailgun/Messages/BatchMessage.php | 7 +- src/Mailgun/Messages/MessageBuilder.php | 22 +-- 4 files changed, 50 insertions(+), 170 deletions(-) diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 530ae3e..d4fda82 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -16,16 +16,12 @@ use Mailgun\Connection\Exceptions\MissingEndpoint; class RestClient{ private $apiKey; - protected $workingDomain; - protected $debugMode; protected $mgClient; - public function __construct($apiKey, $workingDomain, $debugMode = false){ + public function __construct($apiKey, $apiEndpoint){ $this->apiKey = $apiKey; - $this->workingDomain = $workingDomain; - $this->debugMode = $debugMode; - $this->mgClient = new Guzzle('https://' . API_ENDPOINT . '/' . API_VERSION . '/'); + $this->mgClient = new Guzzle('https://' . $apiEndpoint . '/' . API_VERSION . '/'); $this->mgClient->setDefaultOption('curl.options', array('CURLOPT_FORBID_REUSE' => true)); $this->mgClient->setDefaultOption('auth', array (API_USER, $this->apiKey)); $this->mgClient->setDefaultOption('exceptions', false); @@ -102,10 +98,6 @@ class RestClient{ $result->http_response_code = $httpResponeCode; return $result; } - - public function returnWorkingDomain(){ - return $this->workingDomain; - } } ?> \ No newline at end of file diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index 5c4be6d..6545182 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -2,164 +2,45 @@ namespace Mailgun; -use Mailgun\Logs\Logs; -use Mailgun\Stats\Stats; -use Mailgun\Lists\Lists; -use Mailgun\Routes\Routes; -use Mailgun\Bounces\Bounces; -use Mailgun\Address\Address; -use Mailgun\Messages\Messages; -use Mailgun\Campaigns\Campaigns; -use Mailgun\Complaints\Complaints; use Mailgun\Connection\RestClient; -use Mailgun\Unsubscribes\Unsubscribes; + +use Mailgun\Messages\Messages; +use Mailgun\Messages\BatchMessage; +use Mailgun\Messages\MessageBuilder; class Mailgun{ - - /* - * Instantiate the RestClient to make it available to all - * classes created from here. - */ - + private $apiKey; protected $workingDomain; protected $restClient; - protected $debugMode; - public function __construct($apiKey = null, $workingDomain = null, $debugMode = false){ - if(isset($apiKey) && isset($workingDomain)){ - $this->restClient = new RestClient($apiKey, $workingDomain, $debugMode); - } - else{ - $this->apiKey = $apiKey; - $this->workingDomain = $workingDomain; - } + public function __construct($apiKey = null, $apiEndpoint = "api.mailgun.net"){ + $this->restClient = new RestClient($apiKey, $apiEndpoint); } + + public function post($endpointUrl, $postData = array(), $files = array()){ + return $this->restClient->postRequest($endpointUrl, $postData, $files); + } + + public function get($endpointUrl, $queryString = array()){ + return $this->restClient->getRequest($endpointUrl, $queryString); + } + + public function delete($endpointUrl){ + return $this->restClient->getRequest($endpointUrl); + } + + public function put($endpointUrl, $putData){ + return $this->restClient->putRequest($endpointUrl, $putData); + } - private function InstantiateNewClient($workingDomain, $apiKey){ - if(isset($workingDomain) || isset($apiKey)){ - if(isset($workingDomain)){ - $this->workingDomain = $workingDomain; - } - if(isset($apiKey)){ - $this->apiKey = $apiKey; - } - return true; - } - if(isset($this->restClient)){ - return false; - } - else{ - throw new Exception("A valid set of credentials is required to work with this endpoint."); - } - } - - /* - * Factory methods for instantiating each endpoint class. - * If a new endpoint is added, create a factory method here. - * Each endpoint can accept a new domain and API key if you want to - * switch accounts or domains after instantiating the MailgunClient. - * This is for future support of RBAC. - */ - - public function Messages($workingDomain = null, $apiKey = null){ - if($this->InstantiateNewClient($workingDomain, $apiKey)){ - $newClient = new RestClient($this->apiKey, $this->workingDomain, $this->debugMode); - return new Messages($newClient); - } - else{ - return new Messages($this->restClient); - } - } - - public function Unsubscribes($workingDomain = null, $apiKey = null){ - if($this->InstantiateNewClient($workingDomain, $apiKey)){ - $newClient = new RestClient($this->apiKey, $this->workingDomain, $this->debugMode); - return new Unsubscribes($newClient); - } - else{ - return new Unsubscribes($this->restClient); - } - } - - public function Complaints($workingDomain = null, $apiKey = null){ - if($this->InstantiateNewClient($workingDomain, $apiKey)){ - $newClient = new RestClient($this->apiKey, $this->workingDomain, $this->debugMode); - return new Complaints($newClient); - } - else{ - return new Complaints($this->restClient); - } - } - - public function Bounces($workingDomain = null, $apiKey = null){ - if($this->InstantiateNewClient($workingDomain, $apiKey)){ - $newClient = new RestClient($this->apiKey, $this->workingDomain, $this->debugMode); - return new Bounces($newClient); - } - else{ - return new Bounces($this->restClient); - } - } - - public function Stats($workingDomain = null, $apiKey = null){ - if($this->InstantiateNewClient($workingDomain, $apiKey)){ - $newClient = new RestClient($this->apiKey, $this->workingDomain, $this->debugMode); - return new Stats($newClient); - } - else{ - return new Stats($this->restClient); - } - } - - public function Logs($workingDomain = null, $apiKey = null){ - if($this->InstantiateNewClient($workingDomain, $apiKey)){ - $newClient = new RestClient($this->apiKey, $this->workingDomain, $this->debugMode); - return new Logs($newClient); - } - else{ - return new Logs($this->restClient); - } - } - - public function Routes($workingDomain = null, $apiKey = null){ - if($this->InstantiateNewClient($workingDomain, $apiKey)){ - $newClient = new RestClient($this->apiKey, $this->workingDomain, $this->debugMode); - return new Routes($newClient); - } - else{ - return new Routes($this->restClient); - } - } - - public function Campaigns($workingDomain = null, $apiKey = null){ - if($this->InstantiateNewClient($workingDomain, $apiKey)){ - $newClient = new RestClient($this->apiKey, $this->workingDomain, $this->debugMode); - return new Campaigns($newClient); - } - else{ - return new Campaigns($this->restClient); - } - } - - public function Lists($workingDomain = null, $apiKey = null){ - if($this->InstantiateNewClient($workingDomain, $apiKey)){ - $newClient = new RestClient($this->apiKey, $this->workingDomain, $this->debugMode); - return new Lists($newClient); - } - else{ - return new Lists($this->restClient); - } - } - public function Address($workingDomain = null, $apiKey = null){ - if($this->InstantiateNewClient($workingDomain, $apiKey)){ - $newClient = new RestClient($this->apiKey, $this->workingDomain, $this->debugMode); - return new Address($newClient); - } - else{ - return new Address($this->restClient); - } - } + public function MessageBuilder(){ + return new MessageBuilder(); + } + + public function BatchMessage($autoSend = true){ + return new BatchMessage($this->restClient, $autoSend); + } } ?> \ No newline at end of file diff --git a/src/Mailgun/Messages/BatchMessage.php b/src/Mailgun/Messages/BatchMessage.php index 35475f5..81e5fe8 100644 --- a/src/Mailgun/Messages/BatchMessage.php +++ b/src/Mailgun/Messages/BatchMessage.php @@ -6,18 +6,19 @@ use Mailgun\Messages\Exceptions\TooManyParameters; use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters; -class BatchMessage extends MessageBuilder{ +class BatchMessage{ protected $batchRecipientAttributes; protected $autoSend; + protected $restClient; public function __construct($restClient, $autoSend){ - parent::__construct($restClient); $this->batchRecipientAttributes = array(); $this->autoSend = $autoSend; + $this->restClient = $restClient; } - public function addBatchRecipient($address, $attributes){ + public function addToRecipient($address, $attributes){ //Check for maximum recipient count if($this->toRecipientCount == 1000){ //If autoSend is off, do things here. diff --git a/src/Mailgun/Messages/MessageBuilder.php b/src/Mailgun/Messages/MessageBuilder.php index 83ec7f3..8408511 100644 --- a/src/Mailgun/Messages/MessageBuilder.php +++ b/src/Mailgun/Messages/MessageBuilder.php @@ -6,7 +6,7 @@ use Mailgun\Messages\Expcetions\InvalidParameter; use Mailgun\Messages\Exceptions\TooManyParameters; use Mailgun\Messages\Expcetions\InvalidParameterType; -class MessageBuilder extends Messages{ +class MessageBuilder{ protected $message = array(); protected $files = array(); @@ -18,11 +18,10 @@ class MessageBuilder extends Messages{ protected $campaignIdCount = 0; protected $customOptionCount = 0; protected $tagCount = 0; - protected $restClient; - public function __construct($restClient){ - parent::__construct($restClient); - $this->restClient = $restClient; + + public function __get($name){ + return $this->message; } public function addToRecipient($address, $attributes){ @@ -318,10 +317,17 @@ class MessageBuilder extends Messages{ } } - public function getMessage(){ - return $this->message; + public function addCustomParameter($parameterName, $data){ + if(isset($this->message[$parameterName])){ + array_push($this->message[$parameterName], $data); + return true; + } + else{ + $this->message[$parameterName] = array($data); + return true; + } } - + public function getFiles(){ return $this->files; }