Passed
Pull Request — master (#170)
by Rustam
13:54
created

RequestCollector::getCollected()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.1338

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
c 2
b 0
f 0
dl 0
loc 31
ccs 13
cts 19
cp 0.6842
rs 8.9137
cc 6
nc 8
nop 0
crap 7.1338
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
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;
15
16
final class RequestCollector implements CollectorInterface, IndexCollectorInterface
17
{
18
    use CollectorTrait;
19
20
    private string $requestUrl = '';
21
    private string $requestPath = '';
22
    private string $requestQuery = '';
23
    private string $requestMethod = '';
24
    private bool $requestIsAjax = false;
25
    private ?string $userIp = null;
26
    private int $responseStatusCode = 200;
27
    private ?ServerRequestInterface $request = null;
28
    private ?ResponseInterface $response = null;
29 1
30
    public function getCollected(): array
31 1
    {
32 1
        $content = null;
33 1
        if ($this->response !== null) {
34 1
            $stream = $this->response->getBody();
35
            if ($stream->isReadable() && $stream->isSeekable()) {
36
                $position = $stream->tell();
37
                $stream->rewind();
38
                $content = $stream->getContents();
39
                try {
40
                    $content = json_decode($content, associative: true, flags: JSON_THROW_ON_ERROR);
41
                } catch (JsonException) {
42
                    // pass
43
                }
44
                $stream->seek($position);
45
            }
46
        }
47
48 1
        return [
49 1
            'requestUrl' => $this->requestUrl,
50 1
            'requestPath' => $this->requestPath,
51 1
            'requestQuery' => $this->requestQuery,
52 1
            'requestMethod' => $this->requestMethod,
53 1
            'requestIsAjax' => $this->requestIsAjax,
54 1
            'userIp' => $this->userIp,
55 1
            'responseStatusCode' => $this->responseStatusCode,
56 1
            'request' => $this->request,
57
            'requestRaw' => Message::toString($this->request),
0 ignored issues
show
Bug introduced by
It seems like $this->request can also be of type null; however, parameter $message of GuzzleHttp\Psr7\Message::toString() does only seem to accept Psr\Http\Message\MessageInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
            'requestRaw' => Message::toString(/** @scrutinizer ignore-type */ $this->request),
Loading history...
58
            'response' => $this->response,
59
            'responseRaw' => $this->response instanceof ResponseInterface ? Message::toString($this->response) : null,
60
            'content' => $content,
61 1
        ];
62
    }
63 1
64
    public function collect(object $event): void
65
    {
66
        if (!is_object($event) || !$this->isActive()) {
67 1
            return;
68 1
        }
69
70 1
        if ($event instanceof BeforeRequest) {
71 1
            $request = $event->getRequest();
72 1
73 1
            $this->request = $request;
74 1
            $this->requestUrl = (string) $request->getUri();
75 1
            $this->requestPath = $request->getUri()->getPath();
76 1
            $this->requestQuery = $request->getUri()->getQuery();
77
            $this->requestMethod = $request->getMethod();
78 1
            $this->requestIsAjax = strtolower($request->getHeaderLine('X-Requested-With')) === 'xmlhttprequest';
79 1
            $this->userIp = $request->getServerParams()['REMOTE_ADDR'] ?? null;
80
        }
81 1
        if ($event instanceof AfterRequest) {
82 1
            $response = $event->getResponse();
83
84
            $this->response = $response;
85
            $this->responseStatusCode = $response !== null ? $response->getStatusCode() : 500;
86 1
        }
87
    }
88
89
    public function getIndexData(): array
90 1
    {
91 1
        return [
92 1
            'request' => [
93 1
                'url' => $this->requestUrl,
94 1
                'path' => $this->requestPath,
95 1
                'query' => $this->requestQuery,
96
                'method' => $this->requestMethod,
97
                'isAjax' => $this->requestIsAjax,
98 1
                'userIp' => $this->userIp,
99
            ],
100
            'response' => [
101
                'statusCode' => $this->responseStatusCode,
102
            ],
103 1
        ];
104
    }
105 1
106 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...
107 1
    {
108 1
        $this->request = null;
109 1
        $this->response = null;
110 1
        $this->requestUrl = '';
111 1
        $this->requestMethod = '';
112
        $this->requestIsAjax = false;
113
        $this->userIp = null;
114
        $this->responseStatusCode = 200;
115
    }
116
}
117