Passed
Pull Request — master (#118)
by Rustam
16:25
created

RequestCollector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 56
c 8
b 0
f 0
dl 0
loc 82
ccs 34
cts 35
cp 0.9714
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndexData() 0 15 1
A collect() 0 18 6
A getCollected() 0 19 1
A reset() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use JetBrains\PhpStorm\ArrayShape;
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 $requestMethod = '';
21
    private bool $requestIsAjax = false;
22
    private ?string $userIp = null;
23
    private int $responseStatusCode = 200;
24
    private ?ServerRequestInterface $request = null;
25
    private ?ResponseInterface $response = null;
26
27 1
    #[ArrayShape([
28
        'requestUrl' => 'string',
29
        'requestMethod' => 'string',
30
        'requestIsAjax' => 'bool',
31
        'userIp' => 'null|string',
32
        'responseStatusCode' => 'int',
33
        'request' => "null|\Psr\Http\Message\ServerRequestInterface",
34
        'response' => "null|\Psr\Http\Message\ResponseInterface",
35
    ])]
36
    public function getCollected(): array
37
    {
38
        return [
39 1
            'requestUrl' => $this->requestUrl,
40 1
            'requestMethod' => $this->requestMethod,
41 1
            'requestIsAjax' => $this->requestIsAjax,
42 1
            'userIp' => $this->userIp,
43 1
            'responseStatusCode' => $this->responseStatusCode,
44 1
            'request' => $this->request,
45 1
            'response' => $this->response,
46
        ];
47
    }
48
49 1
    public function collect(object $event): void
50
    {
51 1
        if (!is_object($event) || !$this->isActive()) {
52
            return;
53
        }
54
55 1
        if ($event instanceof BeforeRequest) {
56 1
            $this->request = $event->getRequest();
57 1
            $this->requestUrl = (string)$event->getRequest()->getUri();
58 1
            $this->requestMethod = $event->getRequest()->getMethod();
59 1
            $this->requestIsAjax = strtolower(
60 1
                $event->getRequest()->getHeaderLine('X-Requested-With') ?? ''
61
            ) === 'xmlhttprequest';
62 1
            $this->userIp = $event->getRequest()->getServerParams()['REMOTE_ADDR'] ?? null;
63
        }
64 1
        if ($event instanceof AfterRequest) {
65 1
            $this->response = $event->getResponse();
66 1
            $this->responseStatusCode = $event->getResponse() !== null ? $event->getResponse()->getStatusCode() : 500;
67
        }
68
    }
69
70 1
    #[ArrayShape([
71
        'requestUrl' => 'string',
72
        'requestMethod' => 'string',
73
        'requestIsAjax' => 'bool',
74
        'userIp' => 'null|string',
75
        'responseStatusCode' => 'int',
76
    ])]
77
    public function getIndexData(): array
78
    {
79
        return [
80 1
            'requestUrl' => $this->requestUrl,
81 1
            'requestMethod' => $this->requestMethod,
82 1
            'requestIsAjax' => $this->requestIsAjax,
83 1
            'userIp' => $this->userIp,
84 1
            'responseStatusCode' => $this->responseStatusCode,
85
        ];
86
    }
87
88 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...
89
    {
90 1
        $this->request = null;
91 1
        $this->response = null;
92 1
        $this->requestUrl = '';
93 1
        $this->requestMethod = '';
94 1
        $this->requestIsAjax = false;
95 1
        $this->userIp = null;
96 1
        $this->responseStatusCode = 200;
97
    }
98
}
99