|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) Nate Brunette. |
|
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Tebru\Retrofit\Subscriber; |
|
8
|
|
|
|
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
11
|
|
|
use Tebru\Retrofit\Event\AfterSendEvent; |
|
12
|
|
|
use Tebru\Retrofit\Event\ApiExceptionEvent; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class LogSubscriber |
|
16
|
|
|
* |
|
17
|
|
|
* @author Nate Brunette <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class LogSubscriber implements EventSubscriberInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var LoggerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $logger; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Constructor |
|
28
|
|
|
* |
|
29
|
|
|
* @param LoggerInterface $logger |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(LoggerInterface $logger) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->logger = $logger; |
|
34
|
|
|
} |
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function getSubscribedEvents() |
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
|
|
AfterSendEvent::NAME => 'onAfterSend', |
|
42
|
|
|
ApiExceptionEvent::NAME => 'onApiException', |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Log the request and response if it exists |
|
48
|
|
|
* |
|
49
|
|
|
* @param AfterSendEvent $event |
|
50
|
|
|
*/ |
|
51
|
|
|
public function onAfterSend(AfterSendEvent $event) |
|
52
|
|
|
{ |
|
53
|
|
|
$request = $event->getRequest(); |
|
54
|
|
|
$response = $event->getResponse(); |
|
55
|
|
|
|
|
56
|
|
|
$log['request'] = [ |
|
|
|
|
|
|
57
|
|
|
'method' => $request->getMethod(), |
|
58
|
|
|
'uri' => rawurldecode((string) $request->getUri()), |
|
59
|
|
|
'headers' => $request->getHeaders(), |
|
60
|
|
|
'body' => (string) $request->getBody(), |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
if (null !== $response) { |
|
64
|
|
|
$log['response'] = [ |
|
65
|
|
|
'statusCode' => $response->getStatusCode(), |
|
66
|
|
|
'reasonPhrase' => $response->getReasonPhrase(), |
|
67
|
|
|
'body' => (string) $response->getBody(), |
|
68
|
|
|
'headers' => $response->getHeaders(), |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$this->logger->debug('Sent Request', $log); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Log an api exception with the request |
|
77
|
|
|
* |
|
78
|
|
|
* @param ApiExceptionEvent $event |
|
79
|
|
|
*/ |
|
80
|
|
|
public function onApiException(ApiExceptionEvent $event) |
|
81
|
|
|
{ |
|
82
|
|
|
$request = $event->getRequest(); |
|
83
|
|
|
$exception = $event->getException(); |
|
84
|
|
|
|
|
85
|
|
|
$this->logger->error('Request Exception', [ |
|
86
|
|
|
'request' => [ |
|
87
|
|
|
'method' => $request->getMethod(), |
|
88
|
|
|
'uri' => rawurldecode((string) $request->getUri()), |
|
89
|
|
|
'headers' => $request->getHeaders(), |
|
90
|
|
|
'body' => (string) $request->getBody(), |
|
91
|
|
|
], |
|
92
|
|
|
'exception' => $exception, |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.