1 | <?php |
||
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) |
|
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) |
|
197 | } |
||
198 |