Passed
Push — master ( 91e81d...94d9d8 )
by MusikAnimal
07:34
created

WebProcessorMonolog   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 50
ccs 16
cts 18
cp 0.8889
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B processRecord() 0 28 8
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;
22
23
    /**
24
     * WebProcessorMonolog constructor.
25
     * @param RequestStack $requestStack
26
     */
27 27
    public function __construct(RequestStack $requestStack)
28
    {
29 27
        $this->requestStack = $requestStack;
30 27
    }
31
32
    /**
33
     * Adds extra information to the log entry.
34
     * @param array $record
35
     * @return array
36
     */
37 17
    public function processRecord(array $record): array
38
    {
39 17
        $request = $this->requestStack->getCurrentRequest();
40
41 17
        if ($request && $request->hasSession()) {
42 15
            $record['extra']['host'] = $request->getHost();
43 15
            $record['extra']['uri'] = $request->getUri();
44 15
            $record['extra']['useragent'] = $request->headers->get('User-Agent');
45
46 15
            $session = $request->getSession();
47
48
            // Necessary to combat abuse.
49 15
            if (null !== $session->get('logged_in_user')) {
50
                $record['extra']['username'] = $session->get('logged_in_user');
51
            }
52
53 15
            if (null === $session || !$session->isStarted()) {
54
                return $record;
55
            }
56
57 15
            if (!$this->sessionId) {
58 15
                $this->sessionId = substr($session->getId(), 0, 8) ?: '????????';
59
            }
60
61 15
            $record['extra']['token'] = $this->sessionId.'-'.substr(uniqid('', true), -8);
62
        }
63
64 17
        return $record;
65
    }
66
}
67