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

RequestCollector::collect()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
eloc 11
c 3
b 0
f 0
nc 7
nop 1
dl 0
loc 16
ccs 11
cts 12
cp 0.9167
crap 6.0208
rs 9.2222
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