Skip to content

Commit 19967aa

Browse files
author
Martin Brecht-Precht
committed
Refactored the Request, Response and Statistics class.
1 parent 910585d commit 19967aa

File tree

5 files changed

+214
-47
lines changed

5 files changed

+214
-47
lines changed

src/Request/Base/RequestInterface.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
use BasicHttpClient\Request\Authentication\Base\AuthenticationInterface;
66
use BasicHttpClient\Request\Message\Base\MessageInterface;
7+
use BasicHttpClient\Request\Message\Header\Header;
78
use BasicHttpClient\Request\Transport\Base\TransportInterface;
8-
use BasicHttpClient\Response\Response;
9+
use BasicHttpClient\Response\Base\ResponseInterface;
910

1011
/**
1112
* Class Request
@@ -15,6 +16,17 @@
1516
interface RequestInterface extends CurlConfiguratorInterface
1617
{
1718

19+
/**
20+
* @return string
21+
*/
22+
public function getUserAgent();
23+
24+
/**
25+
* @param string $userAgent
26+
* @return $this
27+
*/
28+
public function setUserAgent($userAgent);
29+
1830
/**
1931
* @return string
2032
*/
@@ -31,6 +43,11 @@ public function setEndpoint($endpoint);
3143
*/
3244
public function getPort();
3345

46+
/**
47+
* @return bool
48+
*/
49+
public function hasPort();
50+
3451
/**
3552
* @param int $port
3653
* @return $this
@@ -115,8 +132,23 @@ public function countAuthentications();
115132
public function perform();
116133

117134
/**
118-
* @return Response
135+
* @return ResponseInterface
119136
*/
120137
public function getResponse();
121138

139+
/**
140+
* @return string
141+
*/
142+
public function getEffectiveStatus();
143+
144+
/**
145+
* @return string
146+
*/
147+
public function getEffectiveEndpoint();
148+
149+
/**
150+
* @return Header[]
151+
*/
152+
public function getEffectiveHeaders();
153+
122154
}

src/Request/Request.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use BasicHttpClient\Request\Transport\Base\TransportInterface;
1010
use BasicHttpClient\Request\Transport\HttpsTransport;
1111
use BasicHttpClient\Request\Transport\HttpTransport;
12+
use BasicHttpClient\Response\Base\ResponseInterface;
1213
use BasicHttpClient\Response\Response;
1314
use BasicHttpClient\Util\UrlUtil;
1415
use CommonException\NetworkException\Base\NetworkException;
@@ -65,7 +66,7 @@ class Request implements RequestInterface
6566
private $message;
6667

6768
/**
68-
* @var Response
69+
* @var ResponseInterface
6970
*/
7071
private $response;
7172

@@ -287,30 +288,6 @@ public function countAuthentications()
287288
return count($this->authentications);
288289
}
289290

290-
/**
291-
* @return string
292-
*/
293-
public function getEffectiveStatus()
294-
{
295-
return $this->effectiveStatus;
296-
}
297-
298-
/**
299-
* @return string
300-
*/
301-
public function getEffectiveEndpoint()
302-
{
303-
return $this->effectiveEndpoint;
304-
}
305-
306-
/**
307-
* @return Header[]
308-
*/
309-
public function getEffectiveHeaders()
310-
{
311-
return $this->effectiveHeaders;
312-
}
313-
314291
/**
315292
* @param resource $curl
316293
* @return $this
@@ -389,13 +366,37 @@ public function perform()
389366
}
390367

391368
/**
392-
* @return Response
369+
* @return ResponseInterface
393370
*/
394371
public function getResponse()
395372
{
396373
return $this->response;
397374
}
398375

376+
/**
377+
* @return string
378+
*/
379+
public function getEffectiveStatus()
380+
{
381+
return $this->effectiveStatus;
382+
}
383+
384+
/**
385+
* @return string
386+
*/
387+
public function getEffectiveEndpoint()
388+
{
389+
return $this->effectiveEndpoint;
390+
}
391+
392+
/**
393+
* @return Header[]
394+
*/
395+
public function getEffectiveHeaders()
396+
{
397+
return $this->effectiveHeaders;
398+
}
399+
399400
/**
400401
* @throws \Exception
401402
* @return void
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace BasicHttpClient\Response\Base;
4+
5+
use BasicHttpClient\Request\Base\RequestInterface;
6+
use BasicHttpClient\Response\Header\Header;
7+
use BasicHttpClient\Response\Statistics\Statistics;
8+
9+
/**
10+
* Interface ResponseInterface
11+
*
12+
* @package BasicHttpClient\Response\Base
13+
*/
14+
interface ResponseInterface
15+
{
16+
17+
/**
18+
* Response constructor.
19+
*
20+
* @param RequestInterface $request
21+
*/
22+
public function __construct(RequestInterface $request);
23+
24+
/**
25+
* @param resource $curl
26+
* @param string $responseBody
27+
* @return $this
28+
*/
29+
public function populateFromCurlResult($curl, $responseBody);
30+
31+
/**
32+
* @return RequestInterface
33+
*/
34+
public function getRequest();
35+
36+
/**
37+
* @return int
38+
*/
39+
public function getStatusCode();
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getStatusText();
45+
46+
/**
47+
* @return Header[]
48+
*/
49+
public function getHeaders();
50+
51+
/**
52+
* @return string
53+
*/
54+
public function getBody();
55+
56+
/**
57+
* @return Statistics
58+
*/
59+
public function getStatistics();
60+
61+
}

src/Response/Response.php

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BasicHttpClient\Response;
44

55
use BasicHttpClient\Request\Base\RequestInterface;
6+
use BasicHttpClient\Response\Base\ResponseInterface;
67
use BasicHttpClient\Response\Header\Header;
78
use BasicHttpClient\Response\Statistics\Statistics;
89

@@ -11,7 +12,7 @@
1112
*
1213
* @package BasicHttpClient\Response
1314
*/
14-
class Response
15+
class Response implements ResponseInterface
1516
{
1617

1718
/**
@@ -39,21 +40,6 @@ class Response
3940
*/
4041
private $body;
4142

42-
/**
43-
* @var int
44-
*/
45-
private $redirectCount;
46-
47-
/**
48-
* @var float
49-
*/
50-
private $redirectTime;
51-
52-
/**
53-
* @var string
54-
*/
55-
private $redirectEndpoint;
56-
5743
/**
5844
* @var Statistics
5945
*/
@@ -77,14 +63,59 @@ public function __construct(RequestInterface $request)
7763
public function populateFromCurlResult($curl, $responseBody)
7864
{
7965
$this->statusCode = intval(curl_getinfo($curl, CURLINFO_HTTP_CODE));
80-
$this->redirectCount = curl_getinfo($curl, CURLINFO_REDIRECT_COUNT);
81-
$this->redirectTime = curl_getinfo($curl, CURLINFO_REDIRECT_TIME);
82-
$this->redirectEndpoint = curl_getinfo($curl, CURLINFO_REDIRECT_URL);
8366
$this->setStatistics($curl);
8467
$this->setResponseData($responseBody);
8568
return $this;
8669
}
8770

71+
/**
72+
* @return RequestInterface
73+
*/
74+
public function getRequest()
75+
{
76+
return $this->request;
77+
}
78+
79+
/**
80+
* @return int
81+
*/
82+
public function getStatusCode()
83+
{
84+
return $this->statusCode;
85+
}
86+
87+
/**
88+
* @return string
89+
*/
90+
public function getStatusText()
91+
{
92+
return $this->statusText;
93+
}
94+
95+
/**
96+
* @return Header[]
97+
*/
98+
public function getHeaders()
99+
{
100+
return $this->headers;
101+
}
102+
103+
/**
104+
* @return string
105+
*/
106+
public function getBody()
107+
{
108+
return $this->body;
109+
}
110+
111+
/**
112+
* @return Statistics
113+
*/
114+
public function getStatistics()
115+
{
116+
return $this->statistics;
117+
}
118+
88119
/**
89120
* @param resource $curl
90121
* @return $this

src/Response/Statistics/Statistics.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ class Statistics
3535
*/
3636
private $startTransferTime;
3737

38+
/**
39+
* @var int
40+
*/
41+
private $redirectCount;
42+
43+
/**
44+
* @var float
45+
*/
46+
private $redirectTime;
47+
48+
/**
49+
* @var string
50+
*/
51+
private $redirectEndpoint;
52+
3853
/**
3954
* @param resource $curl
4055
* @return $this
@@ -46,6 +61,9 @@ public function populateFromCurlResult($curl)
4661
$this->connectionEstablishTime = curl_getinfo($curl, CURLINFO_CONNECT_TIME);
4762
$this->preTransferTime = curl_getinfo($curl, CURLINFO_PRETRANSFER_TIME);
4863
$this->startTransferTime = curl_getinfo($curl, CURLINFO_STARTTRANSFER_TIME);
64+
$this->redirectCount = curl_getinfo($curl, CURLINFO_REDIRECT_COUNT);
65+
$this->redirectTime = curl_getinfo($curl, CURLINFO_REDIRECT_TIME);
66+
$this->redirectEndpoint = curl_getinfo($curl, CURLINFO_REDIRECT_URL);
4967
return $this;
5068
}
5169

@@ -89,4 +107,28 @@ public function getStartTransferTime()
89107
return $this->startTransferTime;
90108
}
91109

110+
/**
111+
* @return int
112+
*/
113+
public function getRedirectCount()
114+
{
115+
return $this->redirectCount;
116+
}
117+
118+
/**
119+
* @return float
120+
*/
121+
public function getRedirectTime()
122+
{
123+
return $this->redirectTime;
124+
}
125+
126+
/**
127+
* @return string
128+
*/
129+
public function getRedirectEndpoint()
130+
{
131+
return $this->redirectEndpoint;
132+
}
133+
92134
}

0 commit comments

Comments
 (0)