Completed
Push — master ( a4efda...49f2e8 )
by David
15s queued 11s
created

src/UserContext/RoleProvider.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCacheBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\HttpCacheBundle\UserContext;
13
14
use FOS\HttpCache\UserContext\ContextProvider;
15
use FOS\HttpCache\UserContext\UserContext;
16
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
use Symfony\Component\Security\Core\Role\Role;
19
20
/**
21
 * The RoleProvider adds roles to the UserContext for the hash generation.
22
 */
23
class RoleProvider implements ContextProvider
24
{
25
    /**
26
     * @var TokenStorageInterface|null
27
     */
28
    private $tokenStorage;
29
30
    /**
31
     * Create the role provider with a security context.
32
     *
33
     * The token storage is optional to not fail on routes that have no
34
     * firewall. It is however not valid to call updateUserContext when not in
35
     * a firewall context.
36
     *
37
     * @param TokenStorageInterface $tokenStorage
38
     */
39 24
    public function __construct(TokenStorageInterface $tokenStorage = null)
40
    {
41 24
        $this->tokenStorage = $tokenStorage;
42 24
    }
43
44
    /**
45
     * {@inheritdoc}
46
     *
47
     * @throws InvalidConfigurationException when called without a security context being set
48
     */
49 5
    public function updateUserContext(UserContext $context)
50
    {
51 5
        if (null === $this->tokenStorage) {
52 1
            throw new InvalidConfigurationException('The context hash URL must be under a firewall.');
53
        }
54
55 4
        if (null === $token = $this->tokenStorage->getToken()) {
56 1
            return;
57
        }
58
59 3
        $roles = array_map(function (Role $role) {
60 1
            return $role->getRole();
61 3
        }, $token->getRoles());
0 ignored issues
show
Deprecated Code introduced by Joel Wurtz
The method Symfony\Component\Securi...enInterface::getRoles() has been deprecated with message: since Symfony 4.3, use the getRoleNames() method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
62
63
        // Order is not important for roles and should not change hash.
64 3
        sort($roles);
65
66 3
        $context->addParameter('roles', $roles);
67 3
    }
68
}
69