PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / vendor / apimatic / core / src / Logger / ApiLogger.php
ameliabooking / vendor / apimatic / core / src / Logger Last commit date
Configuration 1 year ago ApiLogger.php 1 year ago ConsoleLogger.php 1 year ago LoggerConstants.php 1 year ago NullApiLogger.php 1 year ago
ApiLogger.php
116 lines
1 <?php
2
3 namespace Core\Logger;
4
5 use Core\Logger\Configuration\LoggingConfiguration;
6 use CoreInterfaces\Core\Logger\ApiLoggerInterface;
7 use CoreInterfaces\Core\Request\RequestInterface;
8 use CoreInterfaces\Core\Response\ResponseInterface;
9
10 class ApiLogger implements ApiLoggerInterface
11 {
12 private $config;
13
14 public function __construct(LoggingConfiguration $config)
15 {
16 $this->config = $config;
17 }
18
19 /**
20 * @inheritDoc
21 */
22 public function logRequest(RequestInterface $request): void
23 {
24 $contentType = $this->getHeaderValue(LoggerConstants::CONTENT_TYPE_HEADER, $request->getHeaders());
25
26 $this->config->logMessage(
27 'Request {' . LoggerConstants::METHOD . '} {' . LoggerConstants::URL .
28 '} {' . LoggerConstants::CONTENT_TYPE . '}',
29 [
30 LoggerConstants::METHOD => $request->getHttpMethod(),
31 LoggerConstants::URL => $this->getRequestUrl($request),
32 LoggerConstants::CONTENT_TYPE => $contentType
33 ]
34 );
35
36 if ($this->config->getRequestConfig()->shouldLogHeaders()) {
37 $headers = $this->config->getRequestConfig()->getLoggableHeaders(
38 $request->getHeaders(),
39 $this->config->shouldMaskSensitiveHeaders()
40 );
41 $this->config->logMessage(
42 'Request Headers {' . LoggerConstants::HEADERS . '}',
43 [LoggerConstants::HEADERS => $headers]
44 );
45 }
46
47 if ($this->config->getRequestConfig()->shouldLogBody()) {
48 $body = $request->getParameters();
49 if (empty($body)) {
50 $body = $request->getBody();
51 }
52 $this->config->logMessage(
53 'Request Body {' . LoggerConstants::BODY . '}',
54 [LoggerConstants::BODY => $body]
55 );
56 }
57 }
58
59 /**
60 * @inheritDoc
61 */
62 public function logResponse(ResponseInterface $response): void
63 {
64 $contentLength = $this->getHeaderValue(LoggerConstants::CONTENT_LENGTH_HEADER, $response->getHeaders());
65 $contentType = $this->getHeaderValue(LoggerConstants::CONTENT_TYPE_HEADER, $response->getHeaders());
66
67 $this->config->logMessage(
68 'Response {' . LoggerConstants::STATUS_CODE . '} {' . LoggerConstants::CONTENT_LENGTH .
69 '} {' . LoggerConstants::CONTENT_TYPE . '}',
70 [
71 LoggerConstants::STATUS_CODE => $response->getStatusCode(),
72 LoggerConstants::CONTENT_LENGTH => $contentLength,
73 LoggerConstants::CONTENT_TYPE => $contentType
74 ]
75 );
76
77 if ($this->config->getResponseConfig()->shouldLogHeaders()) {
78 $headers = $this->config->getResponseConfig()->getLoggableHeaders(
79 $response->getHeaders(),
80 $this->config->shouldMaskSensitiveHeaders()
81 );
82 $this->config->logMessage(
83 'Response Headers {' . LoggerConstants::HEADERS . '}',
84 [LoggerConstants::HEADERS => $headers]
85 );
86 }
87
88 if ($this->config->getResponseConfig()->shouldLogBody()) {
89 $this->config->logMessage(
90 'Response Body {' . LoggerConstants::BODY . '}',
91 [LoggerConstants::BODY => $response->getRawBody()]
92 );
93 }
94 }
95
96 private function getHeaderValue(string $key, array $headers): ?string
97 {
98 $key = strtolower($key);
99 foreach ($headers as $k => $value) {
100 if (strtolower($k) === $key) {
101 return $value;
102 }
103 }
104 return null;
105 }
106
107 private function getRequestUrl(RequestInterface $request): string
108 {
109 $queryUrl = $request->getQueryUrl();
110 if ($this->config->getRequestConfig()->shouldIncludeQueryInPath()) {
111 return $queryUrl;
112 }
113 return explode("?", $queryUrl)[0];
114 }
115 }
116