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

RequestCollector::collect()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.0106

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 15
c 5
b 0
f 0
dl 0
loc 20
ccs 14
cts 15
cp 0.9333
rs 9.2222
cc 6
nc 7
nop 1
crap 6.0106
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