Completed
Pull Request — master (#738)
by
unknown
62:12
created

FluentClient::get()   C

Complexity

Conditions 10
Paths 16

Size

Total Lines 49
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 49
ccs 41
cts 41
cp 1
rs 5.5471
cc 10
eloc 39
nc 16
nop 4
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Illuminate\Database\ConnectionResolverInterface as Resolver;
16
use League\OAuth2\Server\Entity\ClientEntity;
17
use League\OAuth2\Server\Entity\SessionEntity;
18
use League\OAuth2\Server\Storage\ClientInterface;
19
20
/**
21
 * This is the fluent client class.
22
 *
23
 * @author Luca Degasperi <[email protected]>
24
 */
25
class FluentClient extends AbstractFluentAdapter implements ClientInterface
26
{
27
    /**
28
     * Limit clients to grants.
29
     *
30
     * @var bool
31
     */
32
    protected $limitClientsToGrants = false;
33
34
    /**
35
     * Create a new fluent client instance.
36
     *
37
     * @param \Illuminate\Database\ConnectionResolverInterface $resolver
38
     * @param bool $limitClientsToGrants
39
     */
40 24
    public function __construct(Resolver $resolver, $limitClientsToGrants = false)
41
    {
42 24
        parent::__construct($resolver);
43 24
        $this->limitClientsToGrants = $limitClientsToGrants;
44 24
    }
45
46
    /**
47
     * Check if clients are limited to grants.
48
     *
49
     * @return bool
50
     */
51 6
    public function areClientsLimitedToGrants()
52
    {
53 6
        return $this->limitClientsToGrants;
54
    }
55
56
    /**
57
     * Whether or not to limit clients to grants.
58
     *
59
     * @param bool $limit
60
     */
61 6
    public function limitClientsToGrants($limit = false)
62
    {
63 6
        $this->limitClientsToGrants = $limit;
64 6
    }
65
66
    /**
67
     * Get the client.
68
     *
69
     * @param string $clientId
70
     * @param string $clientSecret
0 ignored issues
show
Documentation introduced by
Should the type for parameter $clientSecret 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...
71
     * @param string $redirectUri
0 ignored issues
show
Documentation introduced by
Should the type for parameter $redirectUri 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...
72
     * @param string $grantType
0 ignored issues
show
Documentation introduced by
Should the type for parameter $grantType 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...
73
     *
74
     * @return null|\League\OAuth2\Server\Entity\ClientEntity
75
     */
76 18
    public function get($clientId, $clientSecret = null, $redirectUri = null, $grantType = null)
77
    {
78 18
        $query = null;
79
80 18
        if (!is_null($redirectUri) && is_null($clientSecret)) {
81 6
            $query = $this->getConnection()->table('oauth_clients')
82 6
                   ->select(
83 6
                       'oauth_clients.id as id',
84 6
                       'oauth_clients.secret as secret',
85 6
                       'oauth_client_endpoints.redirect_uri as redirect_uri',
86 6
                       'oauth_clients.name as name')
87 6
                   ->join('oauth_client_endpoints', 'oauth_clients.id', '=', 'oauth_client_endpoints.client_id')
88 6
                   ->where('oauth_clients.id', $clientId)
89 6
                   ->where('oauth_client_endpoints.redirect_uri', $redirectUri);
90 18
        } elseif (!is_null($clientSecret) && is_null($redirectUri)) {
91 6
            $query = $this->getConnection()->table('oauth_clients')
92 6
                   ->select(
93 6
                       'oauth_clients.id as id',
94 6
                       'oauth_clients.secret as secret',
95 6
                       'oauth_clients.name as name')
96 6
                   ->where('oauth_clients.id', $clientId)
97 6
                   ->where('oauth_clients.secret', $clientSecret);
98 15
        } elseif (!is_null($clientSecret) && !is_null($redirectUri)) {
99 12
            $query = $this->getConnection()->table('oauth_clients')
100 12
                   ->select(
101 12
                       'oauth_clients.id as id',
102 12
                       'oauth_clients.secret as secret',
103 12
                       'oauth_client_endpoints.redirect_uri as redirect_uri',
104 12
                       'oauth_clients.name as name')
105 12
                   ->join('oauth_client_endpoints', 'oauth_clients.id', '=', 'oauth_client_endpoints.client_id')
106 12
                   ->where('oauth_clients.id', $clientId)
107 12
                   ->where('oauth_clients.secret', $clientSecret)
108 12
                   ->where('oauth_client_endpoints.redirect_uri', $redirectUri);
109 12
        }
110
111 18
        if ($this->limitClientsToGrants === true && !is_null($grantType)) {
112 6
            $query = $query->join('oauth_client_grants', 'oauth_clients.id', '=', 'oauth_client_grants.client_id')
113 6
                   ->join('oauth_grants', 'oauth_grants.id', '=', 'oauth_client_grants.grant_id')
114 6
                   ->where('oauth_grants.id', $grantType);
115 6
        }
116
117 18
        $result = $query->first();
118
119 18
        if (is_null($result)) {
120 6
            return;
121
        }
122
123 12
        return $this->hydrateEntity($result);
124
    }
125
126
    /**
127
     * Get the client associated with a session.
128
     *
129
     * @param  \League\OAuth2\Server\Entity\SessionEntity $session The session
130
     *
131
     * @return null|\League\OAuth2\Server\Entity\ClientEntity
132
     */
133 6
    public function getBySession(SessionEntity $session)
134
    {
135 6
        $result = $this->getConnection()->table('oauth_clients')
136 6
                ->select(
137 6
                    'oauth_clients.id as id',
138 6
                    'oauth_clients.secret as secret',
139 6
                    'oauth_clients.name as name')
140 6
                ->join('oauth_sessions', 'oauth_sessions.client_id', '=', 'oauth_clients.id')
141 6
                ->where('oauth_sessions.id', '=', $session->getId())
142 6
                ->first();
143
144 6
        if (is_null($result)) {
145 3
            return;
146
        }
147
148 3
        return $this->hydrateEntity($result);
149
    }
150
151
    /**
152
     * Create a new client.
153
     *
154
     * @param string $name The client's unique name
155
     * @param string $id The client's unique id
156
     * @param string $secret The clients' unique secret
157
     *
158
     * @return string
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...
159
     */
160
    public function create($name, $id, $secret)
161
    {
162
        return $this->getConnection()->table('oauth_clients')->insertGetId([
163
            'id' => $id,
164
            'name' => $name,
165
            'secret' => $secret,
166
            'created_at' => Carbon::now(),
167
            'updated_at' => Carbon::now(),
168
        ]);
169
    }
170
171
    /**
172
     * Hydrate the entity.
173
     *
174
     * @param $result
175
     *
176
     * @return \League\OAuth2\Server\Entity\ClientEntity
177
     */
178 15
    protected function hydrateEntity($result)
179
    {
180 15
        $client = new ClientEntity($this->getServer());
181 15
        $client->hydrate([
182 15
            'id' => $result->id,
183 15
            'name' => $result->name,
184 15
            'secret' => $result->secret,
185 15
            'redirectUri' => (isset($result->redirect_uri) ? $result->redirect_uri : null),
186 15
        ]);
187
188 15
        return $client;
189
    }
190
}
191