Passed
Pull Request — master (#79)
by Rustam
02:44
created

RequestCollector   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 77.42%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 35
c 8
b 0
f 0
dl 0
loc 58
ccs 24
cts 31
cp 0.7742
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollected() 0 8 1
A reset() 0 7 1
A getIndexData() 0 8 1
A collect() 0 16 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use Yiisoft\Yii\Web\Event\AfterRequest;
8
use Yiisoft\Yii\Web\Event\BeforeRequest;
9
10
final class RequestCollector implements CollectorInterface, IndexCollectorInterface
11
{
12
    use CollectorTrait;
13
14
    private string $requestUrl = '';
15
    private string $requestMethod = '';
16
    private bool $requestIsAjax = false;
17
    private ?string $userIp = null;
18
    private int $responseStatusCode = 200;
19
20 1
    public function getCollected(): array
21
    {
22
        return [
23 1
            'request_url' => $this->requestUrl,
24 1
            'request_method' => $this->requestMethod,
25 1
            'request_is_ajax' => $this->requestIsAjax,
26 1
            'user_ip' => $this->userIp,
27 1
            'response_status_code' => $this->responseStatusCode,
28
        ];
29
    }
30
31 1
    public function collect(object $event): void
32
    {
33 1
        if (!is_object($event) || !$this->isActive()) {
34
            return;
35
        }
36
37 1
        if ($event instanceof BeforeRequest) {
38 1
            $this->requestUrl = (string)$event->getRequest()->getUri();
39 1
            $this->requestMethod = $event->getRequest()->getMethod();
40 1
            $this->requestIsAjax = strtolower(
41 1
                $event->getRequest()->getHeaderLine('X-Requested-With') ?? ''
42 1
            ) === 'xmlhttprequest';
43 1
            $this->userIp = $event->getRequest()->getServerParams()['REMOTE_ADDR'] ?? null;
44
        }
45 1
        if ($event instanceof AfterRequest) {
46 1
            $this->responseStatusCode = $event->getResponse()->getStatusCode();
47
        }
48 1
    }
49
50
    public function getIndexData(): array
51
    {
52
        return [
53
            'requestUrl' => $this->requestUrl,
54
            'requestMethod' => $this->requestMethod,
55
            'requestIsAjax' => $this->requestIsAjax,
56
            'userIp' => $this->userIp,
57
            'responseStatusCode' => $this->responseStatusCode,
58
        ];
59
    }
60
61 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...
62
    {
63 1
        $this->requestUrl = '';
64 1
        $this->requestMethod = '';
65 1
        $this->requestIsAjax = false;
66 1
        $this->userIp = null;
67 1
        $this->responseStatusCode = 200;
68 1
    }
69
}
70