Passed
Push — issue#767 ( 39899e...c3d4bb )
by Guilherme
10:59
created

AccessTokenManager::getTokenScope()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\OAuthBundle\Model;
12
13
use Doctrine\ORM\EntityManager;
14
use LoginCidadao\OAuthBundle\Entity\AccessToken;
15
use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken;
16
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
19
class AccessTokenManager
20
{
21
    /** @var EntityManager */
22
    private $em;
23
24
    /** @var TokenStorageInterface */
25
    private $tokenStorage;
26
27 1
    public function __construct(EntityManager $em,
0 ignored issues
show
Coding Style introduced by
The first parameter of a multi-line function declaration must be on the line after the opening bracket
Loading history...
28
                                TokenStorageInterface $tokenStorage)
0 ignored issues
show
Coding Style introduced by
Multi-line function declaration not indented correctly; expected 8 spaces but found 32
Loading history...
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
29
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
30 1
        $this->em           = $em;
31 1
        $this->tokenStorage = $tokenStorage;
32 1
    }
33
34
    /**
35
     * @return boolean
36
     */
37
    public function hasToken()
38
    {
39
        $securityToken = $this->tokenStorage->getToken();
40
        return ($securityToken instanceof OAuthToken);
41
    }
42
43
    /**
44
     * @param string $token
45
     * @throws AccessDeniedException when no token is found in the current session
46
     * @return AccessToken
47
     */
48
    public function getToken($token = null)
49
    {
50
        if ($token === null) {
51
            $securityToken = $this->tokenStorage->getToken();
52
            if (!($securityToken instanceof OAuthToken)) {
53
                throw new AccessDeniedException("Couldn't find an AccessToken in the current session.");
54
            }
55
            $token = $securityToken->getToken();
56
        }
57
        $repo = $this->em->getRepository('LoginCidadaoOAuthBundle:AccessToken');
58
        return $repo->findOneByToken($token);
0 ignored issues
show
Bug introduced by
The method findOneByToken() does not exist on LoginCidadao\OAuthBundle...y\AccessTokenRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        return $repo->/** @scrutinizer ignore-call */ findOneByToken($token);
Loading history...
Bug Best Practice introduced by
The expression return $repo->findOneByToken($token) also could return the type array which is incompatible with the documented return type LoginCidadao\OAuthBundle\Entity\AccessToken.
Loading history...
59
    }
60
61
    /**
62
     * @param string $token defaults to the current AccessToken
63
     * @return array
64
     */
65
    public function getTokenScope($token = null)
66
    {
67
        $accessToken = $this->getToken($token);
68
69
        $scope = $accessToken->getScope();
70
71
        if (is_string($scope)) {
0 ignored issues
show
introduced by
The condition is_string($scope) is always true.
Loading history...
72
            return explode(' ', $scope);
73
        } elseif (is_array($scope)) {
74
            return $scope;
75
        } else {
76
            return null;
77
        }
78
    }
79
80
    /**
81
     * @param string $token
82
     * @return ClientInterface
83
     */
84
    public function getTokenClient($token = null)
85
    {
86
        $accessToken = $this->getToken($token);
87
88
        return $accessToken->getClient();
89
    }
90
}
91