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