Passed
Pull Request — master (#119)
by Rustam
02:49
created

RequestCollector::getIndexData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 15
ccs 6
cts 6
cp 1
crap 1
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Collector;
6
7
use JetBrains\PhpStorm\ArrayShape;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Yiisoft\Yii\Http\Event\AfterRequest;
11
use Yiisoft\Yii\Http\Event\BeforeRequest;
12
13
use function is_object;
14
15
final class RequestCollector implements CollectorInterface, IndexCollectorInterface
16
{
17
    use CollectorTrait;
18
19
    private string $requestUrl = '';
20
    private string $requestMethod = '';
21
    private bool $requestIsAjax = false;
22
    private ?string $userIp = null;
23
    private int $responseStatusCode = 200;
24
    private ?ServerRequestInterface $request = null;
25
    private ?ResponseInterface $response = null;
26
27 1
    #[ArrayShape([
28
        'request' => "null|\Psr\Http\Message\ServerRequestInterface",
29
        'response' => "null|\Psr\Http\Message\ResponseInterface",
30
    ])]
31
    public function getCollected(): array
32
    {
33
        return [
34 1
            'request' => $this->request,
35 1
            'response' => $this->response,
36
        ];
37
    }
38
39 1
    public function collect(object $event): void
40
    {
41 1
        if (!is_object($event) || !$this->isActive()) {
42
            return;
43
        }
44
45 1
        if ($event instanceof BeforeRequest) {
46 1
            $this->request = $event->getRequest();
47 1
            $this->requestUrl = (string)$event->getRequest()->getUri();
48 1
            $this->requestMethod = $event->getRequest()->getMethod();
49 1
            $this->requestIsAjax = strtolower(
50 1
                $event->getRequest()->hasHeader('X-Requested-With') ?? ''
0 ignored issues
show
Bug introduced by
It seems like $event->getRequest()->ha...-Requested-With') ?? '' can also be of type boolean; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
                /** @scrutinizer ignore-type */ $event->getRequest()->hasHeader('X-Requested-With') ?? ''
Loading history...
51
            ) === 'xmlhttprequest';
52 1
            $this->userIp = $event->getRequest()->getServerParams()['REMOTE_ADDR'] ?? null;
53
        }
54 1
        if ($event instanceof AfterRequest) {
55 1
            $this->response = $event->getResponse();
56 1
            $this->responseStatusCode = $event->getResponse() !== null ? $event->getResponse()->getStatusCode() : 500;
57
        }
58
    }
59
60 1
    #[ArrayShape([
61
        'requestUrl' => 'string',
62
        'requestMethod' => 'string',
63
        'requestIsAjax' => 'bool',
64
        'userIp' => 'null|string',
65
        'responseStatusCode' => 'int',
66
    ])]
67
    public function getIndexData(): array
68
    {
69
        return [
70 1
            'requestUrl' => $this->requestUrl,
71 1
            'requestMethod' => $this->requestMethod,
72 1
            'requestIsAjax' => $this->requestIsAjax,
73 1
            'userIp' => $this->userIp,
74 1
            'responseStatusCode' => $this->responseStatusCode,
75
        ];
76
    }
77
78 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...
79
    {
80 1
        $this->requestUrl = '';
81 1
        $this->requestMethod = '';
82 1
        $this->requestIsAjax = false;
83 1
        $this->userIp = null;
84 1
        $this->responseStatusCode = 200;
85
    }
86
}
87