Completed
Pull Request — master (#680)
by
unknown
08:59
created

FluentClient::areClientsLimitedToGrants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 27
    public function __construct(Resolver $resolver, $limitClientsToGrants = false)
41
    {
42 27
        parent::__construct($resolver);
43 27
        $this->limitClientsToGrants = $limitClientsToGrants;
44 27
    }
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 21
    public function get($clientId, $clientSecret = null, $redirectUri = null, $grantType = null)
77
    {
78 21
        $query = null;
79
80 21
        if (is_null($redirectUri) && is_null($clientSecret)) {
81 3
            $query = $this->getConnection()->table('oauth_clients')
82 3
                  ->select(
83 3
                      'oauth_clients.id as id',
84 3
                      'oauth_clients.secret as secret',
85 3
                      'oauth_client_endpoints.redirect_uri as redirect_uri',
86 3
                      'oauth_clients.name as name')
87 3
                  ->join('oauth_client_endpoints', 'oauth_clients.id', '=', 'oauth_client_endpoints.client_id')
88 3
                  ->where('oauth_clients.id', $clientId);
89 21 View Code Duplication
        } elseif (!is_null($redirectUri) && is_null($clientSecret)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
90 6
            $query = $this->getConnection()->table('oauth_clients')
91 6
                   ->select(
92 6
                       'oauth_clients.id as id',
93 6
                       'oauth_clients.secret as secret',
94 6
                       'oauth_client_endpoints.redirect_uri as redirect_uri',
95 6
                       'oauth_clients.name as name')
96 6
                   ->join('oauth_client_endpoints', 'oauth_clients.id', '=', 'oauth_client_endpoints.client_id')
97 6
                   ->where('oauth_clients.id', $clientId)
98 6
                   ->where('oauth_client_endpoints.redirect_uri', $redirectUri);
99 18
        } elseif (!is_null($clientSecret) && is_null($redirectUri)) {
100 6
            $query = $this->getConnection()->table('oauth_clients')
101 6
                   ->select(
102 6
                       'oauth_clients.id as id',
103 6
                       'oauth_clients.secret as secret',
104 6
                       'oauth_client_endpoints.redirect_uri as redirect_uri',
105 6
                       'oauth_clients.name as name')
106 6
                   ->join('oauth_client_endpoints', 'oauth_clients.id', '=', 'oauth_client_endpoints.client_id')
107 6
                   ->where('oauth_clients.id', $clientId)
108 6
                   ->where('oauth_clients.secret', $clientSecret);
109 15 View Code Duplication
        } elseif (!is_null($clientSecret) && !is_null($redirectUri)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
110 12
            $query = $this->getConnection()->table('oauth_clients')
111 12
                   ->select(
112 12
                       'oauth_clients.id as id',
113 12
                       'oauth_clients.secret as secret',
114 12
                       'oauth_client_endpoints.redirect_uri as redirect_uri',
115 12
                       'oauth_clients.name as name')
116 12
                   ->join('oauth_client_endpoints', 'oauth_clients.id', '=', 'oauth_client_endpoints.client_id')
117 12
                   ->where('oauth_clients.id', $clientId)
118 12
                   ->where('oauth_clients.secret', $clientSecret)
119 12
                   ->where('oauth_client_endpoints.redirect_uri', $redirectUri);
120 12
        }
121
122 21
        if ($this->limitClientsToGrants === true && !is_null($grantType)) {
123 6
            $query = $query->join('oauth_client_grants', 'oauth_clients.id', '=', 'oauth_client_grants.client_id')
124 6
                   ->join('oauth_grants', 'oauth_grants.id', '=', 'oauth_client_grants.grant_id')
125 6
                   ->where('oauth_grants.id', $grantType);
126 6
        }
127
128 21
        $result = $query->first();
129
130 21
        if (is_null($result)) {
131 6
            return;
132
        }
133
134 15
        return $this->hydrateEntity($result);
135
    }
136
137
    /**
138
     * Get the client associated with a session.
139
     *
140
     * @param  \League\OAuth2\Server\Entity\SessionEntity $session The session
141
     *
142
     * @return null|\League\OAuth2\Server\Entity\ClientEntity
143
     */
144 6
    public function getBySession(SessionEntity $session)
145
    {
146 6
        $result = $this->getConnection()->table('oauth_clients')
147 6
                ->select(
148 6
                    'oauth_clients.id as id',
149 6
                    'oauth_clients.secret as secret',
150 6
                    'oauth_clients.name as name')
151 6
                ->join('oauth_sessions', 'oauth_sessions.client_id', '=', 'oauth_clients.id')
152 6
                ->where('oauth_sessions.id', '=', $session->getId())
153 6
                ->first();
154
155 6
        if (is_null($result)) {
156 3
            return;
157
        }
158
159 3
        return $this->hydrateEntity($result);
160
    }
161
162
    /**
163
     * Create a new client.
164
     *
165
     * @param string $name The client's unique name
166
     * @param string $id The client's unique id
167
     * @param string $secret The clients' unique secret
168
     *
169
     * @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...
170
     */
171
    public function create($name, $id, $secret)
172
    {
173
        return $this->getConnection()->table('oauth_clients')->insertGetId([
174
            'id' => $id,
175
            'name' => $name,
176
            'secret' => $secret,
177
            'created_at' => Carbon::now(),
178
            'updated_at' => Carbon::now(),
179
        ]);
180
    }
181
182
    /**
183
     * Hydrate the entity.
184
     *
185
     * @param $result
186
     *
187
     * @return \League\OAuth2\Server\Entity\ClientEntity
188
     */
189 18
    protected function hydrateEntity($result)
190
    {
191 18
        $client = new ClientEntity($this->getServer());
192 18
        $client->hydrate([
193 18
            'id' => $result->id,
194 18
            'name' => $result->name,
195 18
            'secret' => $result->secret,
196 18
            'redirectUri' => (isset($result->redirect_uri) ? $result->redirect_uri : null),
197 18
        ]);
198
199 18
        return $client;
200
    }
201
}
202