Passed
Push — master ( b2e6ed...98569b )
by Conrad
01:14
created

OauthContext   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOauthClient() 0 7 2
A hasOauthClient() 0 3 1
A hasScopes() 0 12 3
A hasScope() 0 3 2
1
<?php
2
3
namespace Riddler7\Oauth2GraphQL\Helpers;
4
5
use AdvancedLearning\Oauth2Server\Models\Client;
0 ignored issues
show
Bug introduced by
The type AdvancedLearning\Oauth2Server\Models\Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
trait OauthContext
8
{
9
    /**
10
     * Name of the oauth client id in the graphql context.
11
     *
12
     * @var string
13
     */
14
    protected static $oauthClientKey = 'oauthClientIdentifier';
15
16
    /**
17
     * Name of the oauth scopes in the graphql context.
18
     *
19
     * @var string
20
     */
21
    protected static $oauthScopesKey = 'oauthScopes';
22
23
    /**
24
     * Determine if the scope has a valid client.
25
     *
26
     * @param array $context
27
     *
28
     * @return bool
29
     */
30
    public function hasOauthClient(array $context)
31
    {
32
        return !empty($context[self::$oauthClientKey]);
33
    }
34
35
    /**
36
     * Return the model for the Oauth Client.
37
     *
38
     * @param array $context
39
     *
40
     * @return null|\SilverStripe\ORM\DataObject
0 ignored issues
show
Bug introduced by
The type SilverStripe\ORM\DataObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
     */
42
    public function getOauthClient(array $context)
43
    {
44
        if (!$this->hasOauthClient($context)) {
45
            return null;
46
        }
47
48
        return Client::get()->filter(['Identifier' => $context[self::$oauthClientKey]])->first();
49
    }
50
51
    /**
52
     * Determine whether the a scope has been granted.
53
     *
54
     * @param array  $context
55
     * @param string $scope
56
     *
57
     * @return bool
58
     */
59
    public function hasScope(array $context, string $scope)
60
    {
61
        return !empty($context[self::$oauthScopesKey]) && in_array($scope, $context[self::$oauthScopesKey]);
62
    }
63
64
    /**
65
     * Determnie whether all the scopes have been granted.
66
     *
67
     * @param array $context
68
     * @param array $scopes
69
     *
70
     * @return bool
71
     */
72
    public function hasScopes(array $context, array $scopes)
73
    {
74
        $has = true;
75
76
        foreach ($scopes as $scope) {
77
            // stop once we get the first false result
78
            if (!$this->hasScope($context, $scope)) {
79
                return false;
80
            }
81
        }
82
83
        return $has;
84
    }
85
}
86