1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Rpc\Decorators; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use ScayTrase\Api\Rpc\ResponseCollectionInterface; |
7
|
|
|
use ScayTrase\Api\Rpc\RpcRequestInterface; |
8
|
|
|
use ScayTrase\Api\Rpc\RpcResponseInterface; |
9
|
|
|
|
10
|
|
|
final class LoggableResponseCollection implements \IteratorAggregate, ResponseCollectionInterface |
11
|
|
|
{ |
12
|
|
|
/** @var LoggerInterface */ |
13
|
|
|
private $logger; |
14
|
|
|
/** @var ResponseCollectionInterface */ |
15
|
|
|
private $decoratedCollection; |
16
|
|
|
/** @var string[] */ |
17
|
|
|
private $loggedResponses = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* LoggableResponseCollection constructor. |
21
|
|
|
* |
22
|
|
|
* @param ResponseCollectionInterface $decoratedCollection |
23
|
|
|
* @param LoggerInterface $logger |
24
|
|
|
*/ |
25
|
4 |
|
public function __construct(ResponseCollectionInterface $decoratedCollection, LoggerInterface $logger) |
26
|
|
|
{ |
27
|
4 |
|
$this->decoratedCollection = $decoratedCollection; |
28
|
4 |
|
$this->logger = $logger; |
29
|
4 |
|
} |
30
|
|
|
|
31
|
|
|
/** {@inheritdoc} */ |
32
|
3 |
|
public function getResponse(RpcRequestInterface $request) |
33
|
|
|
{ |
34
|
3 |
|
$response = $this->decoratedCollection->getResponse($request); |
35
|
3 |
|
$this->logResponseWithRequest($request, $response); |
36
|
|
|
|
37
|
3 |
|
return $response; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** {@inheritdoc} */ |
41
|
2 |
|
public function getIterator() |
42
|
|
|
{ |
43
|
2 |
|
foreach ($this->decoratedCollection as $response) { |
44
|
2 |
|
$this->logResponse($response); |
45
|
2 |
|
yield $response; |
46
|
2 |
|
} |
47
|
2 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param RpcRequestInterface $request |
51
|
|
|
* @param RpcResponseInterface $response |
52
|
|
|
*/ |
53
|
3 |
|
private function logResponseWithRequest(RpcRequestInterface $request, RpcResponseInterface $response) |
54
|
|
|
{ |
55
|
3 |
|
$hash = spl_object_hash($response); |
56
|
3 |
|
if (in_array($hash, $this->loggedResponses, true)) { |
57
|
1 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
if ($response->isSuccessful()) { |
61
|
1 |
|
$this->logger->info( |
62
|
1 |
|
sprintf('Method "%s" call was successful', $request->getMethod()), |
63
|
1 |
|
['request_hash' => spl_object_hash($request)] |
64
|
1 |
|
); |
65
|
1 |
|
$this->logger->debug( |
66
|
1 |
|
sprintf("Response:\n%s", json_encode($response->getBody(), JSON_PRETTY_PRINT)), |
67
|
1 |
|
['request_hash' => spl_object_hash($request)] |
68
|
1 |
|
); |
69
|
1 |
|
} else { |
70
|
1 |
|
$this->logger->info( |
71
|
1 |
|
sprintf('Method "%s" call was failed', $request->getMethod()), |
72
|
1 |
|
['request_hash' => spl_object_hash($request)] |
73
|
1 |
|
); |
74
|
1 |
|
$this->logger->error( |
75
|
1 |
|
sprintf('ERROR %s: %s', $response->getError()->getCode(), $response->getError()->getMessage()), |
76
|
1 |
|
['request_hash' => spl_object_hash($request)] |
77
|
1 |
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
$this->loggedResponses[] = $hash; |
81
|
2 |
|
} |
82
|
|
|
|
83
|
2 |
|
private function logResponse(RpcResponseInterface $response) |
84
|
|
|
{ |
85
|
2 |
|
$hash = spl_object_hash($response); |
86
|
2 |
|
if (in_array($hash, $this->loggedResponses, true)) { |
87
|
1 |
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
if ($response->isSuccessful()) { |
91
|
1 |
|
$this->logger->info('Successful RPC call'); |
92
|
1 |
|
$this->logger->debug( |
93
|
1 |
|
sprintf("Response:\n%s", json_encode($response->getBody(), JSON_PRETTY_PRINT)) |
94
|
1 |
|
); |
95
|
1 |
|
} else { |
96
|
1 |
|
$this->logger->error( |
97
|
1 |
|
sprintf('RPC Error %s: %s', $response->getError()->getCode(), $response->getError()->getMessage()) |
98
|
1 |
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
$this->loggedResponses[] = $hash; |
102
|
2 |
|
} |
103
|
|
|
} |
104
|
|
|
|