Completed
Push — symfony3-validation-fail ( 1a4398 )
by Kamil
26:23 queued 08:51
created

UserImpersonator   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A impersonate() 0 7 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\CoreBundle\Security;
13
14
use Symfony\Component\HttpFoundation\Session\Session;
15
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
16
use Symfony\Component\Security\Core\User\UserInterface;
17
18
/**
19
 * @author Jan Góralski <[email protected]>
20
 */
21
final class UserImpersonator implements UserImpersonatorInterface
22
{
23
    /**
24
     * @var Session
25
     */
26
    private $session;
27
28
    /**
29
     * @var string
30
     */
31
    private $sessionTokenParameter;
32
33
    /**
34
     * @param Session $session
35
     * @param string $firewallContextName
36
     */
37
    public function __construct(Session $session, $firewallContextName)
38
    {
39
        $this->session = $session;
40
        $this->sessionTokenParameter = sprintf('_security_%s', $firewallContextName);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function impersonate(UserInterface $user)
47
    {
48
        $token = new UsernamePasswordToken($user, $user->getPassword(), $this->sessionTokenParameter, $user->getRoles());
49
50
        $this->session->set($this->sessionTokenParameter, serialize($token));
51
        $this->session->save();
52
    }
53
}
54