BrowserDataAppending::onKernelRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dziki\MonologSentryBundle\SubscribedProcessor;
6
7
use Dziki\MonologSentryBundle\UserAgent\ParserInterface;
8
use Dziki\MonologSentryBundle\UserAgent\UserAgent;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Symfony\Component\HttpKernel\Event\RequestEvent;
11
use Symfony\Component\HttpKernel\KernelEvents;
12
13
class BrowserDataAppending implements EventSubscriberInterface
14
{
15
    /** @var UserAgent */
16
    private $userAgent;
17
    /**
18
     * @var ParserInterface
19
     */
20
    private $parser;
21
22 1
    public function __construct(ParserInterface $parser)
23
    {
24 1
        $this->parser = $parser;
25 1
    }
26
27 1
    public static function getSubscribedEvents(): array
28
    {
29
        return [
30 1
            KernelEvents::REQUEST => ['onKernelRequest', 1024],
31
        ];
32
    }
33
34
    public function __invoke(array $record): array
35
    {
36
        if ($this->userAgent) {
37
            $record['contexts']['browser'] = [
38
                'name' => $this->userAgent->getBrowseName(),
39
                'version' => $this->userAgent->getBrowserVersion(),
40
            ];
41
            $record['contexts']['os'] = ['name' => $this->userAgent->getPlatform()];
42
        }
43
44
        return $record;
45
    }
46
47
    public function onKernelRequest(RequestEvent $event): void
48
    {
49
        /** @var string $userAgent */
50
        $userAgent = $event->getRequest()->headers->get('User-Agent', '');
51
52
        $this->userAgent = $this->parser->parse($userAgent);
53
    }
54
}
55