1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rezzza\RestApiBehatExtension\Rest; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
use Http\Discovery\HttpClientDiscovery; |
7
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
class RestApiBrowser |
12
|
|
|
{ |
13
|
|
|
/** @var HttpClient */ |
14
|
|
|
private $httpClient; |
15
|
|
|
|
16
|
|
|
/** @var RequestInterface */ |
17
|
|
|
private $request; |
18
|
|
|
|
19
|
|
|
/** @var ResponseInterface */ |
20
|
|
|
private $response; |
21
|
|
|
|
22
|
|
|
/** @var array */ |
23
|
|
|
private $requestHeaders = []; |
24
|
|
|
|
25
|
|
|
/** @var ResponseStorage */ |
26
|
|
|
private $responseStorage; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $host; |
30
|
|
|
|
31
|
|
|
/** @var MessageFactoryDiscovery */ |
32
|
|
|
private $messageFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $host |
36
|
|
|
*/ |
37
|
|
|
public function __construct($host, HttpClient $httpClient = null) |
38
|
|
|
{ |
39
|
|
|
$this->host = $host; |
40
|
|
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
41
|
|
|
$this->messageFactory = MessageFactoryDiscovery::find(); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Allow to override the httpClient to use yours with specific middleware for example |
46
|
|
|
*/ |
47
|
|
|
public function useHttpClient(HttpClient $httpClient) |
48
|
|
|
{ |
49
|
|
|
$this->httpClient = $httpClient; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param ResponseStorage $responseStorage |
54
|
|
|
*/ |
55
|
|
|
public function enableResponseStorage(ResponseStorage $responseStorage) |
56
|
|
|
{ |
57
|
|
|
$this->responseStorage = $responseStorage; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return ResponseInterface |
62
|
|
|
*/ |
63
|
|
|
public function getResponse() |
64
|
|
|
{ |
65
|
|
|
return $this->response; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param ResponseInterface $response |
70
|
|
|
*/ |
71
|
|
|
public function setResponse(ResponseInterface $response) |
72
|
|
|
{ |
73
|
|
|
$this->response = $response; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getRequest() |
77
|
|
|
{ |
78
|
|
|
return $this->request; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getRequestHeaders() |
82
|
|
|
{ |
83
|
|
|
return $this->requestHeaders; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $method |
88
|
|
|
* @param string $uri |
89
|
|
|
* @param string|array $body |
90
|
|
|
*/ |
91
|
|
|
public function sendRequest($method, $uri, $body = null) |
92
|
|
|
{ |
93
|
|
|
if (false === $this->hasHost($uri)) { |
94
|
|
|
$uri = rtrim($this->host, '/').'/'.ltrim($uri, '/'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->request = $this->messageFactory->createRequest($method, $uri, $this->requestHeaders, $body); |
|
|
|
|
98
|
|
|
$this->response = $this->httpClient->sendRequest($this->request); |
99
|
|
|
|
100
|
|
|
if (null !== $this->responseStorage) { |
101
|
|
|
$this->responseStorage->writeRawContent((string) $this->response->getBody()); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $name |
107
|
|
|
* @param string $value |
108
|
|
|
*/ |
109
|
|
|
public function setRequestHeader($name, $value) |
110
|
|
|
{ |
111
|
|
|
$this->removeRequestHeader($name); |
112
|
|
|
$this->addRequestHeader($name, $value); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $name |
117
|
|
|
* @param string $value |
118
|
|
|
*/ |
119
|
|
|
public function addRequestHeader($name, $value) |
120
|
|
|
{ |
121
|
|
|
if (isset($this->requestHeaders[$name])) { |
122
|
|
|
$this->requestHeaders[$name] .= ', '.$value; |
123
|
|
|
} else { |
124
|
|
|
$this->requestHeaders[$name] = $value; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $headerName |
130
|
|
|
*/ |
131
|
|
|
private function removeRequestHeader($headerName) |
132
|
|
|
{ |
133
|
|
|
if (array_key_exists($headerName, $this->requestHeaders)) { |
134
|
|
|
unset($this->requestHeaders[$headerName]); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $uri |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
private function hasHost($uri) |
144
|
|
|
{ |
145
|
|
|
return strpos($uri, '://') !== false; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..