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

UsernameProcessor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 27
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUsername() 0 10 3
A __construct() 0 2 1
A __invoke() 0 4 1
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
}