Passed
Push — master ( afa595...3d30f2 )
by Marcel
08:58
created

UsernameProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
namespace App\Monolog;
4
5
use Monolog\Processor\ProcessorInterface;
6
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
7
8
class UsernameProcessor implements ProcessorInterface {
9
10
    private ?string $username = null;
11
12
    public function __construct(private TokenStorageInterface $tokenStorage)
13
    {
14
    }
15
16
    /**
17
     * @inheritDoc
18
     */
19
    public function __invoke(array $record): array {
20
        $record['extra']['username'] = $this->getUsername();
21
22
        return $record;
23
    }
24
25
    private function getUsername(): ?string {
26
        if($this->username === null) {
27
            $token = $this->tokenStorage->getToken();
28
29
            if($token !== null) {
30
                $this->username = $token->getUserIdentifier();
31
            }
32
        }
33
34
        return $this->username;
35
    }
36
}