Passed
Push — master ( ba4e4b...37ef79 )
by Alexander
02:56
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
wmc 8
eloc 34
c 8
b 0
f 0
dl 0
loc 58
ccs 24
cts 31
cp 0.7742
rs 10

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