Completed
Push — master ( 22f213...24f6ce )
by Guilherme
17:25
created

RefreshToken::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
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\OpenIDBundle\Storage;
12
13
use LoginCidadao\CoreBundle\Model\PersonInterface;
14
use LoginCidadao\OAuthBundle\Model\ClientInterface;
15
use OAuth2\ServerBundle\Storage\RefreshToken as BaseClass;
16
use OAuth2\Storage\RefreshTokenInterface;
17
use Doctrine\ORM\EntityManager;
18
19
class RefreshToken extends BaseClass implements RefreshTokenInterface
20
{
21
    private $em;
22
23
    public function __construct(EntityManager $EntityManager)
0 ignored issues
show
Coding Style introduced by
$EntityManager does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style Naming introduced by
The parameter $EntityManager is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
24
    {
25
        parent::__construct($EntityManager);
0 ignored issues
show
Coding Style introduced by
$EntityManager does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
26
        $this->em = $EntityManager;
0 ignored issues
show
Coding Style introduced by
$EntityManager does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
27
    }
28
29
    /**
30
     * Grant refresh access tokens.
31
     *
32
     * Retrieve the stored data for the given refresh token.
33
     *
34
     * Required for OAuth2::GRANT_TYPE_REFRESH_TOKEN.
35
     *
36
     * @param $refresh_token
37
     * Refresh token to be check with.
38
     *
39
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
40
     * An associative array as below, and NULL if the refresh_token is
41
     * invalid:
42
     * - refresh_token: Stored refresh token identifier.
43
     * - client_id: Stored client identifier.
44
     * - user_id: Stored user identifier.
45
     * - expires: Stored expiration unix timestamp.
46
     * - scope: (optional) Stored scope values in space-separated string.
47
     *
48
     * @see http://tools.ietf.org/html/rfc6749#section-6
49
     *
50
     * @ingroup oauth2_section_6
51
     */
52
    public function getRefreshToken($refresh_token)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $refresh_token is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
53
    {
54
        /** @var \LoginCidadao\OAuthBundle\Entity\RefreshToken $refreshToken */
55
        $refreshToken = $this->em->getRepository('LoginCidadaoOAuthBundle:RefreshToken')
56
            ->findOneBy(array('token' => $refresh_token));
0 ignored issues
show
Coding Style introduced by
$refresh_token does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
57
58
        if (!$refreshToken) {
59
            return null;
60
        }
61
62
        // Get Client
63
        /** @var ClientInterface $client */
64
        $client = $refreshToken->getClient();
65
66
        /** @var PersonInterface $user */
67
        $user = $refreshToken->getUser();
68
69
        return [
70
            'refresh_token' => $refreshToken->getToken(),
71
            'client_id' => $client->getPublicId(),
72
            'user_id' => $user->getId(),
73
            'expires' => $refreshToken->getExpiresAt(),
74
            'scope' => $refreshToken->getScope(),
75
        ];
76
    }
77
78
    /**
79
     * Take the provided refresh token values and store them somewhere.
80
     *
81
     * This function should be the storage counterpart to getRefreshToken().
82
     *
83
     * If storage fails for some reason, we're not currently checking for
84
     * any sort of success/failure, so you should bail out of the script
85
     * and provide a descriptive fail message.
86
     *
87
     * Required for OAuth2::GRANT_TYPE_REFRESH_TOKEN.
88
     *
89
     * @param $refresh_token
90
     * Refresh token to be stored.
91
     * @param $client_id
92
     * Client identifier to be stored.
93
     * @param $user_id
94
     * User identifier to be stored.
95
     * @param $expires
96
     * expires to be stored.
97
     * @param $scope
98
     * (optional) Scopes to be stored in space-separated string.
99
     *
100
     * @ingroup oauth2_section_6
101
     * @return null|void
102
     */
103
    public function setRefreshToken(
0 ignored issues
show
Coding Style Naming introduced by
The parameter $refresh_token is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $client_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The parameter $user_id is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
104
        $refresh_token,
105
        $client_id,
106
        $user_id,
107
        $expires,
108
        $scope = null
109
    ) {
110
        // Get Client Entity
111
        $id = explode('_', $client_id);
0 ignored issues
show
Coding Style introduced by
$client_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
112
113
        /** @var ClientInterface $client */
114
        $client = $this->em->getRepository('LoginCidadaoOAuthBundle:Client')
115
            ->find($id[0]);
116
117
        if (!$client) {
118
            return null;
119
        }
120
121
        if ($user_id === null) {
0 ignored issues
show
Coding Style introduced by
$user_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
122
            return null;
123
        } else {
124
            /** @var PersonInterface $user */
125
            $user = $this->em->getRepository('LoginCidadaoCoreBundle:Person')
126
                ->find($user_id);
0 ignored issues
show
Coding Style introduced by
$user_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
127
        }
128
129
        // Create Refresh Token
130
        $refreshToken = new \LoginCidadao\OAuthBundle\Entity\RefreshToken();
131
        $refreshToken->setToken($refresh_token);
0 ignored issues
show
Coding Style introduced by
$refresh_token does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
132
        $refreshToken->setClient($client);
133
        $refreshToken->setUser($user);
134
        $refreshToken->setExpiresAt($expires);
135
        $refreshToken->setScope($scope);
136
137
        // Store Refresh Token
138
        $this->em->persist($refreshToken);
139
        $this->em->flush();
140
    }
141
142
    /**
143
     * Expire a used refresh token.
144
     *
145
     * This is not explicitly required in the spec, but is almost implied.
146
     * After granting a new refresh token, the old one is no longer useful and
147
     * so should be forcibly expired in the data store so it can't be used again.
148
     *
149
     * If storage fails for some reason, we're not currently checking for
150
     * any sort of success/failure, so you should bail out of the script
151
     * and provide a descriptive fail message.
152
     *
153
     * @param $refresh_token
154
     * Refresh token to be expirse.
155
     *
156
     * @ingroup oauth2_section_6
157
     */
158
    public function unsetRefreshToken($refresh_token)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $refresh_token is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
159
    {
160
        $refreshToken = $this->em->getRepository('LoginCidadaoOAuthBundle:RefreshToken')
161
            ->findOneBy(['token' => $refresh_token]);
0 ignored issues
show
Coding Style introduced by
$refresh_token does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
162
        $this->em->remove($refreshToken);
0 ignored issues
show
Bug introduced by
It seems like $refreshToken defined by $this->em->getRepository...en' => $refresh_token)) on line 160 can also be of type null; however, Doctrine\ORM\EntityManager::remove() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
163
        $this->em->flush();
164
    }
165
}
166