Completed
Push — master ( 9a4875...187362 )
by David
05:27
created

RoleProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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
0 ignored issues
show
Documentation introduced by
Should the type for parameter $tokenStorage not be null|TokenStorageInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
38
     */
39 20
    public function __construct(TokenStorageInterface $tokenStorage = null)
40
    {
41 20
        $this->tokenStorage = $tokenStorage;
42 20
    }
43
44
    /**
45
     * {@inheritdoc}
46
     *
47
     * @throws InvalidConfigurationException when called without a security context being set
48
     */
49 3
    public function updateUserContext(UserContext $context)
50
    {
51 3
        if (null === $this->tokenStorage) {
52 1
            throw new InvalidConfigurationException('The context hash URL must be under a firewall.');
53
        }
54
55 2
        if (null === $token = $this->tokenStorage->getToken()) {
56 1
            return;
57
        }
58
59 1
        $roles = array_map(function (Role $role) {
60 1
            return $role->getRole();
61 1
        }, $token->getRoles());
62
63
        // Order is not important for roles and should not change hash.
64 1
        sort($roles);
65
66 1
        $context->addParameter('roles', $roles);
67 1
    }
68
}
69