1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. |
17
|
|
|
*/ |
18
|
|
|
namespace Elastification\Client; |
19
|
|
|
|
20
|
|
|
use Elastification\Client\Exception\ClientException; |
21
|
|
|
use Elastification\Client\Exception\RequestException; |
22
|
|
|
use Elastification\Client\Request\RequestInterface; |
23
|
|
|
use Elastification\Client\Request\RequestManagerInterface; |
24
|
|
|
use Elastification\Client\Request\RequestMethods; |
25
|
|
|
use Elastification\Client\Response\Response; |
26
|
|
|
use Elastification\Client\Response\ResponseInterface; |
27
|
|
|
use Elastification\Client\Transport\Exception\TransportLayerException; |
28
|
|
|
use Elastification\Client\Transport\TransportInterface; |
29
|
|
|
use Elastification\Client\Transport\TransportRequestInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The primary client class. |
33
|
|
|
* |
34
|
|
|
* This class is aware of the transport layer and the requests/response layer. |
35
|
|
|
* It serves as a kind of command pattern talking to a transport facade. |
36
|
|
|
* |
37
|
|
|
* @package Elastification\Client |
38
|
|
|
* @author Daniel Wendlandt |
39
|
|
|
*/ |
40
|
|
|
class Client implements ClientInterface |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var TransportInterface |
44
|
|
|
*/ |
45
|
|
|
private $transport; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Request\RequestManagerInterface |
49
|
|
|
*/ |
50
|
|
|
private $requestManager; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var null|string |
54
|
|
|
*/ |
55
|
|
|
private $elasticsearchVersion; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param TransportInterface $transport |
59
|
|
|
* @param RequestManagerInterface $requestManager |
60
|
|
|
* @param null|string $elasticsearchVersion |
61
|
|
|
*/ |
62
|
9 |
|
public function __construct( |
63
|
|
|
TransportInterface $transport, |
64
|
|
|
RequestManagerInterface $requestManager, |
65
|
|
|
$elasticsearchVersion = null |
66
|
|
|
) { |
67
|
9 |
|
$this->transport = $transport; |
68
|
9 |
|
$this->requestManager = $requestManager; |
69
|
|
|
|
70
|
9 |
|
if (null === $elasticsearchVersion) { |
71
|
9 |
|
$this->elasticsearchVersion = self::ELASTICSEARCH_VERSION_1_4_X; |
72
|
9 |
|
} else { |
73
|
1 |
|
$this->elasticsearchVersion = $elasticsearchVersion; |
74
|
|
|
} |
75
|
9 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* performs sending the request |
79
|
|
|
* |
80
|
|
|
* @param RequestInterface $request |
81
|
|
|
* |
82
|
|
|
* @throws Exception\ClientException |
83
|
|
|
* @throws Exception\RequestException |
84
|
|
|
* @return ResponseInterface |
85
|
|
|
* @author Daniel Wendlandt |
86
|
|
|
*/ |
87
|
6 |
|
public function send(RequestInterface $request) |
88
|
|
|
{ |
89
|
6 |
|
if (!RequestMethods::isAllowed($request->getMethod())) { |
90
|
1 |
|
throw new RequestException('request method is not allowed'); |
91
|
|
|
} |
92
|
|
|
|
93
|
5 |
|
$transportRequest = $this->transport->createRequest($request->getMethod()); |
94
|
5 |
|
$transportRequest->setPath($this->generatePath($request)); |
95
|
5 |
|
$transportRequest = $this->setBody($transportRequest, $request->getBody()); |
96
|
5 |
|
$transportRequest = $this->setQueryParameters($transportRequest, $request->getParameters()); |
97
|
|
|
|
98
|
|
|
try { |
99
|
5 |
|
$transportResponse = $this->transport->send($transportRequest); |
100
|
5 |
|
} catch (TransportLayerException $exception) { |
101
|
1 |
|
$clientException = new ClientException($exception->getMessage(), $exception->getCode(), $exception); |
102
|
1 |
|
throw $clientException; |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
$rawData = (string)$transportResponse->getBody(); |
106
|
|
|
|
107
|
|
|
/** @var Response $response */ |
108
|
4 |
|
$response = $request->createResponse($rawData, $request->getSerializer(), $request->getSerializerParams()); |
109
|
4 |
|
$supportedClass = $request->getSupportedClass(); |
110
|
|
|
|
111
|
4 |
|
$this->mangleSupportedClass($response, $supportedClass); |
112
|
|
|
|
113
|
3 |
|
return $response; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param ResponseInterface $response |
118
|
|
|
* @param string $supportedClass |
119
|
|
|
* |
120
|
|
|
* @throws Exception\ClientException |
121
|
|
|
* @author Mario Mueller |
122
|
|
|
*/ |
123
|
4 |
|
private function mangleSupportedClass(ResponseInterface $response, $supportedClass) |
124
|
|
|
{ |
125
|
4 |
|
if (!$response instanceof $supportedClass) { |
126
|
1 |
|
throw new ClientException('response is not an instance of ' . $supportedClass); |
127
|
|
|
} |
128
|
3 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param TransportRequestInterface $transportRequest |
132
|
|
|
* @param $body |
133
|
|
|
* |
134
|
|
|
* @return TransportRequestInterface |
135
|
|
|
* @author Mario Mueller |
136
|
|
|
*/ |
137
|
5 |
|
private function setBody(TransportRequestInterface $transportRequest, $body) |
138
|
|
|
{ |
139
|
5 |
|
if (null !== $body) { |
140
|
1 |
|
$transportRequest->setBody($body); |
141
|
1 |
|
} |
142
|
|
|
|
143
|
5 |
|
return $transportRequest; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Sets query params to transport request |
148
|
|
|
* |
149
|
|
|
* @param TransportRequestInterface $transportRequest |
150
|
|
|
* @param mixed $parameters |
151
|
|
|
* @return TransportRequestInterface |
152
|
|
|
* @author Daniel Wendlandt |
153
|
|
|
*/ |
154
|
5 |
|
private function setQueryParameters(TransportRequestInterface $transportRequest, $parameters) |
155
|
|
|
{ |
156
|
5 |
|
if(!empty($parameters) && is_array($parameters)) { |
157
|
1 |
|
$transportRequest->setQueryParams($parameters); |
158
|
1 |
|
} |
159
|
|
|
|
160
|
5 |
|
return $transportRequest; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @inheritdoc |
165
|
|
|
*/ |
166
|
2 |
|
public function getRequest($name) |
167
|
|
|
{ |
168
|
2 |
|
return $this->requestManager->getRequest($name); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Generates the path by given request |
173
|
|
|
* |
174
|
|
|
* @param RequestInterface $request |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
* @author Daniel Wendlandt |
178
|
|
|
*/ |
179
|
5 |
|
private function generatePath(RequestInterface $request) |
180
|
|
|
{ |
181
|
5 |
|
$path = array(); |
182
|
|
|
|
183
|
5 |
|
if (null !== $request->getIndex()) { |
184
|
5 |
|
$path[] = $request->getIndex(); |
185
|
5 |
|
} |
186
|
|
|
|
187
|
5 |
|
if (null !== $request->getType()) { |
188
|
5 |
|
$path[] = $request->getType(); |
189
|
5 |
|
} |
190
|
|
|
|
191
|
5 |
|
if (null !== $request->getAction()) { |
192
|
5 |
|
$path[] = $request->getAction(); |
193
|
5 |
|
} |
194
|
|
|
|
195
|
5 |
|
return implode(self::PATH_DIVIDER, $path); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|