SecurityContext::setToken()   A
last analyzed

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
namespace TreeHouse\BaseApiBundle\Security;
4
5
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6
use Symfony\Component\Security\Core\SecurityContextInterface;
7
8
/**
9
 * A custom security context to hold information about the current user that the api client is acting on behalf of.
10
 */
11
class SecurityContext implements SecurityContextInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Symfony\Component\Securi...ecurityContextInterface has been deprecated with message: since version 2.6, to be removed in 3.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
12
{
13
    /**
14
     * @var TokenInterface
15
     */
16
    protected $token;
17
18
    /**
19
     * @inheritdoc
20
     */
21 4
    public function getToken()
22
    {
23 4
        return $this->token;
24
    }
25
26
    /**
27
     * @inheritdoc
28
     */
29 2
    public function setToken(TokenInterface $token = null)
30
    {
31 2
        $this->token = $token;
32 2
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37 6
    public function isGranted($attributes, $object = null)
38
    {
39 6
        if (!is_array($attributes)) {
40 4
            $attributes = [$attributes];
41 2
        }
42
43 6
        if (null === $object) {
44 2
            $object = $this->token->getUser();
45 1
        }
46
47 6
        $acceptedRoles = array_intersect($attributes, $object->getRoles());
48
49 6
        return (0 !== count($acceptedRoles));
50
    }
51
}
52