diff options
author | beardyman <nornholdj@gmail.com> | 2015-10-10 10:18:55 -0400 |
---|---|---|
committer | beardyman <nornholdj@gmail.com> | 2015-10-10 10:18:55 -0400 |
commit | 20301cd311307b8357459a299c3c9746ecf8c065 (patch) | |
tree | 7c0bef42eb7e25f90eebf04e1bd8b91f9ce9e5da /lib | |
parent | 4b8a1370698581fece4d5ec188851031e41dcad0 (diff) | |
parent | 7c39c1e35afb32845859289e8a0cd8eaceece5f2 (diff) | |
download | php-sparkpost-20301cd311307b8357459a299c3c9746ecf8c065.zip php-sparkpost-20301cd311307b8357459a299c3c9746ecf8c065.tar.gz php-sparkpost-20301cd311307b8357459a299c3c9746ecf8c065.tar.bz2 |
Merged master
Diffstat (limited to 'lib')
-rw-r--r-- | lib/SendGridCompatibility/Email.php | 388 | ||||
-rw-r--r-- | lib/SendGridCompatibility/SendGrid.php | 32 | ||||
-rw-r--r-- | lib/SparkPost/APIResource.php | 410 | ||||
-rw-r--r-- | lib/SparkPost/SparkPost.php | 181 | ||||
-rw-r--r-- | lib/SparkPost/Transmission.php | 198 |
5 files changed, 636 insertions, 573 deletions
diff --git a/lib/SendGridCompatibility/Email.php b/lib/SendGridCompatibility/Email.php index 5351324..251b7f2 100644 --- a/lib/SendGridCompatibility/Email.php +++ b/lib/SendGridCompatibility/Email.php @@ -2,198 +2,198 @@ namespace SparkPost\SendGridCompatibility; class Email { - public $model; - - - /** - * @desc Sets up the model for saving the configuration - */ - public function __construct() { - $this->model = array(); - } - - /** - * @desc adds addresses as recipients - * @param string $address - * @param string $name optional - * @return \SparkPost\SendGridCompatibility\Email - */ - public function addTo($address, $name = null) { - if (!isset($this->model['recipients'])) { - $this->model['recipients'] = array(); - } - - if(isset($name)) { - $address = array('address'=>array('email'=>$address, 'name'=>$name)); - } else { - $address = array('address'=>array('email'=>$address)); - } - - array_push($this->model['recipients'], $address); - return $this; - } - - /** - * @desc explicitly sets a list of addresses - * @param array $addresses - * @return \SparkPost\SendGridCompatibility\Email - */ - public function setTos(array $addresses) { - $this->model['recipients'] = $addresses; - return $this; - } - - /** - * @desc sets the from address - * @param string $address - * @return \MessageSystems\SendGridCompatibility\Email - */ - public function setFrom($address) { - $this->model['from'] = array('email' => $address); - return $this; - } - - /** - * @desc sets the name for the from address - * @param string $name - */ - public function setFromName($name) { - if(!isset($this->model['from'])){ - throw new \Exception('Must set "From" prior to setting "From Name".'); - } - $this->model['from']['name'] = $name; - return $this; - } - - /** - * @desc sets the reply to field - * @param string $address - * @return \MessageSystems\SendGridCompatibility\Email - */ - public function setReplyTo ($address) { - $this->model['replyTo'] = $address; - return $this; - } - - /** - * @desc throws an error because bcc fields are not yet implemented. - * @throws \Exception - * @param string $address - * @return \MessageSystems\SendGridCompatibility\Email - */ - public function addBcc($address) { - throw new \Exception('Adding bcc recipients is not yet supported, try adding them as a "to" address'); - } - - /** - * @desc sets the subject header - * @param string $subject - * @return \SparkPost\SendGridCompatibility\Email - */ - public function setSubject($subject) { - $this->model['subject'] = $subject; - return $this; - } - - /** - * @desc sets the text body - * @param string $text - * @return \SparkPost\SendGridCompatibility\Email - */ - public function setText($text) { - $this->model['text'] = $text; - return $this; - } - - /** - * @desc sets the html body - * @param string $html - * @return \SparkPost\SendGridCompatibility\Email - */ - public function setHtml($html) { - $this->model['html'] = $html; - return $this; - } - - /** - * @desc Throws an exception since adding categories is not yet supported - * @throws \Exception - * @param string $category - * @throws \Exception - */ - public function addCategory($category) { - throw new \Exception('Adding categories is not yet supported'); - } - - /** - * @desc Throws an exception since adding attachments is not yet supported - * @throws Exception - * @param mixed $attachment - */ - public function addAttachment($attachment) { - throw new \Exception('Adding attachments is not yet supported'); - } - - /** - * @desc Adds transmission level substitution data - * @param string $name - * @param mixed $values - * @return \SparkPost\SendGridCompatibility\Email - */ - public function addSubstitution($name, $values) { - if (!isset($this->model['substitutionData'])) { - $this->model['substitutionData'] = array(); - } - $this->model['substitutionData'][$name] = $values; - - return $this; - } - - /** - * @desc Adds transmission level substitution data - * @param string $name - * @param mixed $values - */ - public function addSection($name, $values) { - $this->addSubstitution($name, $values); - } - - /** - * @desc Throws an exception because arguments for third party systems is not supported - * @throws Exception - * @param mixed $value - */ - public function addUniqueArg($key, $value) { - throw new \Exception('Adding Unique Arguments is not yet supported'); - } - - /** - * @desc Throws an exception because arguments for third party systems is not supported - * @throws Exception - * @param mixed $values - */ - public function setUniqueArgs(array $values) { - throw new \Exception('Setting Unique Arguments is not yet supported'); - } - - /** - * @desc Adds custom headers to the email header - * @param string $name - * @param string $value - */ - public function addHeader($name, $value) { - if (!isset($this->model['customHeaders'])) { - $this->model['customHeaders'] = array(); - } - $this->model['customHeaders'][$name] = $value; - } - - /** - * @desc converts this object to a configuration for a SparkPost transmission - * @return array - */ - public function toSparkPostTransmission() { - return $this->model; - } + public $model; + + + /** + * @desc Sets up the model for saving the configuration + */ + public function __construct() { + $this->model = array(); + } + + /** + * @desc adds addresses as recipients + * @param string $address + * @param string $name optional + * @return \SparkPost\SendGridCompatibility\Email + */ + public function addTo($address, $name = null) { + if (!isset($this->model['recipients'])) { + $this->model['recipients'] = array(); + } + + if(isset($name)) { + $address = array('address'=>array('email'=>$address, 'name'=>$name)); + } else { + $address = array('address'=>array('email'=>$address)); + } + + array_push($this->model['recipients'], $address); + return $this; + } + + /** + * @desc explicitly sets a list of addresses + * @param array $addresses + * @return \SparkPost\SendGridCompatibility\Email + */ + public function setTos(array $addresses) { + $this->model['recipients'] = $addresses; + return $this; + } + + /** + * @desc sets the from address + * @param string $address + * @return \MessageSystems\SendGridCompatibility\Email + */ + public function setFrom($address) { + $this->model['from'] = array('email' => $address); + return $this; + } + + /** + * @desc sets the name for the from address + * @param string $name + */ + public function setFromName($name) { + if(!isset($this->model['from'])){ + throw new \Exception('Must set \'From\' prior to setting \'From Name\'.'); + } + $this->model['from']['name'] = $name; + return $this; + } + + /** + * @desc sets the reply to field + * @param string $address + * @return \MessageSystems\SendGridCompatibility\Email + */ + public function setReplyTo ($address) { + $this->model['replyTo'] = $address; + return $this; + } + + /** + * @desc throws an error because bcc fields are not yet implemented. + * @throws \Exception + * @param string $address + * @return \MessageSystems\SendGridCompatibility\Email + */ + public function addBcc($address) { + throw new \Exception('Adding bcc recipients is not yet supported, try adding them as a \'to\' address'); + } + + /** + * @desc sets the subject header + * @param string $subject + * @return \SparkPost\SendGridCompatibility\Email + */ + public function setSubject($subject) { + $this->model['subject'] = $subject; + return $this; + } + + /** + * @desc sets the text body + * @param string $text + * @return \SparkPost\SendGridCompatibility\Email + */ + public function setText($text) { + $this->model['text'] = $text; + return $this; + } + + /** + * @desc sets the html body + * @param string $html + * @return \SparkPost\SendGridCompatibility\Email + */ + public function setHtml($html) { + $this->model['html'] = $html; + return $this; + } + + /** + * @desc Throws an exception since adding categories is not yet supported + * @throws \Exception + * @param string $category + * @throws \Exception + */ + public function addCategory($category) { + throw new \Exception('Adding categories is not yet supported'); + } + + /** + * @desc Throws an exception since adding attachments is not yet supported + * @throws Exception + * @param mixed $attachment + */ + public function addAttachment($attachment) { + throw new \Exception('Adding attachments is not yet supported'); + } + + /** + * @desc Adds transmission level substitution data + * @param string $name + * @param mixed $values + * @return \SparkPost\SendGridCompatibility\Email + */ + public function addSubstitution($name, $values) { + if (!isset($this->model['substitutionData'])) { + $this->model['substitutionData'] = array(); + } + $this->model['substitutionData'][$name] = $values; + + return $this; + } + + /** + * @desc Adds transmission level substitution data + * @param string $name + * @param mixed $values + */ + public function addSection($name, $values) { + $this->addSubstitution($name, $values); + } + + /** + * @desc Throws an exception because arguments for third party systems is not supported + * @throws Exception + * @param mixed $value + */ + public function addUniqueArg($key, $value) { + throw new \Exception('Adding Unique Arguments is not yet supported'); + } + + /** + * @desc Throws an exception because arguments for third party systems is not supported + * @throws Exception + * @param mixed $values + */ + public function setUniqueArgs(array $values) { + throw new \Exception('Setting Unique Arguments is not yet supported'); + } + + /** + * @desc Adds custom headers to the email header + * @param string $name + * @param string $value + */ + public function addHeader($name, $value) { + if (!isset($this->model['customHeaders'])) { + $this->model['customHeaders'] = array(); + } + $this->model['customHeaders'][$name] = $value; + } + + /** + * @desc converts this object to a configuration for a SparkPost transmission + * @return array + */ + public function toSparkPostTransmission() { + return $this->model; + } } -?>
\ No newline at end of file +?> diff --git a/lib/SendGridCompatibility/SendGrid.php b/lib/SendGridCompatibility/SendGrid.php index a85337a..7f8697a 100644 --- a/lib/SendGridCompatibility/SendGrid.php +++ b/lib/SendGridCompatibility/SendGrid.php @@ -1,22 +1,24 @@ <?php namespace SparkPost\SendGridCompatibility; -use SparkPost\Transmission; +use SparkPost\SparkPost; use SparkPost\SendGridCompatibility\Email; -use SparkPost\Configuration; class SendGrid{ - public function __construct($username, $password, $options = null) { - //username isn't used in our system - $opts = array('key'=>$password); - if (!is_null($options)) { - $opts = array_merge($opts, $options); - } - Configuration::setConfig($opts); - } - - public function send(Email $email) { - Trasmission::send($email->toSparkPostTransmission()); - } + private $sparky; + + public function __construct($username, $password, $options = null, $httpAdapter) { + //username isn't used in our system + $opts = array('key'=>$password); + if (!is_null($options)) { + $opts = array_merge($opts, $options); + } + + $this->sparky = new SparkPost($httpAdapter, $opts); + } + + public function send(Email $email) { + $this->sparky->transmission->send($email->toSparkPostTransmission()); + } } -?>
\ No newline at end of file +?> diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php index 469ba7e..a2cd5a6 100644 --- a/lib/SparkPost/APIResource.php +++ b/lib/SparkPost/APIResource.php @@ -1,216 +1,210 @@ <?php namespace SparkPost; -use Guzzle\Http\Client; -use Guzzle\Http\Exception\ClientErrorResponseException; +use Ivory\HttpAdapter\HttpAdapterException; +use SparkPost\SparkPost; /** * @desc SDK interface for managing SparkPost API endpoints */ class APIResource { - - /** - * @desc name of the API endpoint, mainly used for URL construction. - * @var string - */ - public static $endpoint; - - /** - * @desc singleton holder to create a guzzle http client - * @var \GuzzleHttp\Client - */ - protected static $request; - - /** - * @desc Mapping for values passed into the send method to the values needed for the respective API - * @var array - */ - protected static $parameterMappings = array(); - - /** - * @desc Sets up default structure and default values for the model that is acceptable by the API - * @var array - */ - protected static $structure = array(); - - /** - * @desc Ensure that this class cannot be instansiated - */ - private function __construct() {} - - /** - * @desc Creates and returns a guzzle http client. - * @return \GuzzleHttp\Client - */ - protected static function getHttpClient() { - if(!isset(self::$request)) { - self::$request = new Client(); - } - return self::$request; - } - - /** - * @desc Private Method helper to get the configuration values to create the base url for the current API endpoint - * - * @return string base url for the transmissions API - */ - protected static function getBaseUrl($config) { - $baseUrl = '/api/' . $config['version'] . '/' . static::$endpoint; - return $config['protocol'] . '://' . $config['host'] . ($config['port'] ? ':' . $config['port'] : '') . $baseUrl; - } - - - /** - * @desc Private Method helper to reference parameter mappings and set the right value for the right parameter - */ - protected static function setMappedValue (&$model, $mapKey, $value) { - //get mapping - if( empty(static::$parameterMappings) ) { - // if parameterMappings is empty we can assume that no wrapper is defined - // for the current endpoint and we will use the mapKey to define the mappings directly - $mapPath = $mapKey; - }elseif(array_key_exists($mapKey, static::$parameterMappings)) { - // use only defined parameter mappings to construct $model - $mapPath = static::$parameterMappings[$mapKey]; - } else { - return; - } - - $path = explode('.', $mapPath); - $temp = &$model; - foreach( $path as $key ) { - if( !isset($temp[$key]) ){ - $temp[$key] = null; - } - $temp = &$temp[$key]; - } - $temp = $value; - - } - - protected static function buildRequestModel( $requestConfig, $model=array() ) { - foreach($requestConfig as $key=>$value) { - self::setMappedValue($model, $key, $value); - } - return $model; - } - - /** - * @desc Method for issuing POST requests - * - * @return array API repsonse represented as key-value pairs - */ - public static function sendRequest( $requestConfig ) { - $hostConfig = SparkPost::getConfig(); - $request = self::getHttpClient(); - - //create model from $transmissionConfig - $model = static::$structure; - $requestModel = self::buildRequestModel( $requestConfig, $model ); - - //send the request - try { - $response = $request->post( - self::getBaseUrl($hostConfig), - array('authorization' => $hostConfig['key']), - json_encode($requestModel), - array("verify"=>$hostConfig['strictSSL']) - )->send(); - - return $response->json(); - } - /* - * Handles 4XX responses - */ - catch (ClientErrorResponseException $exception) { - $response = $exception->getResponse(); - $responseArray = $response->json(); - throw new \Exception(json_encode($responseArray['errors'])); - } - /* - * Handles 5XX Errors, Configuration Errors, and a catch all for other errors - */ - catch (\Exception $exception) { - throw new \Exception("Unable to contact ".ucfirst(static::$endpoint)." API: ". $exception->getMessage()); - } - } - - - /** - * @desc Wrapper method for issuing GET request to current API endpoint - * - * @param string $resourcePath (optional) string resource path of specific resource - * @param array $options (optional) query string parameters - * @return array Result set of transmissions found - */ - public static function fetchResource( $resourcePath=null, $options=array() ) { - return self::callResource( 'get', $resourcePath, $options ); - } - - /** - * @desc Wrapper method for issuing DELETE request to current API endpoint - * - * @param string $resourcePath (optional) string resource path of specific resource - * @param array $options (optional) query string parameters - * @return array Result set of transmissions found - */ - public static function deleteResource( $resourcePath=null, $options=array() ) { - return self::callResource( 'delete', $resourcePath, $options ); - } - - /** - * @desc Private Method for issuing GET and DELETE request to current API endpoint - * - * This method is responsible for getting the collection _and_ - * a specific entity from the API endpoint - * - * If resourcePath parameter is omitted, then we fetch the collection - * - * @param string $action HTTP method type - * @param string $resourcePath (optional) string resource path of specific resource - * @param array $options (optional) query string parameters - * @return array Result set of action performed on resource - */ - private static function callResource( $action, $resourcePath=null, $options=array() ) { - - if( !in_array( $action, array('get', 'delete') ) ) throw new \Exception('Invalid resource action'); - - //build the url - $hostConfig = SparkPost::getConfig(); - $url = self::getBaseUrl($hostConfig); - if (!is_null($resourcePath)){ - $url .= '/'.$resourcePath; - } - - // untested: - if( !empty($options) ) { - $queryString = http_build_query($options); - $url .= '?'.$queryString; - } - - $request = self::getHttpClient(); - - //make request - try { - $response = $request->{$action}($url, array('authorization' => $hostConfig['key']), array("verify"=>$hostConfig['strictSSL']))->send(); - return $response->json(); - } - /* - * Handles 4XX responses - */ - catch (ClientErrorResponseException $exception) { - $response = $exception->getResponse(); - $statusCode = $response->getStatusCode(); - if($statusCode === 404) { - throw new \Exception("The specified resource does not exist", 404); - } - throw new \Exception("Received bad response from ".ucfirst(static::$endpoint)." API: ". $statusCode ); - } - /* - * Handles 5XX Errors, Configuration Errors, and a catch all for other errors - */ - catch (\Exception $exception) { - throw new \Exception("Unable to contact ".ucfirst(static::$endpoint)." API: ". $exception->getMessage()); - } - } - + + /** + * @desc name of the API endpoint, mainly used for URL construction. + * This is public to provide an interface + * + * @var string + */ + public $endpoint; + + /** + * @desc Mapping for values passed into the send method to the values needed for the respective API + * @var array + */ + protected static $parameterMappings = []; + + /** + * @desc Sets up default structure and default values for the model that is acceptable by the API + * @var array + */ + protected static $structure = []; + + /** + * @desc SparkPost reference for httpAdapters and configs + */ + protected $sparkpost; + + /** + * @desc Initializes config and httpAdapter for use later. + * @param $sparkpost SparkPost\SparkPost provides api configuration information + */ + public function __construct(SparkPost $sparkpost) { + $this->sparkpost = $sparkpost; + } + + /** + * @desc Private Method helper to reference parameter mappings and set the right value for the right parameter + * + * @param array $model (pass by reference) the set of values to map + * @param string $mapKey a dot syntax path determining which value to set + * @param mixed $value value for the given path + */ + protected function setMappedValue(&$model, $mapKey, $value) { + //get mapping + if( empty(static::$parameterMappings) ) { + // if parameterMappings is empty we can assume that no wrapper is defined + // for the current endpoint and we will use the mapKey to define the mappings directly + $mapPath = $mapKey; + }elseif(array_key_exists($mapKey, static::$parameterMappings)) { + // use only defined parameter mappings to construct $model + $mapPath = static::$parameterMappings[$mapKey]; + } else { + return; + } + + $path = explode('.', $mapPath); + $temp = &$model; + foreach( $path as $key ) { + if( !isset($temp[$key]) ){ + $temp[$key] = null; + } + $temp = &$temp[$key]; + } + $temp = $value; + + } + + /** + * @desc maps values from the passed in model to those needed for the request + * @param $requestConfig the passed in model + * @param $model the set of defaults + * @return array A model ready for the body of a request + */ + protected function buildRequestModel(Array $requestConfig, Array $model=[] ) { + foreach($requestConfig as $key => $value) { + $this->setMappedValue($model, $key, $value); + } + return $model; + } + + /** + * @desc posts to the api with a supplied body + * @param body post body for the request + * @return array Result of the request + */ + public function create(Array $body=[]) { + return $this->callResource( 'post', null, ['body'=>$body]); + } + + /** + * @desc Makes a put request to the api with a supplied body + * @param body Put body for the request + * @return array Result of the request + */ + public function update( $resourcePath, Array $body=[]) { + return $this->callResource( 'put', $resourcePath, ['body'=>$body]); + } + + /** + * @desc Wrapper method for issuing GET request to current API endpoint + * + * @param string $resourcePath (optional) string resource path of specific resource + * @param array $options (optional) query string parameters + * @return array Result of the request + */ + public function get( $resourcePath=null, Array $query=[] ) { + return $this->callResource( 'get', $resourcePath, ['query'=>$query] ); + } + + /** + * @desc Wrapper method for issuing DELETE request to current API endpoint + * + * @param string $resourcePath (optional) string resource path of specific resource + * @param array $options (optional) query string parameters + * @return array Result of the request + */ + public function delete( $resourcePath=null, Array $query=[] ) { + return $this->callResource( 'delete', $resourcePath, ['query'=>$query] ); + } + + + /** + * @desc assembles a URL for a request + * @param string $resourcePath path after the initial endpoint + * @param array options array with an optional value of query with values to build a querystring from. + * @return string the assembled URL + */ + private function buildUrl($resourcePath, $options) { + $url = join(['/', $this->endpoint, '/']); + if (!is_null($resourcePath)){ + $url .= $resourcePath; + } + + if( !empty($options['query'])) { + $queryString = http_build_query($options['query']); + $url .= '?'.$queryString; + } + + return $url; + } + + + /** + * @desc Prepares a body for put and post requests + * @param array options array with an optional value of body with values to build a request body from. + * @return string|null A json encoded string or null if no body was provided + */ + private function buildBody($options) { + $body = null; + if( !empty($options['body']) ) { + $model = static::$structure; + $requestModel = $this->buildRequestModel( $options['body'], $model ); + $body = json_encode($requestModel); + } + return $body; + } + + + /** + * @desc Private Method for issuing GET and DELETE request to current API endpoint + * + * This method is responsible for getting the collection _and_ + * a specific entity from the API endpoint + * + * If resourcePath parameter is omitted, then we fetch the collection + * + * @param string $action HTTP method type + * @param string $resourcePath (optional) string resource path of specific resource + * @param array $options (optional) query string parameters + * @return array Result set of action performed on resource + */ + private function callResource( $action, $resourcePath=null, $options=[] ) { + $action = strtoupper($action); // normalize + + $url = $this->buildUrl($resourcePath, $options); + $body = $this->buildBody($options); + + //make request + try { + $response = $this->sparkpost->httpAdapter->send($url, $action, $this->sparkpost->getHttpHeaders(), $body); + return json_decode($response->getBody()->getContents(), true); + } + /* + * Handles 4XX responses + */ + catch (HttpAdapterException $exception) { + $response = $exception->getResponse(); + $statusCode = $response->getStatusCode(); + if($statusCode === 404) { + throw new \Exception('The specified resource does not exist', 404); + } + throw new \Exception('Received bad response from '.ucfirst($this->endpoint).' API: '. $statusCode ); + } + /* + * Handles 5XX Errors, Configuration Errors, and a catch all for other errors + */ + catch (\Exception $exception) { + throw new \Exception('Unable to contact '.ucfirst($this->endpoint).' API: '. $exception->getMessage()); + } + } + } diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php index a9035f9..d4449b8 100644 --- a/lib/SparkPost/SparkPost.php +++ b/lib/SparkPost/SparkPost.php @@ -1,66 +1,137 @@ <?php namespace SparkPost; +use Ivory\HttpAdapter\Configuration; +use Ivory\HttpAdapter\HttpAdapterInterface; class SparkPost { - private static $config; - private static $defaults = array( - 'host'=>'api.sparkpost.com', - 'protocol'=>'https', - 'port'=>443, - 'strictSSL'=>true, - 'key'=>'', - 'version'=>'v1' - ); - - /** - * Enforce that this object can't be instansiated - */ - private function __construct(){} - - /** - * Allows the user to pass in values to override the defaults and set their API key - * @param String | Array $config - if a string it is an api key - * If an array, is should contain config values for the SDK to connect to SparkPost - * @throws \Exception - */ - public static function setConfig($config) { + public $transmission; + + /** + * @dec connection config for making requests. + */ + private $config; + + /** + * @desc Ivory\HttpAdapter\HttpAdapterInterface to make requests through. + */ + public $httpAdapter; + + /** + * @desc Default config values. Passed in values will override these. + */ + private static $apiDefaults = [ + 'host'=>'api.sparkpost.com', + 'protocol'=>'https', + 'port'=>443, + 'strictSSL'=>true, + 'key'=>'', + 'version'=>'v1' + ]; + + /** + * @desc sets up httpAdapter and config + * + * Sets up instances of sub libraries. + * + * @param Ivory\HttpAdapter $httpAdapter - An adapter for making http requests + * @param String | Array $settingsConfig - Hashmap that contains config values + * for the SDK to connect to SparkPost. If its a string we assume that + * its just they API Key. + */ + public function __construct($httpAdapter, $settingsConfig) { // if the config map is a string we should assume that its an api key - if (gettype($config) === 'string') { - $config = ['key'=>$config]; + if (gettype($settingsConfig) === 'string') { + $settingsConfig = ['key'=>$settingsConfig]; + } + + //config needs to be setup before adapter because of default adapter settings + $this->setConfig($settingsConfig); + $this->setHttpAdapter($httpAdapter); + + $this->transmission = new Transmission($this); + } + + + + /** + * Creates an unwrapped api interface for endpoints that aren't yet supported. + * The new resource is attached to this object as well as returned + * @return SparkPost\APIResource - the unwrapped resource + */ + public function setupUnwrapped ($endpoint) { + $this->{$endpoint} = new APIResource($this); + $this->{$endpoint}->endpoint = $endpoint; + + return $this->{$endpoint}; + } + + /** + * @desc Merges passed in headers with default headers for http requests + */ + public function getHttpHeaders() { + $defaultOptions = [ + 'Authorization' => $this->config['key'], + 'Content-Type' => 'application/json', + ]; + + return $defaultOptions; + } + + + /** + * @desc Helper function for getting the configuration for http requests + * @return \Ivory\HttpAdapter\Configuration + */ + private function getHttpConfig($config) { + // get composer.json to extract version number + $composerFile = file_get_contents(dirname(__FILE__) . '/../../composer.json'); + $composer = json_decode($composerFile, true); + + // create Configuration for http adapter + $httpConfig = new Configuration(); + $baseUrl = $config['protocol'] . '://' . $config['host'] . ($config['port'] ? ':' . $config['port'] : '') . '/api/' . $config['version']; + $httpConfig->setBaseUri($baseUrl); + $httpConfig->setUserAgent('php-sparkpost/' . $composer['version']); + return $httpConfig; + } + + + /** + * @desc Validates and sets up the httpAdapter + * @param $httpAdapter Ivory\HttpAdapter\HttpAdapterInterface to make requests through. + * @throws \Exception + */ + public function setHttpAdapter($httpAdapter) { + if (!$httpAdapter instanceOf HttpAdapterInterface) { + throw new \Exception('$httpAdapter paramter must be a valid Ivory\HttpAdapter'); } - //check for API key because its required - if (isset($config['key'])){ - $key = trim($config['key']); - if(empty($key)){ - throw new \Exception('You must provide an API key'); - } - } else { - throw new \Exception('You must provide an API key'); - } - self::$config = self::$defaults; - foreach ($config as $configOption => $configValue) { - if(key_exists($configOption, self::$config)) { - self::$config[$configOption] = $configValue; - } - } - } - - /** - * Retrieves the configuration that was previously setup by the user - * @throws \Exception - */ - public static function getConfig() { - if (self::$config === null) { - throw new \Exception('No configuration has been provided'); - } - return self::$config; - } - - public static function unsetConfig() { - self::$config = NULL; - } + $this->httpAdapter = $httpAdapter; + $this->httpAdapter->setConfiguration($this->getHttpConfig($this->config)); + } + + + /** + * Allows the user to pass in values to override the defaults and set their API key + * @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost + * @throws \Exception + */ + public function setConfig(Array $settingsConfig) { + // Validate API key because its required + if (!isset($settingsConfig['key']) || empty(trim($settingsConfig['key']))){ + throw new \Exception('You must provide an API key'); + } + + $this->config = self::$apiDefaults; + + // set config, overriding defaults + foreach ($settingsConfig as $configOption => $configValue) { + if(key_exists($configOption, $this->config)) { + $this->config[$configOption] = $configValue; + } + } + } } ?> diff --git a/lib/SparkPost/Transmission.php b/lib/SparkPost/Transmission.php index 4e54f1d..613ec12 100644 --- a/lib/SparkPost/Transmission.php +++ b/lib/SparkPost/Transmission.php @@ -7,106 +7,102 @@ use Guzzle\Http\Exception\ClientErrorResponseException; * @desc SDK interface for managing transmissions */ class Transmission extends APIResource { - - public static $endpoint = 'transmissions'; - - /** - * @desc Mapping for values passed into the send method to the values needed for the Transmission API - * @var array - */ - protected static $parameterMappings = array( - 'campaign'=>'campaign_id', - 'metadata'=>'metadata', - 'substitutionData'=>'substitution_data', - 'description'=>'description', - 'returnPath'=>'return_path', - 'replyTo'=>'content.reply_to', - 'subject'=>'content.subject', - 'from'=>'content.from', - 'html'=>'content.html', - 'text'=>'content.text', - 'rfc822'=>'content.email_rfc822', - 'customHeaders'=>'content.headers', - 'recipients'=>'recipients', - 'recipientList'=>'recipients.list_id', - 'template'=>'content.template_id', - 'trackOpens'=>'options.open_tracking', - 'trackClicks'=>'options.click_tracking', - 'useDraftTemplate'=>'use_draft_template' - ); - - /** - * @desc Sets up default structure and default values for the model that is acceptable by the API - * @var array - */ - protected static $structure = array( - 'return_path'=>"default@sparkpostmail.com", - 'content'=>array( - 'html'=>null, - 'text'=>null, - 'email_rfc822'=>null - ), - 'use_draft_template'=>false - ); - - /** - * @desc Method for issuing POST request to the Transmissions API - * - * This method assumes that all the appropriate fields have - * been populated by the user through configuration. Acceptable - * configuration values are: - * 'campaign': string, - * 'metadata': array, - * 'substitutionData': array, - * 'description': string, - * 'replyTo': string, - * 'subject': string, - * 'from': string, - * 'html': string, - * 'text': string, - * 'rfc822': string, - * 'customHeaders': array, - * 'recipients': array, - * 'recipientList': string, - * 'template': string, - * 'trackOpens': boolean, - * 'trackClicks': boolean, - * 'useDraftTemplate': boolean - * - * @return array API repsonse represented as key-value pairs - */ - public static function send( $transmissionConfig ) { - return self::sendRequest( $transmissionConfig ); - } - - /** - * @desc Method for retrieving information about all transmissions - * Wrapper method for a cleaner interface - * - * @return array result Set of transmissions - */ - public static function all( $campaignID=null, $templateID=null ) { - $options = array(); - if( $campaignID !== NULL ) $options['campaign_id'] = $campaignID; - if( $templateID !== NULL ) $options['template_id'] = $templateID; - - return self::fetchResource( null, $options ); - } - - /** - * @desc Method for retrieving information about a single transmission - * Wrapper method for a cleaner interface - * - * @param string $transmissionID Identifier of the transmission to be found - * @return array result Single transmission represented in key-value pairs - */ - public static function find($transmissionID) { - return self::fetchResource($transmissionID); - } - - public static function delete( $transmissionID ) { - return self::deleteResource($transmissionID); - } + + public $endpoint = 'transmissions'; + + /** + * @desc Mapping for values passed into the send method to the values needed for the Transmission API + * @var array + */ + protected static $parameterMappings = [ + 'campaign'=>'campaign_id', + 'metadata'=>'metadata', + 'substitutionData'=>'substitution_data', + 'description'=>'description', + 'returnPath'=>'return_path', + 'replyTo'=>'content.reply_to', + 'subject'=>'content.subject', + 'from'=>'content.from', + 'html'=>'content.html', + 'text'=>'content.text', + 'rfc822'=>'content.email_rfc822', + 'customHeaders'=>'content.headers', + 'recipients'=>'recipients', + 'recipientList'=>'recipients.list_id', + 'template'=>'content.template_id', + 'trackOpens'=>'options.open_tracking', + 'trackClicks'=>'options.click_tracking', + 'useDraftTemplate'=>'use_draft_template' + ]; + + /** + * @desc Sets up default structure and default values for the model that is acceptable by the API + * @var array + */ + protected static $structure = [ + 'return_path'=>'default@sparkpostmail.com', + 'content'=>[ + 'html'=>null, + 'text'=>null, + 'email_rfc822'=>null + ], + 'use_draft_template'=>false + ]; + + /** + * @desc Method for issuing POST request to the Transmissions API + * + * This method assumes that all the appropriate fields have + * been populated by the user through configuration. Acceptable + * configuration values are: + * 'campaign': string, + * 'metadata': array, + * 'substitutionData': array, + * 'description': string, + * 'replyTo': string, + * 'subject': string, + * 'from': string, + * 'html': string, + * 'text': string, + * 'rfc822': string, + * 'customHeaders': array, + * 'recipients': array, + * 'recipientList': string, + * 'template': string, + * 'trackOpens': boolean, + * 'trackClicks': boolean, + * 'useDraftTemplate': boolean + * + * @return array API repsonse represented as key-value pairs + */ + public function send( $transmissionConfig ) { + return $this->create( $transmissionConfig ); + } + + /** + * @desc Method for retrieving information about all transmissions + * Wrapper method for a cleaner interface + * + * @return array result Set of transmissions + */ + public function all( $campaignID=null, $templateID=null ) { + $options = []; + if( $campaignID !== NULL ) $options['campaign_id'] = $campaignID; + if( $templateID !== NULL ) $options['template_id'] = $templateID; + + return $this->get( null, $options ); + } + + /** + * @desc Method for retrieving information about a single transmission + * Wrapper method for a cleaner interface + * + * @param string $transmissionID Identifier of the transmission to be found + * @return array result Single transmission represented in key-value pairs + */ + public function find($transmissionID) { + return $this->get($transmissionID); + } } -?>
\ No newline at end of file +?> |