Skip to content

Commit 23378d2

Browse files
committed
Merge pull request #43 from djagya/42-fix-namespaces-clean-code
Fixed namespaces, cleaned code/comments - closes #42
2 parents 4330e32 + d01a019 commit 23378d2

File tree

8 files changed

+98
-110
lines changed

8 files changed

+98
-110
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.buildpath
55
test/output/
66
examples/example-config.json
7+
.idea

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "sparkpost/php-sparkpost",
33
"description": "Client library for interfacing with the SparkPost API.",
44
"license": "Apache 2.0",
5-
"version": "1.0.1",
5+
"version": "1.0.2",
66
"authors": [
77
{
88
"name": "Message Systems, Inc."

lib/SendGridCompatibility/Email.php

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
class Email {
55
public $model;
66

7-
87
/**
9-
* @desc Sets up the model for saving the configuration
8+
* Sets up the model for saving the configuration
109
*/
1110
public function __construct() {
1211
$this->model = array();
1312
}
1413

1514
/**
16-
* @desc adds addresses as recipients
15+
* adds addresses as recipients
1716
* @param string $address
1817
* @param string $name optional
19-
* @return \SparkPost\SendGridCompatibility\Email
18+
* @return $this
2019
*/
2120
public function addTo($address, $name = null) {
2221
if (!isset($this->model['recipients'])) {
@@ -34,28 +33,30 @@ public function addTo($address, $name = null) {
3433
}
3534

3635
/**
37-
* @desc explicitly sets a list of addresses
36+
* explicitly sets a list of addresses
3837
* @param array $addresses
39-
* @return \SparkPost\SendGridCompatibility\Email
38+
* @return $this
4039
*/
4140
public function setTos(array $addresses) {
4241
$this->model['recipients'] = $addresses;
4342
return $this;
4443
}
4544

4645
/**
47-
* @desc sets the from address
46+
* sets the from address
4847
* @param string $address
49-
* @return \MessageSystems\SendGridCompatibility\Email
48+
* @return $this
5049
*/
5150
public function setFrom($address) {
5251
$this->model['from'] = array('email' => $address);
5352
return $this;
5453
}
5554

5655
/**
57-
* @desc sets the name for the from address
56+
* Sets the name for the from address
5857
* @param string $name
58+
* @return $this
59+
* @throws \Exception
5960
*/
6061
public function setFromName($name) {
6162
if(!isset($this->model['from'])){
@@ -66,58 +67,57 @@ public function setFromName($name) {
6667
}
6768

6869
/**
69-
* @desc sets the reply to field
70+
* sets the reply to field
7071
* @param string $address
71-
* @return \MessageSystems\SendGridCompatibility\Email
72+
* @return $this
7273
*/
7374
public function setReplyTo ($address) {
7475
$this->model['replyTo'] = $address;
7576
return $this;
7677
}
7778

7879
/**
79-
* @desc throws an error because bcc fields are not yet implemented.
80+
* throws an error because bcc fields are not yet implemented.
8081
* @throws \Exception
8182
* @param string $address
82-
* @return \MessageSystems\SendGridCompatibility\Email
83+
* @return $this
8384
*/
8485
public function addBcc($address) {
8586
throw new \Exception('Adding bcc recipients is not yet supported, try adding them as a \'to\' address');
8687
}
8788

8889
/**
89-
* @desc sets the subject header
90+
* sets the subject header
9091
* @param string $subject
91-
* @return \SparkPost\SendGridCompatibility\Email
92+
* @return $this
9293
*/
9394
public function setSubject($subject) {
9495
$this->model['subject'] = $subject;
9596
return $this;
9697
}
9798

9899
/**
99-
* @desc sets the text body
100+
* sets the text body
100101
* @param string $text
101-
* @return \SparkPost\SendGridCompatibility\Email
102+
* @return $this
102103
*/
103104
public function setText($text) {
104105
$this->model['text'] = $text;
105106
return $this;
106107
}
107108

108109
/**
109-
* @desc sets the html body
110+
* sets the html body
110111
* @param string $html
111-
* @return \SparkPost\SendGridCompatibility\Email
112+
* @return $this
112113
*/
113114
public function setHtml($html) {
114115
$this->model['html'] = $html;
115116
return $this;
116117
}
117118

118119
/**
119-
* @desc Throws an exception since adding categories is not yet supported
120-
* @throws \Exception
120+
* Throws an exception since adding categories is not yet supported
121121
* @param string $category
122122
* @throws \Exception
123123
*/
@@ -126,19 +126,19 @@ public function addCategory($category) {
126126
}
127127

128128
/**
129-
* @desc Throws an exception since adding attachments is not yet supported
130-
* @throws Exception
129+
* Throws an exception since adding attachments is not yet supported
130+
* @throws \Exception
131131
* @param mixed $attachment
132132
*/
133133
public function addAttachment($attachment) {
134134
throw new \Exception('Adding attachments is not yet supported');
135135
}
136136

137137
/**
138-
* @desc Adds transmission level substitution data
138+
* Adds transmission level substitution data
139139
* @param string $name
140140
* @param mixed $values
141-
* @return \SparkPost\SendGridCompatibility\Email
141+
* @return $this
142142
*/
143143
public function addSubstitution($name, $values) {
144144
if (!isset($this->model['substitutionData'])) {
@@ -150,7 +150,7 @@ public function addSubstitution($name, $values) {
150150
}
151151

152152
/**
153-
* @desc Adds transmission level substitution data
153+
* Adds transmission level substitution data
154154
* @param string $name
155155
* @param mixed $values
156156
*/
@@ -159,25 +159,25 @@ public function addSection($name, $values) {
159159
}
160160

161161
/**
162-
* @desc Throws an exception because arguments for third party systems is not supported
163-
* @throws Exception
162+
* Throws an exception because arguments for third party systems is not supported
163+
* @throws \Exception
164164
* @param mixed $value
165165
*/
166166
public function addUniqueArg($key, $value) {
167167
throw new \Exception('Adding Unique Arguments is not yet supported');
168168
}
169169

170170
/**
171-
* @desc Throws an exception because arguments for third party systems is not supported
172-
* @throws Exception
171+
* Throws an exception because arguments for third party systems is not supported
172+
* @throws \Exception
173173
* @param mixed $values
174174
*/
175175
public function setUniqueArgs(array $values) {
176176
throw new \Exception('Setting Unique Arguments is not yet supported');
177177
}
178178

179179
/**
180-
* @desc Adds custom headers to the email header
180+
* Adds custom headers to the email header
181181
* @param string $name
182182
* @param string $value
183183
*/
@@ -189,7 +189,7 @@ public function addHeader($name, $value) {
189189
}
190190

191191
/**
192-
* @desc converts this object to a configuration for a SparkPost transmission
192+
* converts this object to a configuration for a SparkPost transmission
193193
* @return array
194194
*/
195195
public function toSparkPostTransmission() {

lib/SendGridCompatibility/SendGrid.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
namespace SparkPost\SendGridCompatibility;
33

44
use SparkPost\SparkPost;
5-
use SparkPost\SendGridCompatibility\Email;
65

76
class SendGrid{
87
private $sparky;

lib/SparkPost/APIResource.php

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
11
<?php
22
namespace SparkPost;
3-
use Ivory\HttpAdapter\HttpAdapterException;
4-
use SparkPost\SparkPost;
5-
6-
73

84
/**
9-
* @desc SDK interface for managing SparkPost API endpoints
5+
* SDK interface for managing SparkPost API endpoints
106
*/
117
class APIResource {
128

139
/**
14-
* @desc name of the API endpoint, mainly used for URL construction.
10+
* name of the API endpoint, mainly used for URL construction.
1511
* This is public to provide an interface
1612
*
1713
* @var string
1814
*/
1915
public $endpoint;
2016

2117
/**
22-
* @desc Mapping for values passed into the send method to the values needed for the respective API
18+
* Mapping for values passed into the send method to the values needed for the respective API
2319
* @var array
2420
*/
2521
protected static $parameterMappings = [];
2622

2723
/**
28-
* @desc Sets up default structure and default values for the model that is acceptable by the API
24+
* Sets up default structure and default values for the model that is acceptable by the API
2925
* @var array
3026
*/
3127
protected static $structure = [];
3228

3329
/**
34-
* @desc SparkPost reference for httpAdapters and configs
30+
* SparkPost reference for httpAdapters and configs
3531
*/
3632
protected $sparkpost;
3733

3834
/**
39-
* @desc Initializes config and httpAdapter for use later.
40-
* @param $sparkpost SparkPost\SparkPost provides api configuration information
35+
* Initializes config and httpAdapter for use later.
36+
* @param $sparkpost \SparkPost\SparkPost provides api configuration information
4137
*/
4238
public function __construct(SparkPost $sparkpost) {
4339
$this->sparkpost = $sparkpost;
4440
}
4541

4642
/**
47-
* @desc Private Method helper to reference parameter mappings and set the right value for the right parameter
43+
* Private Method helper to reference parameter mappings and set the right value for the right parameter
4844
*
4945
* @param array $model (pass by reference) the set of values to map
5046
* @param string $mapKey a dot syntax path determining which value to set
@@ -76,9 +72,9 @@ protected function setMappedValue(&$model, $mapKey, $value) {
7672
}
7773

7874
/**
79-
* @desc maps values from the passed in model to those needed for the request
80-
* @param $requestConfig the passed in model
81-
* @param $model the set of defaults
75+
* maps values from the passed in model to those needed for the request
76+
* @param array $requestConfig the passed in model
77+
* @param array $model the set of defaults
8278
* @return array A model ready for the body of a request
8379
*/
8480
protected function buildRequestModel(Array $requestConfig, Array $model=[] ) {
@@ -89,39 +85,41 @@ protected function buildRequestModel(Array $requestConfig, Array $model=[] ) {
8985
}
9086

9187
/**
92-
* @desc posts to the api with a supplied body
93-
* @param body post body for the request
88+
* posts to the api with a supplied body
89+
* @param array $body post body for the request
9490
* @return array Result of the request
9591
*/
9692
public function create(Array $body=[]) {
9793
return $this->callResource( 'post', null, ['body'=>$body]);
9894
}
9995

10096
/**
101-
* @desc Makes a put request to the api with a supplied body
102-
* @param body Put body for the request
97+
* Makes a put request to the api with a supplied body
98+
* @param $resourcePath
99+
* @param array $body Put body for the request
103100
* @return array Result of the request
101+
* @throws APIResponseException
104102
*/
105103
public function update( $resourcePath, Array $body=[]) {
106104
return $this->callResource( 'put', $resourcePath, ['body'=>$body]);
107105
}
108106

109107
/**
110-
* @desc Wrapper method for issuing GET request to current API endpoint
108+
* Wrapper method for issuing GET request to current API endpoint
111109
*
112110
* @param string $resourcePath (optional) string resource path of specific resource
113-
* @param array $options (optional) query string parameters
111+
* @param array $query (optional) query string parameters
114112
* @return array Result of the request
115113
*/
116114
public function get( $resourcePath=null, Array $query=[] ) {
117115
return $this->callResource( 'get', $resourcePath, ['query'=>$query] );
118116
}
119117

120118
/**
121-
* @desc Wrapper method for issuing DELETE request to current API endpoint
119+
* Wrapper method for issuing DELETE request to current API endpoint
122120
*
123121
* @param string $resourcePath (optional) string resource path of specific resource
124-
* @param array $options (optional) query string parameters
122+
* @param array $query (optional) query string parameters
125123
* @return array Result of the request
126124
*/
127125
public function delete( $resourcePath=null, Array $query=[] ) {
@@ -130,13 +128,13 @@ public function delete( $resourcePath=null, Array $query=[] ) {
130128

131129

132130
/**
133-
* @desc assembles a URL for a request
131+
* assembles a URL for a request
134132
* @param string $resourcePath path after the initial endpoint
135-
* @param array options array with an optional value of query with values to build a querystring from.
133+
* @param array $options array with an optional value of query with values to build a querystring from.
136134
* @return string the assembled URL
137135
*/
138136
private function buildUrl($resourcePath, $options) {
139-
$url = join(['/', $this->endpoint, '/']);
137+
$url = "/{$this->endpoint}/";
140138
if (!is_null($resourcePath)){
141139
$url .= $resourcePath;
142140
}
@@ -151,8 +149,8 @@ private function buildUrl($resourcePath, $options) {
151149

152150

153151
/**
154-
* @desc Prepares a body for put and post requests
155-
* @param array options array with an optional value of body with values to build a request body from.
152+
* Prepares a body for put and post requests
153+
* @param array $options array with an optional value of body with values to build a request body from.
156154
* @return string|null A json encoded string or null if no body was provided
157155
*/
158156
private function buildBody($options) {
@@ -165,9 +163,8 @@ private function buildBody($options) {
165163
return $body;
166164
}
167165

168-
169166
/**
170-
* @desc Private Method for issuing GET and DELETE request to current API endpoint
167+
* Private Method for issuing GET and DELETE request to current API endpoint
171168
*
172169
* This method is responsible for getting the collection _and_
173170
* a specific entity from the API endpoint
@@ -178,6 +175,7 @@ private function buildBody($options) {
178175
* @param string $resourcePath (optional) string resource path of specific resource
179176
* @param array $options (optional) query string parameters
180177
* @return array Result set of action performed on resource
178+
* @throws APIResponseException
181179
*/
182180
private function callResource( $action, $resourcePath=null, $options=[] ) {
183181
$action = strtoupper($action); // normalize

0 commit comments

Comments
 (0)