diff options
author | Avi Goldman <avrahamymgoldman@gmail.com> | 2016-06-22 11:44:41 -0400 |
---|---|---|
committer | Avi Goldman <avrahamymgoldman@gmail.com> | 2016-06-22 11:44:41 -0400 |
commit | d7f6b4b22e569ed0da339a9228f4202304fe7a05 (patch) | |
tree | 8f4da3d82e2b3eca9d60131ec768f3a5065382b9 /lib/SparkPost | |
parent | 9f86fd39e85e7d3736b9b5c7ea5e95f5129773f4 (diff) | |
download | php-sparkpost-d7f6b4b22e569ed0da339a9228f4202304fe7a05.zip php-sparkpost-d7f6b4b22e569ed0da339a9228f4202304fe7a05.tar.gz php-sparkpost-d7f6b4b22e569ed0da339a9228f4202304fe7a05.tar.bz2 |
formatting and comments
Diffstat (limited to 'lib/SparkPost')
-rw-r--r-- | lib/SparkPost/Resource.php | 21 | ||||
-rw-r--r-- | lib/SparkPost/SparkPostException.php | 2 | ||||
-rw-r--r-- | lib/SparkPost/SparkPostPromise.php | 11 | ||||
-rw-r--r-- | lib/SparkPost/SparkPostResponse.php | 6 | ||||
-rw-r--r-- | lib/SparkPost/Transmission.php | 69 |
5 files changed, 54 insertions, 55 deletions
diff --git a/lib/SparkPost/Resource.php b/lib/SparkPost/Resource.php index bc6f78d..7fab937 100644 --- a/lib/SparkPost/Resource.php +++ b/lib/SparkPost/Resource.php @@ -5,17 +5,17 @@ namespace SparkPost; class Resource { /** - * SparkPost object used to make requests + * SparkPost object used to make requests. */ protected $sparkpost; /** - * The api endpoint that gets prepended to all requests send through this resource + * The api endpoint that gets prepended to all requests send through this resource. */ protected $endpoint; /** - * Sets up the Resource + * Sets up the Resource. * * @param SparKPost $sparkpost - the sparkpost instance that this resource is attached to * @param string $endpoint - the endpoint that this resource wraps @@ -27,7 +27,7 @@ class Resource } /** - * Sends get request to API at the set endpoint + * Sends get request to API at the set endpoint. * * @see SparkPost->request() */ @@ -37,7 +37,7 @@ class Resource } /** - * Sends put request to API at the set endpoint + * Sends put request to API at the set endpoint. * * @see SparkPost->request() */ @@ -47,7 +47,7 @@ class Resource } /** - * Sends post request to API at the set endpoint + * Sends post request to API at the set endpoint. * * @see SparkPost->request() */ @@ -57,7 +57,7 @@ class Resource } /** - * Sends delete request to API at the set endpoint + * Sends delete request to API at the set endpoint. * * @see SparkPost->request() */ @@ -67,7 +67,7 @@ class Resource } /** - * Sends requests to SparkPost object to the resource endpoint + * Sends requests to SparkPost object to the resource endpoint. * * @see SparkPost->request() * @@ -75,7 +75,6 @@ class Resource */ public function request($method = 'GET', $uri = '', $payload = [], $headers = []) { - if (is_array($uri)) { $headers = $payload; $payload = $uri; @@ -83,7 +82,7 @@ class Resource } $uri = $this->endpoint.'/'.$uri; - + return $this->sparkpost->request($method, $uri, $payload, $headers); } -}
\ No newline at end of file +} diff --git a/lib/SparkPost/SparkPostException.php b/lib/SparkPost/SparkPostException.php index f1e3146..ca92e24 100644 --- a/lib/SparkPost/SparkPostException.php +++ b/lib/SparkPost/SparkPostException.php @@ -38,4 +38,4 @@ class SparkPostException extends \Exception { return $this->body; } -}
\ No newline at end of file +} diff --git a/lib/SparkPost/SparkPostPromise.php b/lib/SparkPost/SparkPostPromise.php index 15f129f..df715d5 100644 --- a/lib/SparkPost/SparkPostPromise.php +++ b/lib/SparkPost/SparkPostPromise.php @@ -29,12 +29,14 @@ class SparkPostPromise implements HttpPromise */ public function then(callable $onFulfilled = null, callable $onRejected = null) { - return $this->promise->then(function($response) use ($onFulfilled) { - if (isset($onFulfilled)) + return $this->promise->then(function ($response) use ($onFulfilled) { + if (isset($onFulfilled)) { $onFulfilled(new SparkPostResponse($response)); - }, function($exception) use ($onRejected) { - if (isset($onRejected)) + } + }, function ($exception) use ($onRejected) { + if (isset($onRejected)) { $onRejected(new SparkPostException($exception)); + } }); } @@ -61,6 +63,7 @@ class SparkPostPromise implements HttpPromise { try { $response = $this->promise->wait($unwrap); + return $response ? new SparkPostResponse($response) : $response; } catch (\Exception $exception) { throw new SparkPostException($exception); diff --git a/lib/SparkPost/SparkPostResponse.php b/lib/SparkPost/SparkPostResponse.php index 9104b33..08750f9 100644 --- a/lib/SparkPost/SparkPostResponse.php +++ b/lib/SparkPost/SparkPostResponse.php @@ -34,10 +34,12 @@ class SparkPostResponse implements ResponseInterface $json = json_decode($body_string, true); - return $json; + return $json; } - // pass these down to the response given in the constructor + /** + * pass these down to the response given in the constructor + */ public function getProtocolVersion() { return $this->response->getProtocolVersion(); diff --git a/lib/SparkPost/Transmission.php b/lib/SparkPost/Transmission.php index f136d5b..1847e1b 100644 --- a/lib/SparkPost/Transmission.php +++ b/lib/SparkPost/Transmission.php @@ -10,24 +10,26 @@ class Transmission extends Resource } /** - * Send post request to transmission endpoint after formatting cc, bcc, and expanding the shorthand emails + * Send post request to transmission endpoint after formatting cc, bcc, and expanding the shorthand emails. * * @return SparkPostPromise or SparkPostResponse depending on sync or async request */ public function post($payload = [], $headers = []) { $payload = $this->formatPayload($payload); + return parent::post($payload, $headers); } /** - * Runs the given payload through the formatting functions + * Runs the given payload through the formatting functions. * * @param array $payload - the request body * * @return array - the modified request body */ - public function formatPayload($payload) { + public function formatPayload($payload) + { $payload = $this->formatBlindCarbonCopy($payload); //Fixes BCCs into payload $payload = $this->formatCarbonCopy($payload); //Fixes CCs into payload $payload = $this->formatShorthandRecipients($payload); //Fixes shorthand recipients format @@ -36,7 +38,7 @@ class Transmission extends Resource } /** - * Formats bcc list into recipients list + * Formats bcc list into recipients list. * * @param array $payload - the request body * @@ -44,9 +46,9 @@ class Transmission extends Resource */ private function formatBlindCarbonCopy($payload) { - + //If there's a list of BCC recipients, move then into the correct format - if(isset($payload['bcc'])) { + if (isset($payload['bcc'])) { $payload = $this->addListToRecipients($payload, 'bcc'); } @@ -54,7 +56,7 @@ class Transmission extends Resource } /** - * Formats cc list into recipients list and adds the CC header to the content + * Formats cc list into recipients list and adds the CC header to the content. * * @param array $payload - the request body * @@ -62,9 +64,9 @@ class Transmission extends Resource */ private function formatCarbonCopy($payload) { - if(isset($payload['cc'])) { + if (isset($payload['cc'])) { $ccAddresses = []; - for ($i = 0; $i < count($payload['cc']); $i++) { + for ($i = 0; $i < count($payload['cc']); ++$i) { array_push($ccAddresses, $this->toAddressString($payload['cc'][$i]['address'])); } @@ -80,7 +82,7 @@ class Transmission extends Resource } /** - * Formats all recipients into the long form of [ "name" => "John", "email" => "john@exmmple.com" ] + * Formats all recipients into the long form of [ "name" => "John", "email" => "john@exmmple.com" ]. * * @param array $payload - the request body * @@ -88,10 +90,9 @@ class Transmission extends Resource */ private function formatShorthandRecipients($payload) { - $payload['content']['from'] = $this->toAddressObject($payload['content']['from']); - - for($i = 0; $i < count($payload['recipients']); $i++) { + + for ($i = 0; $i < count($payload['recipients']); ++$i) { $payload['recipients'][$i]['address'] = $this->toAddressObject($payload['recipients'][$i]['address']); } @@ -99,7 +100,7 @@ class Transmission extends Resource } /** - * Loops through the given listName in the payload and adds all the recipients to the recipients list after removing their names + * Loops through the given listName in the payload and adds all the recipients to the recipients list after removing their names. * * @param array $payload - the request body * @param array $listName - the name of the array in the payload to be moved to the recipients list @@ -112,14 +113,15 @@ class Transmission extends Resource foreach ($payload[$listName] as $recipient) { $recipient['address'] = $this->toAddressObject($recipient['address']); $recipient['address']['header_to'] = $originalAddress; - + // remove name from address - name is only put in the header for cc and not at all for bcc - if (isset($recipient['address']['name'])) + if (isset($recipient['address']['name'])) { unset($recipient['address']['name']); + } array_push($payload['recipients'], $recipient); } - + //Delete the original object from the payload. unset($payload[$listName]); @@ -127,7 +129,7 @@ class Transmission extends Resource } /** - * Takes the shorthand form of an email address and converts it to the long form + * Takes the shorthand form of an email address and converts it to the long form. * * @param $address - the shorthand form of an email address "Name <Email address>" * @@ -141,37 +143,32 @@ class Transmission extends Resource if ($this->isEmail($address)) { $return['email'] = $address; - } - else if(preg_match('/"?(.[^"]+)"?\s*<(.+)>/', $address, $matches)) { + } elseif (preg_match('/"?(.[^"]*)?"?\s*<(.+)>/', $address, $matches)) { $name = trim($matches[1]); $return['name'] = $matches[1]; $return['email'] = $matches[2]; - } - else { + } else { throw new \Exception('Invalid address format: '.$address); } - } return $return; } /** - * Takes the longhand form of an email address and converts it to the shorthand form + * Takes the longhand form of an email address and converts it to the shorthand form. * * @param $address - the longhand form of an email address [ "name" => "John", "email" => "john@exmmple.com" ] - * * @param string - the shorthand form of an email address "Name <Email address>" */ private function toAddressString($address) { // convert object to string - if(!is_string($address)) { + if (!is_string($address)) { if (isset($address['name'])) { - $address = '"' . $address['name'] . '" <' . $address['email'] . '>'; - } - else { - $address = $address['email']; + $address = '"'.$address['name'].'" <'.$address['email'].'>'; + } else { + $address = $address['email']; } } @@ -179,19 +176,17 @@ class Transmission extends Resource } /** - * Checks if a string is an email + * Checks if a string is an email. * * @param string $email - a string that might be an email address - * - * @param boolean - true if the given string is an email + * @param bool - true if the given string is an email */ - private function isEmail($email){ - if(filter_var($email, FILTER_VALIDATE_EMAIL)){ + private function isEmail($email) + { + if (filter_var($email, FILTER_VALIDATE_EMAIL)) { return true; } else { return false; } } } - -?>
\ No newline at end of file |