Passed
Pull Request — master (#153)
by Dmitriy
12:02 queued 09:39
created

RequestCollector   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 86.54%

Importance

Changes 12
Bugs 0 Features 0
Metric Value
wmc 13
eloc 64
c 12
b 0
f 0
dl 0
loc 97
ccs 45
cts 52
cp 0.8654
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollected() 0 29 5
A reset() 0 9 1
A getIndexData() 0 13 1
A collect() 0 22 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use JsonException;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Yiisoft\Yii\Http\Event\AfterRequest;
11
use Yiisoft\Yii\Http\Event\BeforeRequest;
12
13
use function is_object;
14
15
final class RequestCollector implements CollectorInterface, IndexCollectorInterface
16
{
17
    use CollectorTrait;
18
19
    private string $requestUrl = '';
20
    private string $requestPath = '';
21
    private string $requestQuery = '';
22
    private string $requestMethod = '';
23
    private bool $requestIsAjax = false;
24
    private ?string $userIp = null;
25
    private int $responseStatusCode = 200;
26
    private ?ServerRequestInterface $request = null;
27
    private ?ResponseInterface $response = null;
28
29 1
    public function getCollected(): array
30
    {
31 1
        $body = null;
32 1
        if ($this->response !== null) {
33 1
            $stream = $this->response->getBody();
34 1
            if ($stream->isReadable() && $stream->isSeekable()) {
35
                $position = $stream->tell();
36
                $stream->rewind();
37
                $body = $stream->getContents();
38
                try {
39
                    $body = json_decode($body, associative: true, flags: JSON_THROW_ON_ERROR);
40
                } catch (JsonException) {
41
                    // pass
42
                }
43
                $stream->seek($position);
44
            }
45
        }
46
47
        return [
48 1
            'requestUrl' => $this->requestUrl,
49 1
            'requestPath' => $this->requestPath,
50 1
            'requestQuery' => $this->requestQuery,
51 1
            'requestMethod' => $this->requestMethod,
52 1
            'requestIsAjax' => $this->requestIsAjax,
53 1
            'userIp' => $this->userIp,
54 1
            'responseStatusCode' => $this->responseStatusCode,
55 1
            'request' => $this->request,
56 1
            'response' => $this->response,
57
            'responseRaw' => $body,
58
        ];
59
    }
60
61 1
    public function collect(object $event): void
62
    {
63 1
        if (!is_object($event) || !$this->isActive()) {
64
            return;
65
        }
66
67 1
        if ($event instanceof BeforeRequest) {
68 1
            $request = $event->getRequest();
69
70 1
            $this->request = $request;
71 1
            $this->requestUrl = (string) $request->getUri();
72 1
            $this->requestPath = $request->getUri()->getPath();
73 1
            $this->requestQuery = $request->getUri()->getQuery();
74 1
            $this->requestMethod = $request->getMethod();
75 1
            $this->requestIsAjax = strtolower($request->getHeaderLine('X-Requested-With')) === 'xmlhttprequest';
76 1
            $this->userIp = $request->getServerParams()['REMOTE_ADDR'] ?? null;
77
        }
78 1
        if ($event instanceof AfterRequest) {
79 1
            $response = $event->getResponse();
80
81 1
            $this->response = $response;
82 1
            $this->responseStatusCode = $response !== null ? $response->getStatusCode() : 500;
83
        }
84
    }
85
86 1
    public function getIndexData(): array
87
    {
88
        return [
89
            'request' => [
90 1
                'url' => $this->requestUrl,
91 1
                'path' => $this->requestPath,
92 1
                'query' => $this->requestQuery,
93 1
                'method' => $this->requestMethod,
94 1
                'isAjax' => $this->requestIsAjax,
95 1
                'userIp' => $this->userIp,
96
            ],
97
            'response' => [
98 1
                'statusCode' => $this->responseStatusCode,
99
            ],
100
        ];
101
    }
102
103 1
    private function reset(): void
0 ignored issues
show
Unused Code introduced by
The method reset() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
104
    {
105 1
        $this->request = null;
106 1
        $this->response = null;
107 1
        $this->requestUrl = '';
108 1
        $this->requestMethod = '';
109 1
        $this->requestIsAjax = false;
110 1
        $this->userIp = null;
111 1
        $this->responseStatusCode = 200;
112
    }
113
}
114