Skip to content

Commit 893adea

Browse files
author
mapril
committed
refactored code base for extensibility
1 parent 3fb7f60 commit 893adea

File tree

2 files changed

+20
-129
lines changed

2 files changed

+20
-129
lines changed

lib/SparkPost/Transmission.php

Lines changed: 18 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66
/**
77
* @desc SDK interface for managing transmissions
88
*/
9-
class Transmission {
10-
/**
11-
* @desc singleton holder to create a guzzle http client
12-
* @var \GuzzleHttp\Client
13-
*/
14-
private static $request;
9+
class Transmission extends APIResource {
10+
11+
public static $endpoint = 'transmissions';
1512

1613
/**
1714
* @desc Mapping for values passed into the send method to the values needed for the Transmission API
1815
* @var array
1916
*/
20-
private static $parameterMappings = array(
17+
protected static $parameterMappings = array(
2118
'campaign'=>'campaign_id',
2219
'metadata'=>'metadata',
2320
'substitutionData'=>'substitution_data',
@@ -42,7 +39,7 @@ class Transmission {
4239
* @desc Sets up default structure and default values for the model that is acceptable by the API
4340
* @var array
4441
*/
45-
private static $structure = array(
42+
protected static $structure = array(
4643
'return_path'=>"default@sparkpostmail.com",
4744
'content'=>array(
4845
'html'=>null,
@@ -52,48 +49,6 @@ class Transmission {
5249
'use_draft_template'=>false
5350
);
5451

55-
/**
56-
* @desc Ensure that this class cannot be instansiated
57-
*/
58-
private function __construct() {}
59-
60-
/**
61-
* @desc Creates and returns a guzzle http client.
62-
* @return \GuzzleHttp\Client
63-
*/
64-
private static function getHttpClient() {
65-
if(!isset(self::$request)) {
66-
self::$request = new Client();
67-
}
68-
return self::$request;
69-
}
70-
71-
72-
/**
73-
* @desc Private Method helper to reference parameter mappings and set the right value for the right parameter
74-
*/
75-
private static function setMappedValue (&$model, $mapKey, $value) {
76-
//get mapping
77-
if(array_key_exists($mapKey, self::$parameterMappings)) {
78-
$temp = &$model;
79-
$path = explode('.', self::$parameterMappings[$mapKey]);
80-
foreach( $path as $key ) {
81-
$temp = &$temp[$key];
82-
}
83-
$temp = $value;
84-
} //ignore anything we don't have a mapping for
85-
}
86-
87-
/**
88-
* @desc Private Method helper to get the configuration values to create the base url for the transmissions API
89-
*
90-
* @return string base url for the transmissions API
91-
*/
92-
private static function getBaseUrl($config) {
93-
$baseUrl = '/api/' . $config['version'] . '/transmissions';
94-
return $config['protocol'] . '://' . $config['host'] . ($config['port'] ? ':' . $config['port'] : '') . $baseUrl;
95-
}
96-
9752
/**
9853
* @desc Method for issuing POST request to the Transmissions API
9954
*
@@ -120,80 +75,8 @@ private static function getBaseUrl($config) {
12075
*
12176
* @return array API repsonse represented as key-value pairs
12277
*/
123-
public static function send($transmissionConfig) {
124-
$hostConfig = SparkPost::getConfig();
125-
$request = self::getHttpClient();
126-
127-
//create model from $transmissionConfig
128-
$model = self::$structure;
129-
foreach($transmissionConfig as $key=>$value) {
130-
self::setMappedValue($model, $key, $value);
131-
}
132-
133-
//send the request
134-
try {
135-
$response = $request->post(self::getBaseUrl($hostConfig), array('authorization' => $hostConfig['key']), json_encode($model), array("verify"=>$hostConfig['strictSSL']))->send();
136-
return $response->json();
137-
}
138-
/*
139-
* Handles 4XX responses
140-
*/
141-
catch (ClientErrorResponseException $exception) {
142-
$response = $exception->getResponse();
143-
$responseArray = $response->json();
144-
throw new \Exception(json_encode($responseArray['errors']));
145-
}
146-
/*
147-
* Handles 5XX Errors, Configuration Errors, and a catch all for other errors
148-
*/
149-
catch (\Exception $exception) {
150-
throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage());
151-
}
152-
}
153-
154-
/**
155-
* @desc Private Method for issuing GET request to Transmissions API
156-
*
157-
* This method is responsible for getting the collection _and_
158-
* a specific entity from the Transmissions API
159-
*
160-
* If TransmissionID parameter is omitted, then we fetch the collection
161-
*
162-
* @param string $transmissionID (optional) string Transmission ID of specific Transmission to retrieve
163-
* @return array Result set of transmissions found
164-
*/
165-
private static function fetch ($transmissionID = null) {
166-
//figure out the url
167-
$hostConfig = SparkPost::getConfig();
168-
$url = self::getBaseUrl($hostConfig);
169-
if (!is_null($transmissionID)){
170-
$url .= '/'.$transmissionID;
171-
}
172-
173-
$request = self::getHttpClient();
174-
175-
//make request
176-
try {
177-
$response = $request->get($url, array('authorization' => $hostConfig['key']), array("verify"=>$hostConfig['strictSSL']))->send();
178-
return $response->json();
179-
}
180-
/*
181-
* Handles 4XX responses
182-
*/
183-
catch (ClientErrorResponseException $exception) {
184-
$response = $exception->getResponse();
185-
$statusCode = $response->getStatusCode();
186-
if($statusCode === 404) {
187-
throw new \Exception("The specified Transmission ID does not exist", 404);
188-
}
189-
throw new \Exception("Received bad response from Transmission API: ". $statusCode);
190-
}
191-
/*
192-
* Handles 5XX Errors, Configuration Errors, and a catch all for other errors
193-
*/
194-
catch (\Exception $exception) {
195-
throw new \Exception('Unable to contact Transmissions API: '. $exception->getMessage());
196-
}
78+
public static function send( $transmissionConfig ) {
79+
return self::sendRequest( $transmissionConfig );
19780
}
19881

19982
/**
@@ -202,8 +85,12 @@ private static function fetch ($transmissionID = null) {
20285
*
20386
* @return array result Set of transmissions
20487
*/
205-
public static function all() {
206-
return self::fetch();
88+
public static function all( $campaignID=null, $templateID=null ) {
89+
$options = array();
90+
if( $campaignID !== NULL ) $options['campaign_id'] = $campaignID;
91+
if( $templateID !== NULL ) $options['template_id'] = $templateID;
92+
93+
return self::fetchResource( null, $options );
20794
}
20895

20996
/**
@@ -214,7 +101,11 @@ public static function all() {
214101
* @return array result Single transmission represented in key-value pairs
215102
*/
216103
public static function find($transmissionID) {
217-
return self::fetch($transmissionID);
104+
return self::fetchResource($transmissionID);
105+
}
106+
107+
public static function delete( $transmissionID ) {
108+
return self::deleteResource($transmissionID);
218109
}
219110
}
220111

test/unit/TransmissionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function testFindWithGoodResponse() {
6868
/**
6969
* @desc tests 404 bad response
7070
* @expectedException Exception
71-
* @expectedExceptionMessage The specified Transmission ID does not exist
71+
* @expectedExceptionMessage The specified resource does not exist
7272
*/
7373
public function testFindWith404Response() {
7474
$mock = new MockPlugin();
@@ -80,7 +80,7 @@ public function testFindWith404Response() {
8080
/**
8181
* @desc tests unknown bad response
8282
* @expectedException Exception
83-
* @expectedExceptionMessage Received bad response from Transmission API: 400
83+
* @expectedExceptionMessage Received bad response from Transmissions API: 400
8484
*/
8585
public function testFindWithOtherBadResponse() {
8686
$mock = new MockPlugin();

0 commit comments

Comments
 (0)