@@ -40,6 +40,7 @@ class SparkPost
4040 'key ' => '' ,
4141 'version ' => 'v1 ' ,
4242 'async ' => true ,
43+ 'debug ' => false
4344 ];
4445
4546 /**
@@ -93,11 +94,13 @@ public function request($method = 'GET', $uri = '', $payload = [], $headers = []
9394 */
9495 public function syncRequest ($ method = 'GET ' , $ uri = '' , $ payload = [], $ headers = [])
9596 {
96- $ request = $ this ->buildRequest ($ method , $ uri , $ payload , $ headers );
97+ $ requestValues = $ this ->buildRequestValues ($ method , $ uri , $ payload , $ headers );
98+ $ request = $ this ->buildRequest ($ requestValues );
99+
97100 try {
98- return new SparkPostResponse ($ this ->httpClient ->sendRequest ($ request ));
101+ return new SparkPostResponse ($ this ->httpClient ->sendRequest ($ request ), $ this -> ifDebug ( $ requestValues ) );
99102 } catch (\Exception $ exception ) {
100- throw new SparkPostException ($ exception );
103+ throw new SparkPostException ($ exception, $ this -> ifDebug ( $ requestValues ) );
101104 }
102105 }
103106
@@ -114,25 +117,26 @@ public function syncRequest($method = 'GET', $uri = '', $payload = [], $headers
114117 public function asyncRequest ($ method = 'GET ' , $ uri = '' , $ payload = [], $ headers = [])
115118 {
116119 if ($ this ->httpClient instanceof HttpAsyncClient) {
117- $ request = $ this ->buildRequest ($ method , $ uri , $ payload , $ headers );
120+ $ requestValues = $ this ->buildRequestValues ($ method , $ uri , $ payload , $ headers );
121+ $ request = call_user_func_array (array ($ this , 'buildRequest ' ), $ requestValues );
118122
119- return new SparkPostPromise ($ this ->httpClient ->sendAsyncRequest ($ request ));
123+ return new SparkPostPromise ($ this ->httpClient ->sendAsyncRequest ($ request ), $ this -> ifDebug ( $ requestValues ) );
120124 } else {
121125 throw new \Exception ('Your http client does not support asynchronous requests. Please use a different client or use synchronous requests. ' );
122126 }
123127 }
124128
125129 /**
126- * Builds request from given params.
130+ * Builds request values from given params.
127131 *
128132 * @param string $method
129133 * @param string $uri
130134 * @param array $payload
131135 * @param array $headers
132136 *
133- * @return RequestInterface
137+ * @return array $requestValues
134138 */
135- public function buildRequest ($ method , $ uri , $ payload , $ headers )
139+ public function buildRequestValues ($ method , $ uri , $ payload , $ headers )
136140 {
137141 $ method = trim (strtoupper ($ method ));
138142
@@ -153,7 +157,23 @@ public function buildRequest($method, $uri, $payload, $headers)
153157 ];
154158 $ body = strtr (json_encode ($ body ), $ jsonReplace );
155159
156- return $ this ->getMessageFactory ()->createRequest ($ method , $ url , $ headers , $ body );
160+ return [
161+ 'method ' => $ method ,
162+ 'url ' => $ url ,
163+ 'headers ' => $ headers ,
164+ 'body ' => $ body
165+ ];
166+ }
167+
168+ /**
169+ * Build RequestInterface from given params
170+ *
171+ * @param array $requestValues
172+ * @return RequestInterface
173+ */
174+ public function buildRequest ($ method , $ uri , $ headers , $ body )
175+ {
176+ return $ this ->getMessageFactory ()->createRequest ($ method , $ uri , $ headers , $ body );
157177 }
158178
159179 /**
@@ -245,6 +265,17 @@ public function setOptions($options)
245265 }
246266 }
247267
268+ /**
269+ * Returns the given value if debugging, an empty instance otherwise
270+ *
271+ * @param any $param
272+ * @return any $param
273+ */
274+ private function ifDebug ($ param )
275+ {
276+ return $ this ->options ['debug ' ] ? $ param : null ;
277+ }
278+
248279 /**
249280 * Sets up any endpoints to custom classes e.g. $this->transmissions.
250281 */
0 commit comments