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
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
private $debug; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* LoggableResponseCollection constructor. |
25
|
|
|
* |
26
|
|
|
* @param ResponseCollectionInterface $decoratedCollection |
27
|
|
|
* @param LoggerInterface $logger |
28
|
|
|
* @param bool $debug |
29
|
|
|
*/ |
30
|
5 |
|
public function __construct( |
31
|
|
|
ResponseCollectionInterface $decoratedCollection, |
32
|
|
|
LoggerInterface $logger, |
33
|
|
|
$debug = false |
34
|
|
|
) { |
35
|
5 |
|
$this->decoratedCollection = $decoratedCollection; |
36
|
5 |
|
$this->logger = $logger; |
37
|
5 |
|
$this->debug = $debug; |
38
|
5 |
|
} |
39
|
|
|
|
40
|
|
|
/** {@inheritdoc} */ |
41
|
4 |
|
public function getResponse(RpcRequestInterface $request) |
42
|
|
|
{ |
43
|
4 |
|
$response = $this->decoratedCollection->getResponse($request); |
44
|
4 |
|
$this->logResponseWithRequest($request, $response); |
45
|
|
|
|
46
|
4 |
|
return $response; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** {@inheritdoc} */ |
50
|
2 |
|
public function getIterator() |
51
|
|
|
{ |
52
|
2 |
|
foreach ($this->decoratedCollection as $response) { |
53
|
2 |
|
$this->logResponse($response); |
54
|
2 |
|
yield $response; |
55
|
2 |
|
} |
56
|
2 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param RpcRequestInterface $request |
60
|
|
|
* @param RpcResponseInterface $response |
61
|
|
|
*/ |
62
|
4 |
|
private function logResponseWithRequest(RpcRequestInterface $request, RpcResponseInterface $response) |
63
|
|
|
{ |
64
|
4 |
|
$hash = spl_object_hash($response); |
65
|
4 |
|
if (in_array($hash, $this->loggedResponses, true)) { |
66
|
1 |
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
if ($response->isSuccessful()) { |
70
|
2 |
|
$this->logger->info( |
71
|
2 |
|
sprintf('Method "%s" call was successful', $request->getMethod()), |
72
|
2 |
|
['request_hash' => spl_object_hash($request)] |
73
|
2 |
|
); |
74
|
2 |
|
if ($this->debug) { |
75
|
1 |
|
$this->logger->debug( |
76
|
1 |
|
sprintf("Response:\n%s", json_encode($response->getBody(), JSON_PRETTY_PRINT)), |
77
|
1 |
|
['request_hash' => spl_object_hash($request)] |
78
|
1 |
|
); |
79
|
1 |
|
} |
80
|
2 |
|
} else { |
81
|
1 |
|
$this->logger->info( |
82
|
1 |
|
sprintf('Method "%s" call was failed', $request->getMethod()), |
83
|
1 |
|
['request_hash' => spl_object_hash($request)] |
84
|
1 |
|
); |
85
|
1 |
|
$this->logger->error( |
86
|
1 |
|
sprintf('ERROR %s: %s', $response->getError()->getCode(), $response->getError()->getMessage()), |
87
|
1 |
|
['request_hash' => spl_object_hash($request)] |
88
|
1 |
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
$this->loggedResponses[] = $hash; |
92
|
3 |
|
} |
93
|
|
|
|
94
|
2 |
|
private function logResponse(RpcResponseInterface $response) |
95
|
|
|
{ |
96
|
2 |
|
$hash = spl_object_hash($response); |
97
|
2 |
|
if (in_array($hash, $this->loggedResponses, true)) { |
98
|
1 |
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
if ($response->isSuccessful()) { |
102
|
1 |
|
$this->logger->info('Successful RPC call'); |
103
|
1 |
|
if ($this->debug) { |
104
|
|
|
$this->logger->debug( |
105
|
|
|
sprintf("Response:\n%s", json_encode($response->getBody(), JSON_PRETTY_PRINT)) |
106
|
|
|
); |
107
|
|
|
} |
108
|
1 |
|
} else { |
109
|
1 |
|
$this->logger->error( |
110
|
1 |
|
sprintf('RPC Error %s: %s', $response->getError()->getCode(), $response->getError()->getMessage()) |
111
|
1 |
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
$this->loggedResponses[] = $hash; |
115
|
2 |
|
} |
116
|
|
|
} |
117
|
|
|
|