Completed
Pull Request — master (#660)
by
unknown
05:16
created

FluentSession   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 143
Duplicated Lines 44.06 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 81.54%

Importance

Changes 4
Bugs 1 Features 3
Metric Value
wmc 10
c 4
b 1
f 3
lcom 1
cbo 8
dl 63
loc 143
ccs 53
cts 65
cp 0.8154
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 15 2
A getByAccessToken() 17 17 2
A getScopes() 20 20 2
A create() 0 11 1
A associateScope() 9 9 1
A getByAuthCode() 17 17 2

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\AuthCodeEntity;
17
use League\OAuth2\Server\Entity\ScopeEntity;
18
use League\OAuth2\Server\Entity\SessionEntity;
19
use League\OAuth2\Server\Storage\SessionInterface;
20
21
/**
22
 * This is the fluent session class.
23
 *
24
 * @author Luca Degasperi <[email protected]>
25
 */
26
class FluentSession extends AbstractFluentAdapter implements SessionInterface
27
{
28
    /**
29
     * Get a session from it's identifier.
30
     *
31
     * @param string $sessionId
32
     *
33
     * @return \League\OAuth2\Server\Entity\SessionEntity
0 ignored issues
show
Documentation introduced by
Should the return type not be SessionEntity|null?

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...
34
     */
35 6
    public function get($sessionId)
36
    {
37 6
        $result = $this->getConnection()->table('oauth_sessions')
38 6
                    ->where('oauth_sessions.id', $sessionId)
39 6
                    ->where('deleted_at', NULL)
40 6
                    ->first();
41
42 6
        if (is_null($result)) {
43 3
            return;
44
        }
45
46 3
        return (new SessionEntity($this->getServer()))
47 3
               ->setId($result->id)
48 3
               ->setOwner($result->owner_type, $result->owner_id);
49
    }
50
51
    /**
52
     * Get a session from an access token.
53
     *
54
     * @param \League\OAuth2\Server\Entity\AccessTokenEntity $accessToken The access token
55
     *
56
     * @return \League\OAuth2\Server\Entity\SessionEntity
0 ignored issues
show
Documentation introduced by
Should the return type not be SessionEntity|null?

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...
57
     */
58 View Code Duplication
    public function getByAccessToken(AccessTokenEntity $accessToken)
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...
59
    {
60
        $result = $this->getConnection()->table('oauth_sessions')
61
                ->select('oauth_sessions.*')
62
                ->join('oauth_access_tokens', 'oauth_sessions.id', '=', 'oauth_access_tokens.session_id')
63
                ->where('oauth_access_tokens.id', $accessToken->getId())
64
                ->where('deleted_at', NULL)
65
                ->first();
66
67
        if (is_null($result)) {
68
            return;
69
        }
70
71
        return (new SessionEntity($this->getServer()))
72
               ->setId($result->id)
73
               ->setOwner($result->owner_type, $result->owner_id);
74
    }
75
76
    /**
77
     * Get a session's scopes.
78
     *
79
     * @param \League\OAuth2\Server\Entity\SessionEntity
80
     *
81
     * @return array Array of \League\OAuth2\Server\Entity\ScopeEntity
82
     */
83 3 View Code Duplication
    public function getScopes(SessionEntity $session)
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...
84
    {
85
        // TODO: Check this before pushing
86 3
        $result = $this->getConnection()->table('oauth_session_scopes')
87 3
                  ->select('oauth_scopes.*')
88 3
                  ->join('oauth_scopes', 'oauth_session_scopes.scope_id', '=', 'oauth_scopes.id')
89 3
                  ->where('oauth_session_scopes.session_id', $session->getId())
90 3
                  ->get();
91
92 3
        $scopes = [];
93
94 3
        foreach ($result as $scope) {
95 3
            $scopes[] = (new ScopeEntity($this->getServer()))->hydrate([
96 3
                'id' => $scope->id,
97 3
                'description' => $scope->description,
98 3
            ]);
99 3
        }
100
101 3
        return $scopes;
102
    }
103
104
    /**
105
     * Create a new session.
106
     *
107
     * @param string $ownerType Session owner's type (user, client)
108
     * @param string $ownerId Session owner's ID
109
     * @param string $clientId Client ID
110
     * @param string $clientRedirectUri Client redirect URI (default = null)
0 ignored issues
show
Documentation introduced by
Should the type for parameter $clientRedirectUri 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...
111
     *
112
     * @return int The session's ID
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|string?

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...
113
     */
114 3
    public function create($ownerType, $ownerId, $clientId, $clientRedirectUri = null)
115
    {
116 3
        return $this->getConnection()->table('oauth_sessions')->insertGetId([
117 3
            'client_id' => $clientId,
118 3
            'owner_type' => $ownerType,
119 3
            'owner_id' => $ownerId,
120 3
            'client_redirect_uri' => $clientRedirectUri,
121 3
            'created_at' => Carbon::now(),
122 3
            'updated_at' => Carbon::now(),
123 3
        ]);
124
    }
125
126
    /**
127
     * Associate a scope with a session.
128
     *
129
     * @param \League\OAuth2\Server\Entity\SessionEntity $session
130
     * @param \League\OAuth2\Server\Entity\ScopeEntity $scope The scopes ID might be an integer or string
131
     *
132
     * @return void
133
     */
134 3 View Code Duplication
    public function associateScope(SessionEntity $session, 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...
135
    {
136 3
        $this->getConnection()->table('oauth_session_scopes')->insert([
137 3
            'session_id' => $session->getId(),
138 3
            'scope_id' => $scope->getId(),
139 3
            'created_at' => Carbon::now(),
140 3
            'updated_at' => Carbon::now(),
141 3
        ]);
142 3
    }
143
144
    /**
145
     * Get a session from an auth code.
146
     *
147
     * @param \League\OAuth2\Server\Entity\AuthCodeEntity $authCode The auth code
148
     *
149
     * @return \League\OAuth2\Server\Entity\SessionEntity
0 ignored issues
show
Documentation introduced by
Should the return type not be SessionEntity|null?

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...
150
     */
151 6 View Code Duplication
    public function getByAuthCode(AuthCodeEntity $authCode)
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...
152
    {
153 6
        $result = $this->getConnection()->table('oauth_sessions')
154 6
            ->select('oauth_sessions.*')
155 6
            ->where('deleted_at', NULL)
156 6
            ->join('oauth_auth_codes', 'oauth_sessions.id', '=', 'oauth_auth_codes.session_id')
157 6
            ->where('oauth_auth_codes.id', $authCode->getId())
158 6
            ->first();
159
160 6
        if (is_null($result)) {
161 3
            return;
162
        }
163
164 3
        return (new SessionEntity($this->getServer()))
165 3
               ->setId($result->id)
166 3
               ->setOwner($result->owner_type, $result->owner_id);
167
    }
168
}
169