Completed
Push — master ( c3efa2...a812ec )
by Guilherme
13s
created

AccessToken   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 111
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getAccessToken() 0 24 2
B setAccessToken() 0 32 3
A setSubjectIdentifierService() 0 4 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\OpenIDBundle\Storage;
12
13
use LoginCidadao\CoreBundle\Model\PersonInterface;
14
use LoginCidadao\OAuthBundle\Entity\Client;
15
use LoginCidadao\OAuthBundle\Model\ClientInterface;
16
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService;
17
use OAuth2\ServerBundle\Storage\AccessToken as BaseClass;
18
use OAuth2\Storage\AccessTokenInterface;
19
use Doctrine\ORM\EntityManager;
20
21
class AccessToken extends BaseClass implements AccessTokenInterface
22
{
23
    /** @var EntityManager */
24
    private $em;
25
26
    /** @var SubjectIdentifierService */
27
    private $subjectIdentifierService;
28
29
    public function __construct(EntityManager $EntityManager)
0 ignored issues
show
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...
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...
30
    {
31
        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...
32
        $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...
33
    }
34
35
    /**
36
     * Look up the supplied oauth_token from storage.
37
     *
38
     * We need to retrieve access token data as we create and verify tokens.
39
     *
40
     * @param $oauth_token
41
     * oauth_token to be check with.
42
     *
43
     * @return array|null
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use null|array<string,string|integer|null>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
44
     * An associative array as below, and return NULL if the supplied oauth_token
45
     * is invalid:
46
     * - client_id: Stored client identifier.
47
     * - expires: Stored expiration in unix timestamp.
48
     * - scope: (optional) Stored scope values in space-separated string.
49
     *
50
     * @ingroup oauth2_section_7
51
     */
52
    public function getAccessToken($oauth_token)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $oauth_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\AccessToken $accessToken */
55
        $accessToken = $this->em->getRepository('LoginCidadaoOAuthBundle:AccessToken')
56
            ->findOneBy(['token' => $oauth_token]);
0 ignored issues
show
Coding Style introduced by
$oauth_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 (!$accessToken) {
59
            return null;
60
        }
61
62
        /** @var Client $client */
63
        $client = $accessToken->getClient();
64
65
        /** @var PersonInterface $person */
66
        $person = $accessToken->getUser();
67
68
        return [
69
            'client_id' => $client->getClientId(),
70
            'user_id' => $this->subjectIdentifierService->getSubjectIdentifier($person, $client),
0 ignored issues
show
Documentation introduced by
$client is of type object<LoginCidadao\OAuthBundle\Entity\Client>, but the function expects a null|object<LoginCidadao...\Entity\ClientMetadata>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
            'expires' => $accessToken->getExpiresAt(),
72
            'scope' => $accessToken->getScope(),
73
            'id_token' => $accessToken->getIdToken(),
74
        ];
75
    }
76
77
    /**
78
     * Store the supplied access token values to storage.
79
     *
80
     * We need to store access token data as we create and verify tokens.
81
     *
82
     * @param string $oauth_token
83
     * oauth_token to be stored.
84
     * @param string $client_id
85
     * Client identifier to be stored.
86
     * @param string $user_id
87
     * User identifier to be stored.
88
     * @param int $expires Expiration to be stored as a Unix timestamp.
89
     * @param string $scope (optional) Scopes to be stored in space-separated string.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $scope not be string|null?

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...
90
     * @param null|string $id_token
91
     * @return null|void
92
     * @ingroup oauth2_section_4
93
     */
94
    public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = null, $id_token = null)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $oauth_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...
Coding Style Naming introduced by
The parameter $id_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...
95
    {
96
        // Get Client Entity
97
        $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...
98
99
        /** @var ClientInterface $client */
100
        $client = $this->em->getRepository('LoginCidadaoOAuthBundle:Client')->find($id[0]);
101
102
        if (!$client) {
103
            return null;
104
        }
105
106
        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...
107
            return null;
108
        } else {
109
            /** @var PersonInterface $user */
110
            $user = $this->em->getRepository('LoginCidadaoCoreBundle:Person')->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...
111
        }
112
113
        // Create Access Token
114
        $accessToken = new \LoginCidadao\OAuthBundle\Entity\AccessToken();
115
        $accessToken->setToken($oauth_token);
0 ignored issues
show
Coding Style introduced by
$oauth_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...
116
        $accessToken->setClient($client);
117
        $accessToken->setUser($user);
118
        $accessToken->setExpiresAt($expires);
119
        $accessToken->setScope($scope);
120
        $accessToken->setIdToken($id_token);
0 ignored issues
show
Coding Style introduced by
$id_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...
121
122
        // Store Access Token and Authorization
123
        $this->em->persist($accessToken);
124
        $this->em->flush();
125
    }
126
127
    public function setSubjectIdentifierService(SubjectIdentifierService $subjectIdentifierService)
128
    {
129
        $this->subjectIdentifierService = $subjectIdentifierService;
130
    }
131
}
132