GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 21442f...5c1eec )
by Cees-Jan
08:49
created

LoggerMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Middleware\Log;
4
5
use ApiClients\Foundation\Middleware\MiddlewareInterface;
6
use ApiClients\Foundation\Middleware\Priority;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Log\LoggerInterface;
10
use React\Promise\CancellablePromiseInterface;
11
use Throwable;
12
use function React\Promise\reject;
13
use function React\Promise\resolve;
14
15
class LoggerMiddleware implements MiddlewareInterface
16
{
17
    const REQUEST  = 'request';
18
    const RESPONSE = 'response';
19
    const ERROR    = 'error';
20
21
    /**
22
     * @var LoggerInterface
23
     */
24
    private $logger;
25
26
    /**
27
     * @var string
28
     */
29
    private $requestId;
30
31
    /**
32
     * @var array
33
     */
34
    private $context = [
35
        self::REQUEST => [
36
            'method' => null,
37
            'uri' => null,
38
            'protocol_version' => null,
39
            'headers' => [],
40
        ],
41
        self::RESPONSE => [
42
            'status_code' => null,
43
            'status_reason' => null,
44
            'protocol_version' => null,
45
            'headers' => [],
46
        ],
47
    ];
48
49
    /**
50
     * LogMiddleware constructor.
51
     * @param LoggerInterface $logger
52
     */
53
    public function __construct(LoggerInterface $logger)
54
    {
55
        $this->logger = $logger;
56
    }
57
58
    /**
59
     * @return int
60
     */
61
    public function priority(): int
62
    {
63
        return Priority::LAST;
64
    }
65
66
    /**
67
     * @param RequestInterface $request
68
     * @param array $options
69
     * @return CancellablePromiseInterface
70
     */
71
    public function pre(RequestInterface $request, array $options = []): CancellablePromiseInterface
72
    {
73
        if (!isset($options[self::class][Options::LEVEL])) {
74
            return resolve($request);
75
        }
76
77
        $this->requestId = spl_object_hash($request);
78
79
        $this->context[self::REQUEST]['method'] = $request->getMethod();
80
        $this->context[self::REQUEST]['uri'] = (string)$request->getUri();
81
        $this->context[self::REQUEST]['protocol_version'] = (string)$request->getProtocolVersion();
82
        $ignoreHeaders = $options[self::class][Options::IGNORE_HEADERS] ?? [];
83
        $this->iterateHeaders(self::REQUEST, $request->getHeaders(), $ignoreHeaders);
84
85
        return resolve($request);
86
    }
87
88
    /**
89
     * @param ResponseInterface $response
90
     * @param array $options
91
     * @return CancellablePromiseInterface
92
     */
93
    public function post(ResponseInterface $response, array $options = []): CancellablePromiseInterface
94
    {
95
        if (!isset($options[self::class][Options::LEVEL])) {
96
            return resolve($response);
97
        }
98
99
        $message = 'Request ' . $this->requestId . ' completed.';
100
101
        $this->addResponseToContext($response, $options);
102
103
        $this->logger->log($options[self::class][Options::LEVEL], $message, $this->context);
104
105
        return resolve($response);
106
    }
107
108
    /**
109
     * @param Throwable $throwable
110
     * @param array $options
111
     * @return CancellablePromiseInterface
112
     */
113
    public function error(Throwable $throwable, array $options = []): CancellablePromiseInterface
114
    {
115
        if (!isset($options[self::class][Options::ERROR_LEVEL])) {
116
            return reject($throwable);
117
        }
118
119
        $message = $throwable->getMessage();
120
121
        $response = null;
122
        if (method_exists($throwable, 'getResponse')) {
123
            $response = $throwable->getResponse();
124
        }
125
        if ($response instanceof ResponseInterface) {
126
            $this->addResponseToContext($response, $options);
127
        }
128
129
        $this->context[self::ERROR]['code']  = $throwable->getCode();
130
        $this->context[self::ERROR]['file']  = $throwable->getFile();
131
        $this->context[self::ERROR]['line']  = $throwable->getLine();
132
        $this->context[self::ERROR]['trace'] = $throwable->getTraceAsString();
133
134
        $this->logger->log($options[self::class][Options::ERROR_LEVEL], $message, $this->context);
135
136
        return reject($throwable);
137
    }
138
139
    /**
140
     * @param string $prefix
141
     * @param array $headers
142
     * @param array $ignoreHeaders
143
     */
144
    protected function iterateHeaders(string $prefix, array $headers, array $ignoreHeaders)
145
    {
146
        foreach ($headers as $header => $value) {
147
            if (in_array($header, $ignoreHeaders)) {
148
                continue;
149
            }
150
151
            $this->context[$prefix]['headers'][$header] = $value;
152
        }
153
    }
154
155
    private function addResponseToContext(ResponseInterface $response, array $options)
156
    {
157
        $this->context[self::RESPONSE]['status_code']      = $response->getStatusCode();
158
        $this->context[self::RESPONSE]['status_reason']    = $response->getReasonPhrase();
159
        $this->context[self::RESPONSE]['protocol_version'] = $response->getProtocolVersion();
160
        $ignoreHeaders = $options[self::class][Options::IGNORE_HEADERS] ?? [];
161
        $this->iterateHeaders(self::RESPONSE, $response->getHeaders(), $ignoreHeaders);
162
    }
163
}
164