Completed
Pull Request — master (#746)
by
unknown
83:49 queued 18:51
created

FluentSession   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 140
Duplicated Lines 43.57 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 10
c 3
b 1
f 2
lcom 1
cbo 8
dl 61
loc 140
ccs 62
cts 62
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 14 2
A getScopes() 20 20 2
A create() 0 11 1
A associateScope() 9 9 1
A getByAuthCode() 16 16 2
A getByAccessToken() 16 16 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
                    ->first();
40
41 6
        if (is_null($result)) {
42 3
            return;
43
        }
44
45 3
        return (new SessionEntity($this->getServer()))
46 3
               ->setId($result->id)
47 3
               ->setOwner($result->owner_type, $result->owner_id);
48
    }
49
50
    /**
51
     * Get a session from an access token.
52
     *
53
     * @param \League\OAuth2\Server\Entity\AccessTokenEntity $accessToken The access token
54
     *
55
     * @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...
56
     */
57 6 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...
58
    {
59 6
        $result = $this->getConnection()->table('oauth_sessions')
60 6
                ->select('oauth_sessions.*')
61 6
                ->join('oauth_access_tokens', 'oauth_sessions.id', '=', 'oauth_access_tokens.session_id')
62 6
                ->where('oauth_access_tokens.id', $accessToken->getId())
63 6
                ->first();
64
65 6
        if (is_null($result)) {
66 3
            return;
67
        }
68
69 3
        return (new SessionEntity($this->getServer()))
70 3
               ->setId($result->id)
71 3
               ->setOwner($result->owner_type, $result->owner_id);
72
    }
73
74
    /**
75
     * Get a session's scopes.
76
     *
77
     * @param \League\OAuth2\Server\Entity\SessionEntity
78
     *
79
     * @return array Array of \League\OAuth2\Server\Entity\ScopeEntity
80
     */
81 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...
82
    {
83
        // TODO: Check this before pushing
84 3
        $result = $this->getConnection()->table('oauth_session_scopes')
85 3
                  ->select('oauth_scopes.*')
86 3
                  ->join('oauth_scopes', 'oauth_session_scopes.scope_id', '=', 'oauth_scopes.id')
87 3
                  ->where('oauth_session_scopes.session_id', $session->getId())
88 3
                  ->get();
89
90 3
        $scopes = [];
91
92 3
        foreach ($result as $scope) {
93 3
            $scopes[] = (new ScopeEntity($this->getServer()))->hydrate([
94 3
                'id' => $scope->id,
95 3
                'description' => $scope->description,
96 3
            ]);
97 3
        }
98
99 3
        return $scopes;
100
    }
101
102
    /**
103
     * Create a new session.
104
     *
105
     * @param string $ownerType Session owner's type (user, client)
106
     * @param string $ownerId Session owner's ID
107
     * @param string $clientId Client ID
108
     * @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...
109
     *
110
     * @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...
111
     */
112 3
    public function create($ownerType, $ownerId, $clientId, $clientRedirectUri = null)
113
    {
114 3
        return $this->getConnection()->table('oauth_sessions')->insertGetId([
115 3
            'client_id' => $clientId,
116 3
            'owner_type' => $ownerType,
117 3
            'owner_id' => $ownerId,
118 3
            'client_redirect_uri' => $clientRedirectUri,
119 3
            'created_at' => Carbon::now(),
120 3
            'updated_at' => Carbon::now(),
121 3
        ]);
122
    }
123
124
    /**
125
     * Associate a scope with a session.
126
     *
127
     * @param \League\OAuth2\Server\Entity\SessionEntity $session
128
     * @param \League\OAuth2\Server\Entity\ScopeEntity $scope The scopes ID might be an integer or string
129
     *
130
     * @return void
131
     */
132 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...
133
    {
134 3
        $this->getConnection()->table('oauth_session_scopes')->insert([
135 3
            'session_id' => $session->getId(),
136 3
            'scope_id' => $scope->getId(),
137 3
            'created_at' => Carbon::now(),
138 3
            'updated_at' => Carbon::now(),
139 3
        ]);
140 3
    }
141
142
    /**
143
     * Get a session from an auth code.
144
     *
145
     * @param \League\OAuth2\Server\Entity\AuthCodeEntity $authCode The auth code
146
     *
147
     * @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...
148
     */
149 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...
150
    {
151 6
        $result = $this->getConnection()->table('oauth_sessions')
152 6
            ->select('oauth_sessions.*')
153 6
            ->join('oauth_auth_codes', 'oauth_sessions.id', '=', 'oauth_auth_codes.session_id')
154 6
            ->where('oauth_auth_codes.id', $authCode->getId())
155 6
            ->first();
156
157 6
        if (is_null($result)) {
158 3
            return;
159
        }
160
161 3
        return (new SessionEntity($this->getServer()))
162 3
               ->setId($result->id)
163 3
               ->setOwner($result->owner_type, $result->owner_id);
164
    }
165
}
166