6
6
/**
7
7
* @desc SDK interface for managing transmissions
8
8
*/
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 ' ;
15
12
16
13
/**
17
14
* @desc Mapping for values passed into the send method to the values needed for the Transmission API
18
15
* @var array
19
16
*/
20
- private static $ parameterMappings = array (
17
+ protected static $ parameterMappings = array (
21
18
'campaign ' =>'campaign_id ' ,
22
19
'metadata ' =>'metadata ' ,
23
20
'substitutionData ' =>'substitution_data ' ,
@@ -42,7 +39,7 @@ class Transmission {
42
39
* @desc Sets up default structure and default values for the model that is acceptable by the API
43
40
* @var array
44
41
*/
45
- private static $ structure = array (
42
+ protected static $ structure = array (
46
43
'return_path ' =>"default@sparkpostmail.com " ,
47
44
'content ' =>array (
48
45
'html ' =>null ,
@@ -52,48 +49,6 @@ class Transmission {
52
49
'use_draft_template ' =>false
53
50
);
54
51
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
-
97
52
/**
98
53
* @desc Method for issuing POST request to the Transmissions API
99
54
*
@@ -120,80 +75,8 @@ private static function getBaseUrl($config) {
120
75
*
121
76
* @return array API repsonse represented as key-value pairs
122
77
*/
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 );
197
80
}
198
81
199
82
/**
@@ -202,8 +85,12 @@ private static function fetch ($transmissionID = null) {
202
85
*
203
86
* @return array result Set of transmissions
204
87
*/
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 );
207
94
}
208
95
209
96
/**
@@ -214,7 +101,11 @@ public static function all() {
214
101
* @return array result Single transmission represented in key-value pairs
215
102
*/
216
103
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 );
218
109
}
219
110
}
220
111
0 commit comments