Failed Conditions
Push — issue#777 ( 19a7c1 )
by Guilherme
08:25
created

AccessTokenManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTokenScope() 0 12 3
A getToken() 0 11 3
A getTokenClient() 0 5 1
A hasToken() 0 4 1
A __construct() 0 5 1
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
    public function __construct(EntityManager $em,
28
                                TokenStorageInterface $tokenStorage)
29
    {
30
        $this->em           = $em;
31
        $this->tokenStorage = $tokenStorage;
32
    }
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