Passed
Push — master ( f7ef7a...65c26d )
by Dmitriy
02:46
created

RequestCollector   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 74
dl 0
loc 111
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
B getCollected() 0 43 7
A getSummary() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector\Web;
6
7
use GuzzleHttp\Psr7\Message;
8
use JsonException;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Yiisoft\Yii\Http\Event\AfterRequest;
12
use Yiisoft\Yii\Http\Event\BeforeRequest;
13
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
14
use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface;
15
16
use function is_object;
17
18
final class RequestCollector implements SummaryCollectorInterface
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
        $content = 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
                $content = $stream->getContents();
41
                try {
42
                    $content = json_decode($content, associative: true, flags: JSON_THROW_ON_ERROR);
43
                } catch (JsonException) {
44
                    // pass
45
                }
46
                $stream->seek($position);
47
            }
48
        }
49
50
        $requestRaw = null;
51
        if ($this->request instanceof ServerRequestInterface) {
52
            $requestRaw = Message::toString($this->request);
53
            Message::rewindBody($this->request);
54
        }
55
56
        $responseRaw = null;
57
        if ($this->response instanceof ResponseInterface) {
58
            $responseRaw = Message::toString($this->response);
59
            Message::rewindBody($this->response);
60
        }
61
62
        return [
63
            'requestUrl' => $this->requestUrl,
64
            'requestPath' => $this->requestPath,
65
            'requestQuery' => $this->requestQuery,
66
            'requestMethod' => $this->requestMethod,
67
            'requestIsAjax' => $this->requestIsAjax,
68
            'userIp' => $this->userIp,
69
            'responseStatusCode' => $this->responseStatusCode,
70
            'request' => $this->request,
71
            'requestRaw' => $requestRaw,
72
            'response' => $this->response,
73
            'responseRaw' => $responseRaw,
74
            'content' => $content,
75
        ];
76
    }
77
78
    public function collect(object $event): void
79
    {
80
        if (!is_object($event) || !$this->isActive()) {
81
            return;
82
        }
83
84
        if ($event instanceof BeforeRequest) {
85
            $request = $event->getRequest();
86
87
            $this->request = $request;
88
            $this->requestUrl = (string) $request->getUri();
89
            $this->requestPath = $request->getUri()->getPath();
90
            $this->requestQuery = $request->getUri()->getQuery();
91
            $this->requestMethod = $request->getMethod();
92
            $this->requestIsAjax = strtolower($request->getHeaderLine('X-Requested-With')) === 'xmlhttprequest';
93
            $this->userIp = $request->getServerParams()['REMOTE_ADDR'] ?? null;
94
        }
95
        if ($event instanceof AfterRequest) {
96
            $response = $event->getResponse();
97
98
            $this->response = $response;
99
            $this->responseStatusCode = $response !== null ? $response->getStatusCode() : 500;
100
        }
101
    }
102
103
    public function getSummary(): array
104
    {
105
        return [
106
            'request' => [
107
                'url' => $this->requestUrl,
108
                'path' => $this->requestPath,
109
                'query' => $this->requestQuery,
110
                'method' => $this->requestMethod,
111
                'isAjax' => $this->requestIsAjax,
112
                'userIp' => $this->userIp,
113
            ],
114
            'response' => [
115
                'statusCode' => $this->responseStatusCode,
116
            ],
117
        ];
118
    }
119
120
    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...
121
    {
122
        $this->request = null;
123
        $this->response = null;
124
        $this->requestUrl = '';
125
        $this->requestMethod = '';
126
        $this->requestIsAjax = false;
127
        $this->userIp = null;
128
        $this->responseStatusCode = 200;
129
    }
130
}
131