Skip to content

Commit 2413a10

Browse files
committed
- Refactored common validation into new method in RestMultiClient.
- Removed uneeded comments. - Added exception to code climate config
1 parent f62211c commit 2413a10

File tree

4 files changed

+41
-47
lines changed

4 files changed

+41
-47
lines changed

.codeclimate.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ engines:
88
enabled: true
99
phpmd:
1010
enabled: true
11+
exclude_fingerprints:
12+
- 22c29059ae6bb876ee678dfad3c26ec4
1113
ratings:
1214
paths:
13-
- "**.php"
15+
- "**.php"
1416
exclude_paths:
1517
- .settings/
1618
- build/

src/RestClientLib/RestClient.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ public function get($action) {
118118
$this->curlSetup();
119119
$this->setRequestUrl($action);
120120
curl_setopt($this->curl, CURLOPT_HTTPGET, true);
121-
// execute call. Can throw \Exception.
122121
return $this->curlExec();
123122
}
124123

@@ -138,7 +137,6 @@ public function post($action, $data) {
138137
$this->setRequestUrl($action);
139138
$this->setRequestData($data);
140139
curl_setopt($this->curl, CURLOPT_POST, true);
141-
// execute call. Can throw \Exception.
142140
return $this->curlExec();
143141
}
144142

@@ -158,7 +156,6 @@ public function put($action, $data) {
158156
$this->setRequestUrl($action);
159157
$this->setRequestData($data);
160158
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
161-
// execute call. Can throw \Exception.
162159
return $this->curlExec();
163160
}
164161

@@ -175,7 +172,6 @@ public function delete($action) {
175172
$this->curlSetup();
176173
$this->setRequestUrl($action);
177174
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
178-
// execute call. Can throw \Exception.
179175
return $this->curlExec();
180176
}
181177

@@ -193,7 +189,6 @@ public function head($action) {
193189
$this->setRequestUrl($action);
194190
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
195191
curl_setopt($this->curl, CURLOPT_NOBODY, true);
196-
// execute call. Can throw \Exception.
197192
return $this->curlExec();
198193
}
199194

src/RestClientLib/RestMultiClient.php

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@ public function __construct() {}
4646
*/
4747
public function get($actions) {
4848
$this->validateActionArray($actions);
49-
50-
// set up curl handles
5149
$this->curlMultiSetup(count($actions));
5250
$this->setRequestUrls($actions);
5351
foreach($this->curlHandles as $curl) {
54-
curl_setopt($curl, CURLOPT_HTTPGET, true); // explicitly set the method to GET
52+
curl_setopt($curl, CURLOPT_HTTPGET, true);
5553
}
5654
return $this->curlMultiExec();
5755
}
@@ -67,19 +65,12 @@ public function get($actions) {
6765
* @throws \LengthException
6866
*/
6967
public function post($actions, $data) {
70-
$this->validateActionArray($actions);
71-
$this->validateDataArray($data);
72-
// verify that the number of data elements matches the number of action elements
73-
if (count($actions) !== count($data)) {
74-
throw new \LengthException('The number of actions requested does not match the number of data elements provided.');
75-
}
76-
77-
// set up curl handles
68+
$this->validateInputArrays($actions, $data);
7869
$this->curlMultiSetup(count($actions));
7970
$this->setRequestUrls($actions);
8071
$this->setRequestDataArray($data);
8172
foreach($this->curlHandles as $curl) {
82-
curl_setopt($curl, CURLOPT_POST, true); // explicitly set the method to POST
73+
curl_setopt($curl, CURLOPT_POST, true);
8374
}
8475
return $this->curlMultiExec();
8576
}
@@ -95,19 +86,12 @@ public function post($actions, $data) {
9586
* @throws \LengthException
9687
*/
9788
public function put($actions, $data) {
98-
$this->validateActionArray($actions);
99-
$this->validateDataArray($data);
100-
// verify that the number of data elements matches the number of action elements
101-
if (count($actions) !== count($data)) {
102-
throw new \LengthException('The number of actions requested does not match the number of data elements provided.');
103-
}
104-
105-
// set up curl handles
89+
$this->validateInputArrays($actions, $data);
10690
$this->curlMultiSetup(count($actions));
10791
$this->setRequestUrls($actions);
10892
$this->setRequestDataArray($data);
10993
foreach($this->curlHandles as $curl) {
110-
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); // explicitly set the method to PUT
94+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
11195
}
11296
return $this->curlMultiExec();
11397
}
@@ -124,12 +108,10 @@ public function put($actions, $data) {
124108
*/
125109
public function delete($actions) {
126110
$this->validateActionArray($actions);
127-
128-
// set up curl handles
129111
$this->curlMultiSetup(count($actions));
130112
$this->setRequestUrls($actions);
131113
foreach($this->curlHandles as $curl) {
132-
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); // explicitly set the method to DELETE
114+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
133115
}
134116
return $this->curlMultiExec();
135117
}
@@ -145,8 +127,6 @@ public function delete($actions) {
145127
*/
146128
public function head($actions) {
147129
$this->validateActionArray($actions);
148-
149-
// set up curl handles
150130
$this->curlMultiSetup(count($actions));
151131
$this->setRequestUrls($actions);
152132
foreach($this->curlHandles as $curl) {
@@ -235,7 +215,7 @@ private function curlMultiExec() {
235215
}
236216

237217
// process the results. Note there could be individual errors on specific calls
238-
$curlMultiHttpResponse = new CurlMultiHttpResponse();
218+
$curlMultiResponse = new CurlMultiHttpResponse();
239219
foreach($this->curlHandles as $curl) {
240220
try {
241221
$response = new CurlHttpResponse(
@@ -250,10 +230,10 @@ private function curlMultiExec() {
250230
$e
251231
);
252232
}
253-
$curlMultiHttpResponse->addResponse($response);
233+
$curlMultiResponse->addResponse($response);
254234
}
255235
$this->curlMultiTeardown();
256-
return $curlMultiHttpResponse;
236+
return $curlMultiResponse;
257237
}
258238

259239
/**
@@ -282,6 +262,23 @@ private function setRequestDataArray(array $data) {
282262
}
283263
}
284264

265+
/**
266+
* Method to provide validation to action and data arrays for POST/PUT methods
267+
*
268+
* @param string[] $actions
269+
* @param mixed[] $data
270+
* @return void
271+
* @throws \InvalidArgumentException
272+
* @throws \LengthException
273+
*/
274+
private function validateInputArrays(array $actions, array $data) {
275+
$this->validateActionArray($actions);
276+
$this->validateDataArray($data);
277+
if (count($actions) !== count($data)) {
278+
throw new \LengthException('The number of actions requested does not match the number of data elements provided.');
279+
}
280+
}
281+
285282
/**
286283
* Method to provide common validation for action array parameters
287284
*

tests/RestMultiClientTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ public function testValidateDataArrayThrowsExceptionOnOversizedArray() {
175175
);
176176
}
177177

178+
/**
179+
* @expectedException \LengthException
180+
* @covers MikeBrant\RestClientLib\RestMultiClient::validateInputArrays
181+
*/
182+
public function testValidateInputArraysThrowsExceptionOnArraySizeMismatch() {
183+
$maxHandles = $this->client->getMaxHandles();
184+
$this->client->post(
185+
array_fill(0, $maxHandles, 'action'),
186+
array_fill(0, $maxHandles - 1, 'data')
187+
);
188+
}
189+
178190
/**
179191
* @expectedException \Exception
180192
* @covers MikeBrant\RestClientLib\RestMultiClient::curlMultiSetup
@@ -227,18 +239,6 @@ public function testGet() {
227239
$this->assertAttributeEquals(null, 'curlMultiHandle', $this->client);
228240
}
229241

230-
/**
231-
* @expectedException \LengthException
232-
* @covers MikeBrant\RestClientLib\RestMultiClient::post
233-
*/
234-
public function testPostThrowsExceptionOnArraySizeMismatch() {
235-
$maxHandles = $this->client->getMaxHandles();
236-
$this->client->post(
237-
array_fill(0, $maxHandles, 'action'),
238-
array_fill(0, $maxHandles - 1, 'data')
239-
);
240-
}
241-
242242
/**
243243
* @covers MikeBrant\RestClientLib\RestMultiClient::post
244244
* @covers MikeBrant\RestClientLib\RestMultiClient::validateDataArray

0 commit comments

Comments
 (0)