Passed
Push — main ( de60d9...c38ba8 )
by Peter
04:15
created

Options   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 94.87%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
c 2
b 0
f 0
dl 0
loc 86
ccs 37
cts 39
cp 0.9487
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 3 1
A __construct() 0 11 1
A getScopes() 0 3 1
A make() 0 6 1
A getDefaults() 0 9 1
A validateOptions() 0 23 1
1
<?php
2
3
namespace Pelmered\LaravelHttpOAuthHelper;
4
5
use Closure;
6
use Illuminate\Support\Facades\Validator;
7
use Illuminate\Validation\Rule;
8
9
class Options
10
{
11
    /**
12
     * @param  array<string>  $scopes
13
     */
14 18
    final public function __construct(
15
        public array $scopes = [],
16
        public string $authType = Credentials::AUTH_TYPE_BASIC, //TODO: Which auth type should be default?
17
        public string $grantType = Credentials::GRANT_TYPE_CLIENT_CREDENTIALS,
18
        public string $tokenType = AccessToken::TYPE_BEARER,
19
        public string $tokenName = 'token',
20
        public int|string|Closure $expires = 3600,
21
        public string|Closure $accessToken = 'access_token',
22
        public ?Closure $tokenTypeCustomCallback = null,
23
    ) {
24 18
        $this->validateOptions();
25
    }
26
27
    /*
28
    public function __get($name)
29
    {
30
        return $this->$name;
31
    }
32
    */
33
34
    /**
35
     * @return array<string, string>
36
     */
37
    public function toArray(): array
38
    {
39
        return get_object_vars($this);
40
    }
41
42 18
    protected function validateOptions(): void
43
    {
44
        // Note: closures can't be checked at this point since we don't have access to the response objects
45 18
        Validator::make((array) $this, [
46 18
            'scopes.*' => 'string',
47 18
            'authType' => Rule::in([
48 18
                Credentials::AUTH_TYPE_BEARER,
49 18
                Credentials::AUTH_TYPE_BODY,
50 18
                Credentials::AUTH_TYPE_QUERY,
51 18
                Credentials::AUTH_TYPE_BASIC,
52 18
                Credentials::AUTH_TYPE_CUSTOM,
53 18
            ]),
54 18
            'grantType' => Rule::in([
55 18
                Credentials::GRANT_TYPE_CLIENT_CREDENTIALS,
56 18
                Credentials::GRANT_TYPE_PASSWORD_CREDENTIALS,
57 18
            ]),
58 18
            'tokenType' => Rule::in([
59 18
                AccessToken::TYPE_BEARER,
60 18
                AccessToken::TYPE_QUERY,
61 18
                AccessToken::TYPE_CUSTOM,
62 18
            ]),
63 18
            'tokenName' => 'string',
64 18
        ])->validate();
65
    }
66
67 12
    public function getScopes(): string
68
    {
69 12
        return implode(' ', $this->scopes);
70
    }
71
72
    /**
73
     * @param  array<string, mixed>  ...$parameters
74
     */
75 4
    public static function make(...$parameters): static
76
    {
77 4
        $defaults = static::getDefaults();
78 4
        $options  = array_merge($defaults, ...$parameters);
79
80 4
        return new static(...$options);
0 ignored issues
show
Bug introduced by
$options is expanded, but the parameter $scopes of Pelmered\LaravelHttpOAut...\Options::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        return new static(/** @scrutinizer ignore-type */ ...$options);
Loading history...
81
    }
82
83
    /**
84
     * @return array<string, mixed>
85
     */
86 4
    protected static function getDefaults(): array
87
    {
88 4
        return [
89 4
            'scopes'      => [],
90 4
            'grantType'   => Credentials::GRANT_TYPE_CLIENT_CREDENTIALS,
91 4
            'tokenType'   => AccessToken::TYPE_BEARER,
92 4
            'authType'    => Credentials::AUTH_TYPE_BASIC,
93 4
            'expires'     => 3600,
94 4
            'accessToken' => 'access_token',
95 4
        ];
96
    }
97
}
98