Completed
Push — master ( e55d65...bad107 )
by Vincent
10s
created

FluentAccessToken   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 126
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 7
c 4
b 1
f 2
lcom 1
cbo 6
dl 42
loc 126
ccs 47
cts 47
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 14 2
A getScopes() 19 19 2
A create() 14 14 1
A associateScope() 9 9 1
A delete() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of OAuth 2.0 Laravel.
5
 *
6
 * (c) Luca Degasperi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LucaDegasperi\OAuth2Server\Storage;
13
14
use Carbon\Carbon;
15
use League\OAuth2\Server\Entity\AccessTokenEntity;
16
use League\OAuth2\Server\Entity\ScopeEntity;
17
use League\OAuth2\Server\Storage\AccessTokenInterface;
18
19
/**
20
 * This is the fluent access token class.
21
 *
22
 * @author Luca Degasperi <[email protected]>
23
 */
24
class FluentAccessToken extends AbstractFluentAdapter implements AccessTokenInterface
25
{
26
    /**
27
     * Get an instance of Entities\AccessToken.
28
     *
29
     * @param string $token The access token
30
     *
31
     * @return null|AbstractTokenEntity
0 ignored issues
show
Documentation introduced by
Should the return type not be null|AccessTokenEntity?

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...
32
     */
33 9
    public function get($token)
34
    {
35 9
        $result = $this->getConnection()->table('oauth_access_tokens')
36 9
                ->where('oauth_access_tokens.id', $token)
37 9
                ->first();
38
39 9
        if (is_null($result)) {
40 6
            return;
41
        }
42
43 3
        return (new AccessTokenEntity($this->getServer()))
44 3
               ->setId($result->id)
45 3
               ->setExpireTime((int) $result->expire_time);
46
    }
47
48
    /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
    public function getByRefreshToken(RefreshTokenEntity $refreshToken)
50
    {
51
        $result = $this->getConnection()->table('oauth_access_tokens')
52
                ->select('oauth_access_tokens.*')
53
                ->join('oauth_refresh_tokens', 'oauth_access_tokens.id', '=', 'oauth_refresh_tokens.access_token_id')
54
                ->where('oauth_refresh_tokens.id', $refreshToken->getId())
55
                ->first();
56
57
        if (is_null($result)) {
58
            return null;
59
        }
60
61
        return (new AccessTokenEntity($this->getServer()))
62
               ->setId($result->id)
63
               ->setExpireTime((int)$result->expire_time);
64
    }
65
    */
66
67
    /**
68
     * Get the scopes for an access token.
69
     *
70
     * @param \League\OAuth2\Server\Entity\AccessTokenEntity $token The access token
71
     *
72
     * @return array Array of \League\OAuth2\Server\Entity\ScopeEntity
73
     */
74 3 View Code Duplication
    public function getScopes(AccessTokenEntity $token)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76 3
        $result = $this->getConnection()->table('oauth_access_token_scopes')
77 3
                ->select('oauth_scopes.*')
78 3
                ->join('oauth_scopes', 'oauth_access_token_scopes.scope_id', '=', 'oauth_scopes.id')
79 3
                ->where('oauth_access_token_scopes.access_token_id', $token->getId())
80 3
                ->get();
81
82 3
        $scopes = [];
83
84 3
        foreach ($result as $scope) {
85 3
            $scopes[] = (new ScopeEntity($this->getServer()))->hydrate([
86 3
               'id' => $scope->id,
87 3
                'description' => $scope->description,
88 3
            ]);
89 3
        }
90
91 3
        return $scopes;
92
    }
93
94
    /**
95
     * Creates a new access token.
96
     *
97
     * @param string $token The access token
98
     * @param int $expireTime The expire time expressed as a unix timestamp
99
     * @param string|int $sessionId The session ID
100
     *
101
     * @return \League\OAuth2\Server\Entity\AccessTokenEntity
102
     */
103 3 View Code Duplication
    public function create($token, $expireTime, $sessionId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105 3
        $this->getConnection()->table('oauth_access_tokens')->insert([
106 3
            'id' => $token,
107 3
            'expire_time' => $expireTime,
108 3
            'session_id' => $sessionId,
109 3
            'created_at' => Carbon::now(),
110 3
            'updated_at' => Carbon::now(),
111 3
        ]);
112
113 3
        return (new AccessTokenEntity($this->getServer()))
114 3
               ->setId($token)
115 3
               ->setExpireTime((int) $expireTime);
116
    }
117
118
    /**
119
     * Associate a scope with an access token.
120
     *
121
     * @param \League\OAuth2\Server\Entity\AccessTokenEntity $token The access token
122
     * @param \League\OAuth2\Server\Entity\ScopeEntity $scope The scope
123
     *
124
     * @return void
125
     */
126 3 View Code Duplication
    public function associateScope(AccessTokenEntity $token, ScopeEntity $scope)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
    {
128 3
        $this->getConnection()->table('oauth_access_token_scopes')->insert([
129 3
            'access_token_id' => $token->getId(),
130 3
            'scope_id' => $scope->getId(),
131 3
            'created_at' => Carbon::now(),
132 3
            'updated_at' => Carbon::now(),
133 3
        ]);
134 3
    }
135
136
    /**
137
     * Delete an access token.
138
     *
139
     * @param \League\OAuth2\Server\Entity\AccessTokenEntity $token The access token to delete
140
     *
141
     * @return void
142
     */
143 3
    public function delete(AccessTokenEntity $token)
144
    {
145 3
        $this->getConnection()->table('oauth_access_tokens')
146 3
        ->where('oauth_access_tokens.id', $token->getId())
147 3
        ->delete();
148 3
    }
149
}
150