Completed
Push — master ( d964e4...900267 )
by Jeroen
23:03 queued 15:42
created

UserProcessor::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 6
cts 8
cp 0.75
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Helper;
4
5
use Symfony\Component\DependencyInjection\ContainerInterface;
6
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
7
use Symfony\Component\Security\Core\User\UserInterface;
8
9
/**
10
 * Adds the user information to the context of the record which will be logged
11
 */
12
class UserProcessor
13
{
14
    /**
15
     * Use container else we have a continous loop in our dependency
16
     *
17
     * @var ContainerInterface
18
     */
19
    private $container;
20
21
    /**
22
     * @var UserInterface
23
     */
24
    private $user;
25
26
    /**
27
     * @var array
28
     */
29
    private $record = array();
30
31
    private $tokenStorage;
32
33
    /**
34
     * @param ContainerInterface|TokenStorageInterface $tokenStorage
35
     */
36 1 View Code Duplication
    public function __construct(/*TokenStorageInterface */ $tokenStorage)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38 1
        if ($tokenStorage instanceof ContainerInterface) {
39 1
            @trigger_error(sprintf('Passing the container as the first argument of "%s" is deprecated in KunstmaanAdminBundle 5.4 and will be removed in KunstmaanAdminBundle 6.0. Inject the "security.token_storage" service instead.', __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
40
41 1
            $this->container = $tokenStorage;
42 1
            $this->tokenStorage = $this->container->get('security.token_storage');
43
44 1
            return;
45
        }
46
47
        $this->tokenStorage = $tokenStorage;
48
    }
49
50
    /**
51
     * @param array $record
52
     *
53
     * @return array
54
     */
55
    public function processRecord(array $record)
56
    {
57
        if (\is_null($this->user)) {
58
            if (($this->tokenStorage !== null) && ($this->tokenStorage->getToken() !== null) && ($this->tokenStorage->getToken()->getUser() instanceof \Symfony\Component\Security\Core\User\AdvancedUserInterface)) {
59
                $this->user = $this->tokenStorage->getToken()->getUser();
60
                $this->record['extra']['user']['username'] = $this->user->getUsername();
61
                $this->record['extra']['user']['roles'] = $this->user->getRoles();
62
                $this->record['extra']['user']['is_account_non_expired'] = $this->user->isAccountNonExpired();
63
                $this->record['extra']['user']['is_account_non_locked'] = $this->user->isAccountNonLocked();
64
                $this->record['extra']['user']['is_credentials_non_expired'] = $this->user->isCredentialsNonExpired();
65
                $this->record['extra']['user']['is_enabled'] = $this->user->isEnabled();
66
            }
67
        }
68
69
        return array_merge($record, $this->record);
70
    }
71
}
72