Passed
Push — master ( cf37f2...bd53e8 )
by Alexander
02:23
created

RequestCollector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 34
c 8
b 0
f 0
dl 0
loc 58
ccs 30
cts 31
cp 0.9677
rs 10
wmc 9

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