Removal of all classes. No OOP. :(

This commit is contained in:
Travis Swientek 2013-08-07 17:41:14 -07:00
parent 1ff4357810
commit 1b3720a339
4 changed files with 50 additions and 170 deletions

View File

@ -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;
}
}
?>

View File

@ -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);
}
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.");
}
}
public function post($endpointUrl, $postData = array(), $files = array()){
return $this->restClient->postRequest($endpointUrl, $postData, $files);
}
/*
* 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 get($endpointUrl, $queryString = array()){
return $this->restClient->getRequest($endpointUrl, $queryString);
}
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 delete($endpointUrl){
return $this->restClient->getRequest($endpointUrl);
}
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 put($endpointUrl, $putData){
return $this->restClient->putRequest($endpointUrl, $putData);
}
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 MessageBuilder(){
return new MessageBuilder();
}
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 BatchMessage($autoSend = true){
return new BatchMessage($this->restClient, $autoSend);
}
}
?>

View File

@ -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.

View File

@ -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,8 +317,15 @@ 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(){