Completed
Push — master ( 19b2ae...0e0de4 )
by Nate
04:43
created

GuardianSettings::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 36
cp 0
rs 9.344
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/patron/license
6
 * @link       https://www.flipboxfactory.com/software/patron/
7
 */
8
9
namespace flipbox\patron\settings;
10
11
use Craft;
12
use craft\helpers\Json;
13
use craft\validators\UrlValidator;
14
use flipbox\craft\ember\helpers\ModelHelper;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class GuardianSettings extends BaseSettings
21
{
22
    /**
23
     * The input scope delimiter
24
     */
25
    const SCOPE_DELIMITER = ' ';
26
27
    /**
28
     * @var string
29
     */
30
    public $baseAuthorizationUrl;
31
32
    /**
33
     * @var string
34
     */
35
    public $baseAccessTokenUrl;
36
37
    /**
38
     * @var string
39
     */
40
    public $baseResourceOwnerDetailsUrl;
41
42
    /**
43
     * @var array
44
     */
45
    private $defaultScopes = [];
46
47
    /**
48
     * @return array
49
     */
50
    public function getDefaultScopes(): array
51
    {
52
        return $this->defaultScopes;
53
    }
54
55
    /**
56
     * @param null|string|string[] $scopes
57
     * @return $this
58
     */
59
    public function setDefaultScopes($scopes = null)
60
    {
61
        $this->defaultScopes = [];
62
63
        $scopes = $this->normalizeScopes($scopes);
64
65
        if (!empty($scopes)) {
66
            $this->defaultScopes = $scopes;
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * @param mixed $scopes
74
     * @return array
75
     */
76
    private function normalizeScopes($scopes = null): array
77
    {
78
        if (is_string($scopes)) {
79
            $scopes = Json::decodeIfJson($scopes);
80
81
            // Not json ... it's just a string
82
            if (is_string($scopes)) {
83
                $scopes = explode(
84
                    static::SCOPE_DELIMITER,
85
                    $scopes
86
                );
87
            }
88
        }
89
90
        if (!is_array($scopes)) {
91
            $scopes = [$scopes];
92
        }
93
94
        return array_filter($scopes);
95
    }
96
97
    /**
98
     * @return string
99
     * @throws \Twig_Error_Loader
100
     * @throws \yii\base\Exception
101
     */
102
    public function inputHtml(): string
103
    {
104
        return Craft::$app->getView()->renderTemplate(
105
            'patron/_settings/guardian',
106
            [
107
                'settings' => $this
108
            ]
109
        );
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function attributes()
116
    {
117
        return array_merge(
118
            parent::attributes(),
119
            [
120
                'defaultScopes'
121
            ]
122
        );
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128
    public function attributeLabels()
129
    {
130
        return array_merge(
131
            parent::attributeLabels(),
132
            [
133
                'baseAuthorizationUrl' => Craft::t('patron', "Authorization Url"),
134
                'baseAccessTokenUrl' => Craft::t('patron', "Access Token Url"),
135
                'baseResourceOwnerDetailsUrl' => Craft::t('patron', "Resource Owner Details Url"),
136
                'defaultScopes' => Craft::t('patron', "Default Scopes")
137
            ]
138
        );
139
    }
140
141
    /**
142
     * @inheritdoc
143
     */
144
    public function rules()
145
    {
146
        return array_merge(
147
            parent::rules(),
148
            [
149
                [
150
                    [
151
                        'baseAuthorizationUrl',
152
                        'baseAccessTokenUrl',
153
                        'baseResourceOwnerDetailsUrl',
154
                        'defaultScopes'
155
                    ],
156
                    'safe',
157
                    'on' => [
158
                        ModelHelper::SCENARIO_DEFAULT
159
                    ]
160
                ],
161
                [
162
                    [
163
                        'baseAuthorizationUrl',
164
                        'baseAccessTokenUrl',
165
                        'baseResourceOwnerDetailsUrl'
166
                    ],
167
                    'required'
168
                ],
169
                [
170
                    [
171
                        'baseAuthorizationUrl',
172
                        'baseAccessTokenUrl',
173
                        'baseResourceOwnerDetailsUrl'
174
                    ],
175
                    UrlValidator::class
176
                ]
177
            ]
178
        );
179
    }
180
}
181