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 Http\Client\Common\PluginClient; |
9
|
|
|
use Http\Client\Common\Plugin\HeaderDefaultsPlugin; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
|
13
|
|
|
class RestApiBrowser |
14
|
|
|
{ |
15
|
|
|
/** @var Http\Client\HttpClient */ |
16
|
|
|
private $httpClient; |
17
|
|
|
|
18
|
|
|
/** @var RequestInterface */ |
19
|
|
|
private $request; |
20
|
|
|
|
21
|
|
|
/** @var ResponseInterface */ |
22
|
|
|
private $response; |
23
|
|
|
|
24
|
|
|
/** @var array */ |
25
|
|
|
private $requestHeaders = []; |
26
|
|
|
|
27
|
|
|
/** @var ResponseStorage */ |
28
|
|
|
private $responseStorage; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
private $host; |
32
|
|
|
|
33
|
|
|
/** @var MessageFactoryDiscovery */ |
34
|
|
|
private $messageFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $host |
38
|
|
|
*/ |
39
|
|
|
public function __construct($host, HttpClient $httpClient = null) |
40
|
|
|
{ |
41
|
|
|
$this->host = $host; |
42
|
|
|
$this->httpClient = $httpClient ?: HttpClientDiscovery::find(); |
|
|
|
|
43
|
|
|
$this->messageFactory = MessageFactoryDiscovery::find(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ResponseStorage $responseStorage |
48
|
|
|
*/ |
49
|
|
|
public function enableResponseStorage(ResponseStorage $responseStorage) |
50
|
|
|
{ |
51
|
|
|
$this->responseStorage = $responseStorage; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return ResponseInterface |
56
|
|
|
*/ |
57
|
|
|
public function getResponse() |
58
|
|
|
{ |
59
|
|
|
return $this->response; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param ResponseInterface $response |
64
|
|
|
*/ |
65
|
|
|
public function setResponse(ResponseInterface $response) |
66
|
|
|
{ |
67
|
|
|
$this->response = $response; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getRequest() |
71
|
|
|
{ |
72
|
|
|
return $this->request; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getRequestHeaders() |
76
|
|
|
{ |
77
|
|
|
return $this->requestHeaders; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return Http\Client\HttpClient |
82
|
|
|
*/ |
83
|
|
|
public function getHttpClient() |
84
|
|
|
{ |
85
|
|
|
return $this->httpClient; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $method |
90
|
|
|
* @param string $uri |
91
|
|
|
* @param string|array $body |
92
|
|
|
*/ |
93
|
|
|
public function sendRequest($method, $uri, $body = null) |
94
|
|
|
{ |
95
|
|
|
if (false === $this->hasHost($uri)) { |
96
|
|
|
$uri = rtrim($this->host, '/').'/'.ltrim($uri, '/'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->request = $this->messageFactory->createRequest($method, $uri, $this->requestHeaders, $body); |
|
|
|
|
100
|
|
|
$this->response = $this->httpClient->sendRequest($this->request); |
101
|
|
|
|
102
|
|
|
if (null !== $this->responseStorage) { |
103
|
|
|
$this->responseStorage->writeRawContent((string) $this->response->getBody()); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $uri |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
private function hasHost($uri) |
113
|
|
|
{ |
114
|
|
|
return strpos($uri, '://') !== false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $name |
119
|
|
|
* @param string $value |
120
|
|
|
*/ |
121
|
|
|
public function setRequestHeader($name, $value) |
122
|
|
|
{ |
123
|
|
|
$this->removeRequestHeader($name); |
124
|
|
|
$this->addRequestHeader($name, $value); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $headerName |
129
|
|
|
*/ |
130
|
|
|
private function removeRequestHeader($headerName) |
131
|
|
|
{ |
132
|
|
|
if (array_key_exists($headerName, $this->requestHeaders)) { |
133
|
|
|
unset($this->requestHeaders[$headerName]); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $name |
139
|
|
|
* @param string $value |
140
|
|
|
*/ |
141
|
|
|
public function addRequestHeader($name, $value) |
142
|
|
|
{ |
143
|
|
|
if (isset($this->requestHeaders[$name])) { |
144
|
|
|
$this->requestHeaders[$name] .= ', '.$value; |
145
|
|
|
} else { |
146
|
|
|
$this->requestHeaders[$name] = $value; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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..