Skip to content

Commit 70b315c

Browse files
committed
-Removed request/response state from client classes.
-Updated README -Added Code Climate .yml file -Update doc blocks
1 parent 0bcdeb2 commit 70b315c

File tree

7 files changed

+69
-132
lines changed

7 files changed

+69
-132
lines changed

.codeclimate.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
engines:
2+
duplication:
3+
enabled: true
4+
config:
5+
languages:
6+
- php
7+
fixme:
8+
enabled: true
9+
phpmd:
10+
enabled: true
11+
ratings:
12+
paths:
13+
- "**.php"
14+
exclude_paths:
15+
- .settings/
16+
- build/
17+
- tests/
18+
- vendor/

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
# php-rest-client
66

7-
This library provides classes to implement basic REST clients based on PHP's cURL extension. Two client classes are made available:
7+
This library provides classes implementing basic REST clients based on PHP's cURL extension. Two client classes are made available:
88

99
- **RestClient** - a class for executing RESTful service calls using a fluent interface.
1010
- **RestMultiClient** - a class which extends RestClient to provide curl_multi capabilities to allow for multiple REST calls to be made in parallel.
11+
12+
Additionally this library provides classes which wrap curl responses within object oriented interface:
1113
- **CurlHttpResponse** - a class which encapsulates curl response into class wrapper.
1214
- **CurlMultiHttpResponse** - a class which represents a collection of CurlHttpRepsonse objects as returned from multiple parallel REST calls.
1315

@@ -31,7 +33,7 @@ This library is developed against PHP 7.1 and tested via Travis CI against:
3133
Please see Travis CI build status at: https://travis-ci.org/mikecbrant/php-rest-client
3234
Please see Code Climate code coverage and health informatoin at: https://codeclimate.com/github/mikecbrant/php-rest-client
3335

34-
This version represents a total re-factoring from previous versions of this library, which were getting long in the tooth and were out of compliance with more modern PHP development standards (i.e. PSR) and tools (i.e. composer).
36+
This library is available as as mikecbrant/php-rest-client omposer package at https://packagist.org/packages/mikecbrant/php-rest-client
3537

3638
**Usage example:**
3739

src/RestClientLib/CurlMultiHttpResponse.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class CurlMultiHttpResponse
77
/**
88
* Variable to store individual CurlHttpResponse objects from curl_multi call
99
*
10-
* @var array
10+
* @var CurlHttpResponse[]
1111
*/
1212
protected $curlHttpResponses = array();
1313

@@ -29,7 +29,7 @@ public function addResponse(CurlHttpResponse $response) {
2929
/**
3030
* Returns array of all CurlHttpResponse objects in collection.
3131
*
32-
* @return array
32+
* @return CurlHttpResponse[]
3333
*/
3434
public function getCurlHttpResponses() {
3535
return $this->curlHttpResponses;
@@ -38,7 +38,7 @@ public function getCurlHttpResponses() {
3838
/**
3939
* Alias for getCurlHttpResponses
4040
*
41-
* @return array
41+
* @return CurlHttpResponse[]
4242
*/
4343
public function getAll() {
4444
return $this->getCurlHttpResponses();
@@ -47,7 +47,7 @@ public function getAll() {
4747
/**
4848
* Returns array of response bodies for each response in collection.
4949
*
50-
* @return array
50+
* @return mixed[]
5151
*/
5252
public function getResponseBodies() {
5353
return array_map(
@@ -61,7 +61,7 @@ function(CurlHttpResponse $value) {
6161
/**
6262
* Returns array of response codes for each response in collection.
6363
*
64-
* @return array
64+
* @return integer[]
6565
*/
6666
public function getHttpCodes() {
6767
return array_map(
@@ -75,7 +75,7 @@ function(CurlHttpResponse $value) {
7575
/**
7676
* Returns array of URL's used for each response in collectoin as returned via curl_getinfo.
7777
*
78-
* @return array
78+
* @return string[]
7979
*/
8080
public function getRequestUrls() {
8181
return array_map(
@@ -89,7 +89,7 @@ function(CurlHttpResponse $value) {
8989
/**
9090
* Returns array of request headers for each response in collection as returned via curl_getinfo.
9191
*
92-
* @return array
92+
* @return string[]
9393
*/
9494
public function getRequestHeaders() {
9595
return array_map(
@@ -104,7 +104,7 @@ function(CurlHttpResponse $value) {
104104
* Returns array of curl_getinfo arrays for each response in collection.
105105
* See documentation at http://php.net/manual/en/function.curl-getinfo.php for expected format for each array element.
106106
*
107-
* @return array
107+
* @return array[]
108108
*/
109109
public function getCurlGetinfoArrays() {
110110
return array_map(

src/RestClientLib/RestClient.php

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@ class RestClient
8585
*/
8686
private $curl = null;
8787

88-
/**
89-
* Variable to store request URL that is formed before a request is made
90-
*
91-
* @var string
92-
*/
93-
private $requestUrl = null;
94-
9588
/**
9689
* Array containing headers to be used for request
9790
*
@@ -105,13 +98,6 @@ class RestClient
10598
* @var string
10699
*/
107100

108-
/**
109-
* Variable to store CurlHttpResponse result from curl call
110-
*
111-
* @var CurlHttpResponse
112-
*/
113-
private $response = null;
114-
115101
/**
116102
* Constructor method. Currently there is no instantiation logic.
117103
*
@@ -133,9 +119,7 @@ public function get($action) {
133119
$this->setRequestUrl($action);
134120
curl_setopt($this->curl, CURLOPT_HTTPGET, true);
135121
// execute call. Can throw \Exception.
136-
$this->curlExec();
137-
138-
return $this->response;
122+
return $this->curlExec();
139123
}
140124

141125
/**
@@ -155,9 +139,7 @@ public function post($action, $data) {
155139
$this->setRequestData($data);
156140
curl_setopt($this->curl, CURLOPT_POST, true);
157141
// execute call. Can throw \Exception.
158-
$this->curlExec();
159-
160-
return $this->response;
142+
return $this->curlExec();
161143
}
162144

163145
/**
@@ -177,9 +159,7 @@ public function put($action, $data) {
177159
$this->setRequestData($data);
178160
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
179161
// execute call. Can throw \Exception.
180-
$this->curlExec();
181-
182-
return $this->response;
162+
return $this->curlExec();
183163
}
184164

185165
/**
@@ -196,9 +176,7 @@ public function delete($action) {
196176
$this->setRequestUrl($action);
197177
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
198178
// execute call. Can throw \Exception.
199-
$this->curlExec();
200-
201-
return $this->response;
179+
return $this->curlExec();
202180
}
203181

204182
/**
@@ -216,9 +194,7 @@ public function head($action) {
216194
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
217195
curl_setopt($this->curl, CURLOPT_NOBODY, true);
218196
// execute call. Can throw \Exception.
219-
$this->curlExec();
220-
221-
return $this->response;
197+
return $this->curlExec();
222198
}
223199

224200
/**
@@ -469,21 +445,17 @@ public function getMaxRedirects() {
469445
}
470446

471447
/**
472-
* Method to initialize curl handle in object
448+
* Method to set up curl handle on client
473449
*
474450
* @return void
475451
* @throws \Exception
476452
*/
477453
private function curlSetup() {
478-
// reset all request/response properties
479-
$this->resetRequestResponseProperties();
480-
481-
// initialize curl. Throws \Exception on failure.
482454
$this->curl = $this->curlInit();
483455
}
484456

485457
/**
486-
* Method to initilize a curl handle
458+
* Method to initilize and return a curl handle
487459
*
488460
* @return resource
489461
* @throws \Exception
@@ -557,7 +529,7 @@ protected function curlClose($curl) {
557529
/**
558530
* Method to execute curl call
559531
*
560-
* @return void
532+
* @return CurlHttpResponse
561533
* @throws \Exception
562534
*/
563535
private function curlExec() {
@@ -571,7 +543,7 @@ private function curlExec() {
571543

572544
// return CurlHttpResponse
573545
try {
574-
$this->response = new CurlHttpResponse($curlResult, curl_getinfo($this->curl));
546+
$response = new CurlHttpResponse($curlResult, curl_getinfo($this->curl));
575547
} catch (\InvalidArgumentException $e) {
576548
throw new \Exception(
577549
'Unable to instantiate CurlHttpResponse. Message: "' . $e->getMessage() . '"',
@@ -581,16 +553,8 @@ private function curlExec() {
581553
} finally {
582554
$this->curlTeardown();
583555
}
584-
}
585-
586-
/**
587-
* Method to reset all properties specific to a particular request/response sequence.
588-
*
589-
* @return void
590-
*/
591-
protected function resetRequestResponseProperties() {
592-
$this->requestUrl = null;
593-
$this->response = null;
556+
557+
return $response;
594558
}
595559

596560
/**
@@ -601,7 +565,6 @@ protected function resetRequestResponseProperties() {
601565
*/
602566
protected function setRequestUrl($action) {
603567
$url = $this->buildUrl($action);
604-
$this->requestUrl = $url;
605568
curl_setopt($this->curl, CURLOPT_URL, $url);
606569
}
607570

0 commit comments

Comments
 (0)