Passed
Pull Request — master (#133)
by Rustam
03:14
created

RequestCollector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 97.56%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 9
eloc 52
c 10
b 0
f 0
dl 0
loc 78
ccs 40
cts 41
cp 0.9756
rs 10

4 Methods

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