Passed
Pull Request — master (#160)
by Dmitriy
07:03 queued 04:41
created

RequestCollector   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 64
dl 0
loc 97
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 9 1
A collect() 0 22 6
A getCollected() 0 29 5
A getIndexData() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector\Web;
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;
0 ignored issues
show
Coding Style introduced by
Header blocks must be separated by a single blank line
Loading history...
14
use Yiisoft\Yii\Debug\Collector\CollectorInterface;
0 ignored issues
show
Coding Style introduced by
Similar statements must be grouped together inside header blocks; the first "use" statement was found on line 7
Loading history...
15
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
16
use Yiisoft\Yii\Debug\Collector\IndexCollectorInterface;
17
18
final class RequestCollector implements CollectorInterface, IndexCollectorInterface
19
{
20
    use CollectorTrait;
21
22
    private string $requestUrl = '';
23
    private string $requestPath = '';
24
    private string $requestQuery = '';
25
    private string $requestMethod = '';
26
    private bool $requestIsAjax = false;
27
    private ?string $userIp = null;
28
    private int $responseStatusCode = 200;
29
    private ?ServerRequestInterface $request = null;
30
    private ?ResponseInterface $response = null;
31
32
    public function getCollected(): array
33
    {
34
        $body = null;
35
        if ($this->response !== null) {
36
            $stream = $this->response->getBody();
37
            if ($stream->isReadable() && $stream->isSeekable()) {
38
                $position = $stream->tell();
39
                $stream->rewind();
40
                $body = $stream->getContents();
41
                try {
42
                    $body = json_decode($body, associative: true, flags: JSON_THROW_ON_ERROR);
43
                } catch (JsonException) {
44
                    // pass
45
                }
46
                $stream->seek($position);
47
            }
48
        }
49
50
        return [
51
            'requestUrl' => $this->requestUrl,
52
            'requestPath' => $this->requestPath,
53
            'requestQuery' => $this->requestQuery,
54
            'requestMethod' => $this->requestMethod,
55
            'requestIsAjax' => $this->requestIsAjax,
56
            'userIp' => $this->userIp,
57
            'responseStatusCode' => $this->responseStatusCode,
58
            'request' => $this->request,
59
            'response' => $this->response,
60
            'responseRaw' => $body,
61
        ];
62
    }
63
64
    public function collect(object $event): void
65
    {
66
        if (!is_object($event) || !$this->isActive()) {
67
            return;
68
        }
69
70
        if ($event instanceof BeforeRequest) {
71
            $request = $event->getRequest();
72
73
            $this->request = $request;
74
            $this->requestUrl = (string) $request->getUri();
75
            $this->requestPath = $request->getUri()->getPath();
76
            $this->requestQuery = $request->getUri()->getQuery();
77
            $this->requestMethod = $request->getMethod();
78
            $this->requestIsAjax = strtolower($request->getHeaderLine('X-Requested-With')) === 'xmlhttprequest';
79
            $this->userIp = $request->getServerParams()['REMOTE_ADDR'] ?? null;
80
        }
81
        if ($event instanceof AfterRequest) {
82
            $response = $event->getResponse();
83
84
            $this->response = $response;
85
            $this->responseStatusCode = $response !== null ? $response->getStatusCode() : 500;
86
        }
87
    }
88
89
    public function getIndexData(): array
90
    {
91
        return [
92
            'request' => [
93
                'url' => $this->requestUrl,
94
                'path' => $this->requestPath,
95
                'query' => $this->requestQuery,
96
                'method' => $this->requestMethod,
97
                'isAjax' => $this->requestIsAjax,
98
                'userIp' => $this->userIp,
99
            ],
100
            'response' => [
101
                'statusCode' => $this->responseStatusCode,
102
            ],
103
        ];
104
    }
105
106
    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...
107
    {
108
        $this->request = null;
109
        $this->response = null;
110
        $this->requestUrl = '';
111
        $this->requestMethod = '';
112
        $this->requestIsAjax = false;
113
        $this->userIp = null;
114
        $this->responseStatusCode = 200;
115
    }
116
}
117