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\Request\RequestInterface; |
22
|
|
|
use Elastification\Client\Response\ResponseInterface; |
23
|
|
|
use Psr\Log\LoggerInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The primary client class. |
27
|
|
|
* |
28
|
|
|
* This class is aware of the transport layer and the requests/response layer. |
29
|
|
|
* It serves as a kind of command pattern talking to a transport facade. |
30
|
|
|
* |
31
|
|
|
* @package Elastification\Client |
32
|
|
|
* @author Daniel Wendlandt |
33
|
|
|
*/ |
34
|
|
|
class LoggerClient implements ClientInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var LoggerInterface |
38
|
|
|
*/ |
39
|
|
|
private $logger; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var ClientInterface |
43
|
|
|
*/ |
44
|
|
|
private $client; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ClientInterface $client |
48
|
|
|
* @param LoggerInterface $logger |
49
|
|
|
*/ |
50
|
4 |
|
public function __construct( |
51
|
|
|
ClientInterface $client, |
52
|
|
|
LoggerInterface $logger |
53
|
|
|
) { |
54
|
4 |
|
$this->client = $client; |
55
|
4 |
|
$this->logger = $logger; |
56
|
4 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* performs sending the request |
60
|
|
|
* |
61
|
|
|
* @param RequestInterface $request |
62
|
|
|
* |
63
|
|
|
* @throws Exception\ClientException |
64
|
|
|
* @throws Exception\RequestException |
65
|
|
|
* @return ResponseInterface |
66
|
|
|
* @author Daniel Wendlandt |
67
|
|
|
*/ |
68
|
2 |
|
public function send(RequestInterface $request) |
69
|
|
|
{ |
70
|
2 |
|
$this->logger->info('request: ' . get_class($request), $this->generateLogRequestData($request)); |
71
|
|
|
|
72
|
|
|
try { |
73
|
2 |
|
$timeTaken = microtime(true); |
74
|
2 |
|
$response = $this->client->send($request); |
75
|
|
|
|
76
|
1 |
|
$this->logger->debug( |
77
|
|
|
'time taken: ' . |
78
|
1 |
|
(microtime(true) - $timeTaken) . |
79
|
1 |
|
's (' . get_class($request) . ')' |
80
|
1 |
|
); |
81
|
|
|
|
82
|
1 |
|
$this->logger->debug('response: ' . get_class($response), $this->generateLogResponseData($response)); |
83
|
|
|
|
84
|
1 |
|
return $response; |
85
|
1 |
|
} catch (ClientException $exception) { |
86
|
1 |
|
$this->logger->error( |
87
|
1 |
|
$exception->getMessage(), |
88
|
|
|
array( |
89
|
1 |
|
'code' => $exception->getCode(), |
90
|
1 |
|
'file' => $exception->getFile(), |
91
|
1 |
|
'line' => $exception->getLine(), |
92
|
1 |
|
'trace' => $exception->getTrace(), |
93
|
1 |
|
'message' => $exception->getMessage(), |
94
|
|
|
|
95
|
|
|
) |
96
|
1 |
|
); |
97
|
|
|
|
98
|
1 |
|
throw $exception; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
105
|
1 |
|
public function getRequest($name) |
106
|
|
|
{ |
107
|
1 |
|
return $this->client->getRequest($name); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* generates an array with all request information in it. |
112
|
|
|
* |
113
|
|
|
* @param RequestInterface $request |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
* @author Daniel Wendlandt |
117
|
|
|
*/ |
118
|
2 |
|
private function generateLogRequestData(RequestInterface $request) |
119
|
|
|
{ |
120
|
|
|
return array( |
121
|
2 |
|
'class' => get_class($request), |
122
|
2 |
|
'method' => $request->getMethod(), |
123
|
2 |
|
'index' => $request->getIndex(), |
124
|
2 |
|
'type' => $request->getType(), |
125
|
2 |
|
'action' => $request->getAction(), |
126
|
2 |
|
'response_class' => $request->getSupportedClass(), |
127
|
2 |
|
'body' => $request->getBody() |
128
|
2 |
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* generates an array with all response information in it. |
133
|
|
|
* |
134
|
|
|
* @param ResponseInterface $response |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
* @author Daniel Wendlandt |
138
|
|
|
*/ |
139
|
1 |
|
private function generateLogResponseData(ResponseInterface $response) |
140
|
|
|
{ |
141
|
|
|
return array( |
142
|
1 |
|
'class' => get_class($response), |
143
|
1 |
|
'raw_data' => $response->getRawData() |
144
|
|
|
|
145
|
1 |
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|