summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/MessageSystems/SparkPost.php51
-rw-r--r--lib/MessageSystems/Transmission.php216
2 files changed, 267 insertions, 0 deletions
diff --git a/lib/MessageSystems/SparkPost.php b/lib/MessageSystems/SparkPost.php
new file mode 100644
index 0000000..b1ad20a
--- /dev/null
+++ b/lib/MessageSystems/SparkPost.php
@@ -0,0 +1,51 @@
+<?php
+namespace MessageSystems;
+
+class SparkPost {
+
+ private static $config;
+ private static $defaults = [
+ '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 Array $configMap - Hashmap that contains config values for the SDK to connect to SparkPost
+ * @throws \Exception
+ */
+ public static function setConfig(array $configMap) {
+ //check for API key because its required
+ if (!isset($configMap['key']) || empty(trim($configMap['key']))){
+ throw new \Exception('You must provide an API key');
+ }
+ self::$config = self::$defaults;
+ foreach ($configMap 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;
+ }
+}
+
+?> \ No newline at end of file
diff --git a/lib/MessageSystems/Transmission.php b/lib/MessageSystems/Transmission.php
new file mode 100644
index 0000000..e820cff
--- /dev/null
+++ b/lib/MessageSystems/Transmission.php
@@ -0,0 +1,216 @@
+<?php
+namespace MessageSystems;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\RequestException;
+
+/**
+ * @desc SDK interface for managing transmissions
+ */
+class Transmission {
+ /**
+ * @desc singleton holder to create a guzzle http client
+ * @var \GuzzleHttp\Client
+ */
+ private static $request;
+
+ /**
+ * @desc Mapping for values passed into the send method to the values needed for the Transmission API
+ * @var array
+ */
+ private 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',
+ 'rfc822Part'=>'content.email_rfc822',
+ 'headers'=>'content.headers',
+ 'recipients'=>'recipients',
+ 'recipientList'=>'recipients.list_id',
+ 'template'=>'content.template_id',
+ 'openTracking'=>'options.open_tracking',
+ 'clickTracking'=>'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
+ */
+ private static $structure = [
+ 'return_path'=>"default@sparkpostmail.com",
+ 'content'=>[
+ 'html'=>null,
+ 'text'=>null,
+ 'email_rfc822'=>null
+ ],
+ 'options'=>[
+ 'open_tracking'=>true,
+ 'click_tracking'=>true
+ ],
+ 'use_draft_template'=>false
+ ];
+
+ /**
+ * @desc Ensure that this class cannot be instansiated
+ */
+ private function __construct() {}
+
+ /**
+ * @desc Creates and returns a guzzle http client.
+ * @return \GuzzleHttp\Client
+ */
+ private static function getHttpClient() {
+ if(!isset(self::$request)) {
+ self::$request = new Client();
+ }
+ return self::$request;
+ }
+
+
+ /**
+ * @desc Private Method helper to reference parameter mappings and set the right value for the right parameter
+ */
+ private static function setMappedValue (&$model, $mapKey, $value) {
+ //get mapping
+ if(array_key_exists($mapKey, self::$parameterMappings)) {
+ $temp = &$model;
+ $path = explode('.', self::$parameterMappings[$mapKey]);
+ foreach( $path as $key ) {
+ $temp = &$temp[$key];
+ }
+ $temp = $value;
+ } //ignore anything we don't have a mapping for
+ }
+
+ /**
+ * @desc Private Method helper to get the configuration values to create the base url for the transmissions API
+ *
+ * @return string base url for the transmissions API
+ */
+ private static function getBaseUrl($config) {
+ $baseUrl = '/api/' . $config['version'] . '/transmissions';
+ return $config['protocol'] . '://' . $config['host'] . ($config['port'] ? ':' . $config['port'] : '') . $baseUrl;
+ }
+
+ /**
+ * @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,
+ * 'rfc822Part': string,
+ * 'headers': array,
+ * 'recipients': array,
+ * 'recipientList': string,
+ * 'template': string,
+ * 'openTracking': boolean,
+ * 'clickTracking': boolean,
+ * 'useDraftTemplate': boolean
+ *
+ * @return array API repsonse represented as key-value pairs
+ */
+ public static function send($transmissionConfig) {
+ $hostConfig = SparkPost::getConfig();
+ $request = self::getHttpClient();
+
+ //create model from $transmissionConfig
+ $model = self::$structure;
+ foreach($transmissionConfig as $key=>$value) {
+ self::setMappedValue($model, $key, $value);
+ }
+
+ //send the request
+ try {
+ $response = $request->post(self::getBaseUrl($hostConfig), [
+ 'json'=>$model,
+ "headers"=>['authorization' => $hostConfig['key']],
+ "verify"=>$hostConfig['strictSSL']
+ ]);
+ return $response->json();
+ } catch (RequestException $exception) {
+ $response = $exception->getResponse();
+ throw new \Exception(json_encode($response->json()['errors']));
+ } catch (\Exception $exception) {
+ throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage());
+ }
+
+ }
+
+ /**
+ * @desc Private Method for issuing GET request to Transmissions API
+ *
+ * This method is responsible for getting the collection _and_
+ * a specific entity from the Transmissions API
+ *
+ * If TransmissionID parameter is omitted, then we fetch the collection
+ *
+ * @param string $transmissionID (optional) string Transmission ID of specific Transmission to retrieve
+ * @return array Result set of transmissions found
+ */
+ private static function fetch ($transmissionID = null) {
+ //figure out the url
+ $hostConfig = SparkPost::getConfig();
+ $url = self::getBaseUrl($hostConfig);
+ if (!is_null($transmissionID)){
+ $url .= '/'.$transmissionID;
+ }
+
+ $request = self::getHttpClient();
+
+ //make request
+ try {
+ $response = $request->get($url, [
+ "headers"=>['authorization' => $hostConfig['key']],
+ "verify"=>$hostConfig['strictSSL']
+ ]);
+ return $response->json();
+ } catch (RequestException $exception) {
+ $response = $exception->getResponse();
+ if($response->getStatusCode() === '404') {
+ throw new \Exception("The specified Transmission ID does not exist", 404);
+ } else {
+ throw new \Exception("Received bad response from Transmission API: ". $response->getStatusCode());
+ }
+ } catch (\Exception $exception) {
+ throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage());
+ }
+ }
+
+ /**
+ * @desc Method for retrieving information about all transmissions
+ * Wrapper method for a cleaner interface
+ *
+ * @return array result Set of transmissions
+ */
+ public static function all() {
+ return self::fetch();
+ }
+
+ /**
+ * @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::fetch($transmissionID);
+ }
+}
+
+?> \ No newline at end of file