Passed
Branch main (6b08b1)
by MusikAnimal
10:10
created

WebProcessorMonolog::processRecord()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
nc 3
nop 1
dl 0
loc 22
ccs 11
cts 12
cp 0.9167
crap 4.0092
rs 9.8666
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the WebProcessorMonolog class.
4
 */
5
6
declare(strict_types = 1);
7
8
namespace AppBundle\Monolog;
9
10
use Symfony\Component\HttpFoundation\RequestStack;
11
12
/**
13
 * WebProcessorMonolog extends information included in error reporting.
14
 */
15
class WebProcessorMonolog
16
{
17
    /** @var RequestStack The request stack. */
18
    private $requestStack;
19
20
    /** @var string The unique identifier for the session. */
21
    private $sessionId;
0 ignored issues
show
introduced by
The private property $sessionId is not used, and could be removed.
Loading history...
22
23
    /**
24
     * WebProcessorMonolog constructor.
25
     * @param RequestStack $requestStack
26
     */
27 34
    public function __construct(RequestStack $requestStack)
28
    {
29 34
        $this->requestStack = $requestStack;
30 34
    }
31
32
    /**
33
     * Adds extra information to the log entry.
34
     * @param array $record
35
     * @return array
36
     */
37 16
    public function processRecord(array $record): array
38
    {
39 16
        $request = $this->requestStack->getCurrentRequest();
40
41 16
        if ($request && $request->hasSession()) {
42 14
            $record['extra']['host'] = $request->getHost();
43 14
            $record['extra']['uri'] = $request->getUri();
44 14
            $record['extra']['useragent'] = $request->headers->get('User-Agent');
45 14
            $record['extra']['referer'] = $request->headers->get('referer');
46
47 14
            $session = $request->getSession();
48
49
            // Necessary to combat abuse.
50 14
            if (null !== $session->get('logged_in_user')) {
51
                $record['extra']['username'] = $session->get('logged_in_user')->username;
52
            } else {
53
                // Intentionally not included if we have a username, for privacy reasons.
54 14
                $record['extra']['xff'] = $request->headers->get('x-forwarded-for', '');
55
            }
56
        }
57
58 16
        return $record;
59
    }
60
}
61