1 | <?php |
||
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) |
|
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) |
|
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) |
|
147 | |||
148 | } |
||
149 |