Passed
Pull Request — master (#160)
by Dmitriy
04:52 queued 02:01
created

RequestCollector::getCollected()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 43
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 43
rs 8.4746
c 0
b 0
f 0
cc 7
nc 16
nop 0
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
14
use function is_object;
0 ignored issues
show
Coding Style introduced by
Header blocks must be separated by a single blank line
Loading history...
15
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...
16
use Yiisoft\Yii\Debug\Collector\CollectorTrait;
17
use Yiisoft\Yii\Debug\Collector\IndexCollectorInterface;
18
19
final class RequestCollector implements CollectorInterface, IndexCollectorInterface
20
{
21
    use CollectorTrait;
22
23
    private string $requestUrl = '';
24
    private string $requestPath = '';
25
    private string $requestQuery = '';
26
    private string $requestMethod = '';
27
    private bool $requestIsAjax = false;
28
    private ?string $userIp = null;
29
    private int $responseStatusCode = 200;
30
    private ?ServerRequestInterface $request = null;
31
    private ?ResponseInterface $response = null;
32
33
    public function getCollected(): array
34
    {
35
        $content = null;
36
        if ($this->response !== null) {
37
            $stream = $this->response->getBody();
38
            if ($stream->isReadable() && $stream->isSeekable()) {
39
                $position = $stream->tell();
40
                $stream->rewind();
41
                $content = $stream->getContents();
42
                try {
43
                    $content = json_decode($content, associative: true, flags: JSON_THROW_ON_ERROR);
44
                } catch (JsonException) {
45
                    // pass
46
                }
47
                $stream->seek($position);
48
            }
49
        }
50
51
        $requestRaw = null;
52
        if ($this->request instanceof ServerRequestInterface) {
53
            $requestRaw = Message::toString($this->request);
54
            Message::rewindBody($this->request);
55
        }
56
57
        $responseRaw = null;
58
        if ($this->response instanceof ResponseInterface) {
59
            $responseRaw = Message::toString($this->response);
60
            Message::rewindBody($this->response);
61
        }
62
63
        return [
64
            'requestUrl' => $this->requestUrl,
65
            'requestPath' => $this->requestPath,
66
            'requestQuery' => $this->requestQuery,
67
            'requestMethod' => $this->requestMethod,
68
            'requestIsAjax' => $this->requestIsAjax,
69
            'userIp' => $this->userIp,
70
            'responseStatusCode' => $this->responseStatusCode,
71
            'request' => $this->request,
72
            'requestRaw' => $requestRaw,
73
            'response' => $this->response,
74
            'responseRaw' => $responseRaw,
75
            'content' => $content,
76
        ];
77
    }
78
79
    public function collect(object $event): void
80
    {
81
        if (!is_object($event) || !$this->isActive()) {
82
            return;
83
        }
84
85
        if ($event instanceof BeforeRequest) {
86
            $request = $event->getRequest();
87
88
            $this->request = $request;
89
            $this->requestUrl = (string) $request->getUri();
90
            $this->requestPath = $request->getUri()->getPath();
91
            $this->requestQuery = $request->getUri()->getQuery();
92
            $this->requestMethod = $request->getMethod();
93
            $this->requestIsAjax = strtolower($request->getHeaderLine('X-Requested-With')) === 'xmlhttprequest';
94
            $this->userIp = $request->getServerParams()['REMOTE_ADDR'] ?? null;
95
        }
96
        if ($event instanceof AfterRequest) {
97
            $response = $event->getResponse();
98
99
            $this->response = $response;
100
            $this->responseStatusCode = $response !== null ? $response->getStatusCode() : 500;
101
        }
102
    }
103
104
    public function getIndexData(): array
105
    {
106
        return [
107
            'request' => [
108
                'url' => $this->requestUrl,
109
                'path' => $this->requestPath,
110
                'query' => $this->requestQuery,
111
                'method' => $this->requestMethod,
112
                'isAjax' => $this->requestIsAjax,
113
                'userIp' => $this->userIp,
114
            ],
115
            'response' => [
116
                'statusCode' => $this->responseStatusCode,
117
            ],
118
        ];
119
    }
120
121
    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...
122
    {
123
        $this->request = null;
124
        $this->response = null;
125
        $this->requestUrl = '';
126
        $this->requestMethod = '';
127
        $this->requestIsAjax = false;
128
        $this->userIp = null;
129
        $this->responseStatusCode = 200;
130
    }
131
}
132