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

FluentScope::get()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 31
ccs 22
cts 22
cp 1
rs 8.439
cc 6
eloc 19
nc 8
nop 3
crap 6
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 Illuminate\Database\ConnectionResolverInterface as Resolver;
15
use League\OAuth2\Server\Entity\ScopeEntity;
16
use League\OAuth2\Server\Storage\ScopeInterface;
17
18
/**
19
 * This is the fluent scope class.
20
 *
21
 * @author Luca Degasperi <[email protected]>
22
 */
23
class FluentScope extends AbstractFluentAdapter implements ScopeInterface
24
{
25
    /*
26
     * Limit clients to scopes.
27
     *
28
     * @var bool
29
     */
30
    protected $limitClientsToScopes = false;
31
32
    /*
33
     * Limit scopes to grants.
34
     *
35
     * @var bool
36
     */
37
    protected $limitScopesToGrants = false;
38
39
    /**
40
     * Create a new fluent scope instance.
41
     *
42
     * @param \Illuminate\Database\ConnectionResolverInterface $resolver
43
     * @param bool|false $limitClientsToScopes
44
     * @param bool|false $limitScopesToGrants
45
     */
46 21
    public function __construct(Resolver $resolver, $limitClientsToScopes = false, $limitScopesToGrants = false)
47
    {
48 21
        parent::__construct($resolver);
49 21
        $this->limitClientsToScopes = $limitClientsToScopes;
50 21
        $this->limitScopesToGrants = $limitScopesToGrants;
51 21
    }
52
53
    /**
54
     * Set limit clients to scopes.
55
     *
56
     * @param bool|false $limit
57
     */
58 12
    public function limitClientsToScopes($limit = false)
59
    {
60 12
        $this->limitClientsToScopes = $limit;
61 12
    }
62
63
    /**
64
     * Set limit scopes to grants.
65
     *
66
     * @param bool|false $limit
67
     */
68 12
    public function limitScopesToGrants($limit = false)
69
    {
70 12
        $this->limitScopesToGrants = $limit;
71 12
    }
72
73
    /**
74
     * Check if clients are limited to scopes.
75
     *
76
     * @return bool|false
77
     */
78 9
    public function areClientsLimitedToScopes()
79
    {
80 9
        return $this->limitClientsToScopes;
81
    }
82
83
    /**
84
     * Check if scopes are limited to grants.
85
     *
86
     * @return bool|false
87
     */
88 9
    public function areScopesLimitedToGrants()
89
    {
90 9
        return $this->limitScopesToGrants;
91
    }
92
93
    /**
94
     * Return information about a scope.
95
     *
96
     * Example SQL query:
97
     *
98
     * <code>
99
     * SELECT * FROM oauth_scopes WHERE scope = :scope
100
     * </code>
101
     *
102
     * @param string $scope The scope
103
     * @param string $grantType The grant type used in the request (default = "null")
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...
104
     * @param string $clientId The client id used for the request (default = "null")
0 ignored issues
show
Documentation introduced by
Should the type for parameter $clientId 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...
105
     *
106
     * @return \League\OAuth2\Server\Entity\ScopeEntity|null If the scope doesn't exist return false
107
     */
108 21
    public function get($scope, $grantType = null, $clientId = null)
109
    {
110 21
        $query = $this->getConnection()->table('oauth_scopes')
111 21
                    ->select('oauth_scopes.id as id', 'oauth_scopes.description as description')
112 21
                    ->where('oauth_scopes.id', $scope);
113
114 21
        if ($this->limitClientsToScopes === true && !is_null($clientId)) {
115 12
            $query = $query->join('oauth_client_scopes', 'oauth_scopes.id', '=', 'oauth_client_scopes.scope_id')
116 12
                           ->where('oauth_client_scopes.client_id', $clientId);
117 12
        }
118
119 21
        if ($this->limitScopesToGrants === true && !is_null($grantType)) {
120 12
            $query = $query->join('oauth_grant_scopes', 'oauth_scopes.id', '=', 'oauth_grant_scopes.scope_id')
121 12
                           ->join('oauth_grants', 'oauth_grants.id', '=', 'oauth_grant_scopes.grant_id')
122 12
                           ->where('oauth_grants.id', $grantType);
123 12
        }
124
125 21
        $result = $query->first();
126
127 21
        if (is_null($result)) {
128 9
            return;
129
        }
130
131 12
        $scope = new ScopeEntity($this->getServer());
132 12
        $scope->hydrate([
133 12
            'id' => $result->id,
134 12
            'description' => $result->description,
135 12
        ]);
136
137 12
        return $scope;
138
    }
139
}
140