Skip to content

Commit d7f6b4b

Browse files
committed
formatting and comments
1 parent 9f86fd3 commit d7f6b4b

8 files changed

+129
-149
lines changed

lib/SparkPost/Resource.php

+10-11
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
class Resource
66
{
77
/**
8-
* SparkPost object used to make requests
8+
* SparkPost object used to make requests.
99
*/
1010
protected $sparkpost;
1111

1212
/**
13-
* The api endpoint that gets prepended to all requests send through this resource
13+
* The api endpoint that gets prepended to all requests send through this resource.
1414
*/
1515
protected $endpoint;
1616

1717
/**
18-
* Sets up the Resource
18+
* Sets up the Resource.
1919
*
2020
* @param SparKPost $sparkpost - the sparkpost instance that this resource is attached to
2121
* @param string $endpoint - the endpoint that this resource wraps
@@ -27,7 +27,7 @@ public function __construct(SparkPost $sparkpost, $endpoint)
2727
}
2828

2929
/**
30-
* Sends get request to API at the set endpoint
30+
* Sends get request to API at the set endpoint.
3131
*
3232
* @see SparkPost->request()
3333
*/
@@ -37,7 +37,7 @@ public function get($uri = '', $payload = [], $headers = [])
3737
}
3838

3939
/**
40-
* Sends put request to API at the set endpoint
40+
* Sends put request to API at the set endpoint.
4141
*
4242
* @see SparkPost->request()
4343
*/
@@ -47,7 +47,7 @@ public function put($uri = '', $payload = [], $headers = [])
4747
}
4848

4949
/**
50-
* Sends post request to API at the set endpoint
50+
* Sends post request to API at the set endpoint.
5151
*
5252
* @see SparkPost->request()
5353
*/
@@ -57,7 +57,7 @@ public function post($payload = [], $headers = [])
5757
}
5858

5959
/**
60-
* Sends delete request to API at the set endpoint
60+
* Sends delete request to API at the set endpoint.
6161
*
6262
* @see SparkPost->request()
6363
*/
@@ -67,23 +67,22 @@ public function delete($uri = '', $payload = [], $headers = [])
6767
}
6868

6969
/**
70-
* Sends requests to SparkPost object to the resource endpoint
70+
* Sends requests to SparkPost object to the resource endpoint.
7171
*
7272
* @see SparkPost->request()
7373
*
7474
* @return SparkPostPromise or SparkPostResponse depending on sync or async request
7575
*/
7676
public function request($method = 'GET', $uri = '', $payload = [], $headers = [])
7777
{
78-
7978
if (is_array($uri)) {
8079
$headers = $payload;
8180
$payload = $uri;
8281
$uri = '';
8382
}
8483

8584
$uri = $this->endpoint.'/'.$uri;
86-
85+
8786
return $this->sparkpost->request($method, $uri, $payload, $headers);
8887
}
89-
}
88+
}

lib/SparkPost/SparkPostException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ public function getBody()
3838
{
3939
return $this->body;
4040
}
41-
}
41+
}

lib/SparkPost/SparkPostPromise.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ public function __construct(HttpPromise $promise)
2929
*/
3030
public function then(callable $onFulfilled = null, callable $onRejected = null)
3131
{
32-
return $this->promise->then(function($response) use ($onFulfilled) {
33-
if (isset($onFulfilled))
32+
return $this->promise->then(function ($response) use ($onFulfilled) {
33+
if (isset($onFulfilled)) {
3434
$onFulfilled(new SparkPostResponse($response));
35-
}, function($exception) use ($onRejected) {
36-
if (isset($onRejected))
35+
}
36+
}, function ($exception) use ($onRejected) {
37+
if (isset($onRejected)) {
3738
$onRejected(new SparkPostException($exception));
39+
}
3840
});
3941
}
4042

@@ -61,6 +63,7 @@ public function wait($unwrap = true)
6163
{
6264
try {
6365
$response = $this->promise->wait($unwrap);
66+
6467
return $response ? new SparkPostResponse($response) : $response;
6568
} catch (\Exception $exception) {
6669
throw new SparkPostException($exception);

lib/SparkPost/SparkPostResponse.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ public function getBody()
3434

3535
$json = json_decode($body_string, true);
3636

37-
return $json;
37+
return $json;
3838
}
3939

40-
// pass these down to the response given in the constructor
40+
/**
41+
* pass these down to the response given in the constructor
42+
*/
4143
public function getProtocolVersion()
4244
{
4345
return $this->response->getProtocolVersion();

lib/SparkPost/Transmission.php

+32-37
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,26 @@ public function __construct(SparkPost $sparkpost)
1010
}
1111

1212
/**
13-
* Send post request to transmission endpoint after formatting cc, bcc, and expanding the shorthand emails
13+
* Send post request to transmission endpoint after formatting cc, bcc, and expanding the shorthand emails.
1414
*
1515
* @return SparkPostPromise or SparkPostResponse depending on sync or async request
1616
*/
1717
public function post($payload = [], $headers = [])
1818
{
1919
$payload = $this->formatPayload($payload);
20+
2021
return parent::post($payload, $headers);
2122
}
2223

2324
/**
24-
* Runs the given payload through the formatting functions
25+
* Runs the given payload through the formatting functions.
2526
*
2627
* @param array $payload - the request body
2728
*
2829
* @return array - the modified request body
2930
*/
30-
public function formatPayload($payload) {
31+
public function formatPayload($payload)
32+
{
3133
$payload = $this->formatBlindCarbonCopy($payload); //Fixes BCCs into payload
3234
$payload = $this->formatCarbonCopy($payload); //Fixes CCs into payload
3335
$payload = $this->formatShorthandRecipients($payload); //Fixes shorthand recipients format
@@ -36,35 +38,35 @@ public function formatPayload($payload) {
3638
}
3739

3840
/**
39-
* Formats bcc list into recipients list
41+
* Formats bcc list into recipients list.
4042
*
4143
* @param array $payload - the request body
4244
*
4345
* @return array - the modified request body
4446
*/
4547
private function formatBlindCarbonCopy($payload)
4648
{
47-
49+
4850
//If there's a list of BCC recipients, move then into the correct format
49-
if(isset($payload['bcc'])) {
51+
if (isset($payload['bcc'])) {
5052
$payload = $this->addListToRecipients($payload, 'bcc');
5153
}
5254

5355
return $payload;
5456
}
5557

5658
/**
57-
* Formats cc list into recipients list and adds the CC header to the content
59+
* Formats cc list into recipients list and adds the CC header to the content.
5860
*
5961
* @param array $payload - the request body
6062
*
6163
* @return array - the modified request body
6264
*/
6365
private function formatCarbonCopy($payload)
6466
{
65-
if(isset($payload['cc'])) {
67+
if (isset($payload['cc'])) {
6668
$ccAddresses = [];
67-
for ($i = 0; $i < count($payload['cc']); $i++) {
69+
for ($i = 0; $i < count($payload['cc']); ++$i) {
6870
array_push($ccAddresses, $this->toAddressString($payload['cc'][$i]['address']));
6971
}
7072

@@ -80,26 +82,25 @@ private function formatCarbonCopy($payload)
8082
}
8183

8284
/**
83-
* Formats all recipients into the long form of [ "name" => "John", "email" => "john@exmmple.com" ]
85+
* Formats all recipients into the long form of [ "name" => "John", "email" => "john@exmmple.com" ].
8486
*
8587
* @param array $payload - the request body
8688
*
8789
* @return array - the modified request body
8890
*/
8991
private function formatShorthandRecipients($payload)
9092
{
91-
9293
$payload['content']['from'] = $this->toAddressObject($payload['content']['from']);
93-
94-
for($i = 0; $i < count($payload['recipients']); $i++) {
94+
95+
for ($i = 0; $i < count($payload['recipients']); ++$i) {
9596
$payload['recipients'][$i]['address'] = $this->toAddressObject($payload['recipients'][$i]['address']);
9697
}
9798

9899
return $payload;
99100
}
100101

101102
/**
102-
* Loops through the given listName in the payload and adds all the recipients to the recipients list after removing their names
103+
* Loops through the given listName in the payload and adds all the recipients to the recipients list after removing their names.
103104
*
104105
* @param array $payload - the request body
105106
* @param array $listName - the name of the array in the payload to be moved to the recipients list
@@ -112,22 +113,23 @@ private function addListToRecipients($payload, $listName)
112113
foreach ($payload[$listName] as $recipient) {
113114
$recipient['address'] = $this->toAddressObject($recipient['address']);
114115
$recipient['address']['header_to'] = $originalAddress;
115-
116+
116117
// remove name from address - name is only put in the header for cc and not at all for bcc
117-
if (isset($recipient['address']['name']))
118+
if (isset($recipient['address']['name'])) {
118119
unset($recipient['address']['name']);
120+
}
119121

120122
array_push($payload['recipients'], $recipient);
121123
}
122-
124+
123125
//Delete the original object from the payload.
124126
unset($payload[$listName]);
125127

126128
return $payload;
127129
}
128130

129131
/**
130-
* Takes the shorthand form of an email address and converts it to the long form
132+
* Takes the shorthand form of an email address and converts it to the long form.
131133
*
132134
* @param $address - the shorthand form of an email address "Name <Email address>"
133135
*
@@ -141,57 +143,50 @@ private function toAddressObject($address)
141143

142144
if ($this->isEmail($address)) {
143145
$return['email'] = $address;
144-
}
145-
else if(preg_match('/"?(.[^"]+)"?\s*<(.+)>/', $address, $matches)) {
146+
} elseif (preg_match('/"?(.[^"]*)?"?\s*<(.+)>/', $address, $matches)) {
146147
$name = trim($matches[1]);
147148
$return['name'] = $matches[1];
148149
$return['email'] = $matches[2];
149-
}
150-
else {
150+
} else {
151151
throw new \Exception('Invalid address format: '.$address);
152152
}
153-
154153
}
155154

156155
return $return;
157156
}
158157

159158
/**
160-
* Takes the longhand form of an email address and converts it to the shorthand form
159+
* Takes the longhand form of an email address and converts it to the shorthand form.
161160
*
162161
* @param $address - the longhand form of an email address [ "name" => "John", "email" => "john@exmmple.com" ]
163-
*
164162
* @param string - the shorthand form of an email address "Name <Email address>"
165163
*/
166164
private function toAddressString($address)
167165
{
168166
// convert object to string
169-
if(!is_string($address)) {
167+
if (!is_string($address)) {
170168
if (isset($address['name'])) {
171-
$address = '"' . $address['name'] . '" <' . $address['email'] . '>';
172-
}
173-
else {
174-
$address = $address['email'];
169+
$address = '"'.$address['name'].'" <'.$address['email'].'>';
170+
} else {
171+
$address = $address['email'];
175172
}
176173
}
177174

178175
return $address;
179176
}
180177

181178
/**
182-
* Checks if a string is an email
179+
* Checks if a string is an email.
183180
*
184181
* @param string $email - a string that might be an email address
185-
*
186-
* @param boolean - true if the given string is an email
182+
* @param bool - true if the given string is an email
187183
*/
188-
private function isEmail($email){
189-
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
184+
private function isEmail($email)
185+
{
186+
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
190187
return true;
191188
} else {
192189
return false;
193190
}
194191
}
195192
}
196-
197-
?>

0 commit comments

Comments
 (0)