Completed
Push — master ( 424357...d1d6a1 )
by Nate
24:35 queued 09:39
created

Settings::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 9
cp 0
rs 9.552
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\models;
10
11
use Craft;
12
use craft\base\Model;
13
use craft\helpers\StringHelper;
14
use craft\validators\UriValidator;
15
use flipbox\craft\ember\helpers\UrlHelper;
16
use flipbox\craft\ember\views\Template;
17
use flipbox\craft\ember\views\ViewInterface;
18
use yii\base\Exception;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class Settings extends Model
25
{
26
    /**
27
     * The callback url path
28
     */
29
    const DEFAULT_CALLBACK_URL_PATH = 'patron/authorization/callback';
30
31
    /**
32
     * Tge callback url route
33
     */
34
    const DEFAULT_CALLBACK_ROUTE = self::DEFAULT_CALLBACK_URL_PATH;
35
36
    /**
37
     * @var string|null
38
     */
39
    private $callbackUrlPath;
40
41
    /**
42
     * @var array|null
43
     */
44
    private $providers = [];
45
46
47
    /*******************************************
48
     * PROVIDER OVERRIDES
49
     *******************************************/
50
51
    /**
52
     * Get an array provider override configurations
53
     *
54
     * @return array
55
     */
56
    public function getProviders(): array
57
    {
58
        return $this->providers;
59
    }
60
61
    /**
62
     * Set an array provider override configurations
63
     *
64
     * @param array $providers
65
     * @return static
66
     */
67
    public function setProviders(array $providers)
68
    {
69
        $this->providers = $providers;
70
        return $this;
71
    }
72
73
    /**
74
     * Get a provider override configuration by the provider handle
75
     *
76
     * @param string $handle
77
     * @return array
78
     */
79
    public function getProvider(string $handle): array
80
    {
81
        return $this->providers[$handle] ?? [];
82
    }
83
84
85
    /*******************************************
86
     * ENCRYPTION [DEPRECATED]
87
     *******************************************/
88
89
    /**
90
     * @param bool $value
91
     * @return $this
92
     *
93
     * @deprecated
94
     */
95
    public function setEncryptStorageData(bool $value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
    {
97
        return $this;
98
    }
99
100
    /*******************************************
101
     * TOKEN ENVIRONMENTS [DEPRECATED]
102
     *******************************************/
103
104
    /**
105
     * @param bool $value
106
     * @return $this
107
     *
108
     * @deprecated
109
     */
110
    public function setAutoPopulateTokenEnvironments(bool $value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111 3
    {
112
        return $this;
113 3
    }
114
115
    /**
116
     * @param bool $value
117
     * @return $this
118
     *
119
     * @deprecated
120
     */
121
    public function setApplyProviderEnvironmentsToToken(bool $value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
    {
123
        return $this;
124
    }
125
126
    /*******************************************
127
     * ENVIRONMENTS [DEPRECATED]
128
     *******************************************/
129
130
    /**
131
     * @param string $environment
132
     * @return $this
133
     *
134
     * @deprecated
135
     */
136
    public function setEnvironment(string $environment)
0 ignored issues
show
Unused Code introduced by
The parameter $environment is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
    {
138
        return $this;
139
    }
140
141
    /**
142
     * @param array $environments
143
     * @return $this
144
     *
145
     * @deprecated
146
     */
147
    public function setEnvironments(array $environments)
0 ignored issues
show
Unused Code introduced by
The parameter $environments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
        return $this;
150
    }
151
152
153
    /**
154
     * @param array $environments
155
     * @return $this
156
     *
157
     * @deprecated
158
     */
159
    public function setDefaultEnvironments(array $environments)
0 ignored issues
show
Unused Code introduced by
The parameter $environments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
    {
161
        return $this;
162
    }
163
164
    /*******************************************
165
     * CALLBACK
166
     *******************************************/
167
168
    /**
169
     * @return string
170
     */
171
    public function getCallbackUrl(): string
172
    {
173 3
        try {
174
            if ($this->callbackUrlPath === null) {
175 3
                return UrlHelper::siteActionUrl(self::DEFAULT_CALLBACK_URL_PATH);
176 3
            }
177
178
            return UrlHelper::siteUrl($this->callbackUrlPath);
179 3
        } catch (Exception $e) {
180
            if ($this->callbackUrlPath === null) {
181
                return UrlHelper::actionUrl(self::DEFAULT_CALLBACK_URL_PATH);
182
            }
183
184
            return UrlHelper::url($this->callbackUrlPath);
185
        }
186
    }
187
188
    /**
189
     * @param $callbackUrlPath
190
     * @return $this
191
     * @throws Exception
192
     */
193
    public function setCallbackUrlPath($callbackUrlPath)
194
    {
195 3
        $callbackUrlPath = trim(
196
            StringHelper::removeLeft(
197 3
                (string)$callbackUrlPath,
198 3
                UrlHelper::siteUrl()
199
            ),
200
            ' /'
201 3
        );
202 3
203 3
        $this->callbackUrlPath = empty($callbackUrlPath) ? null : $callbackUrlPath;
204
205
        return $this;
206
    }
207
208
    /**
209
     * @return string|null
210
     */
211
    public function getCallbackUrlPath()
212
    {
213
        return $this->callbackUrlPath;
214
    }
215
216
    /**
217
     * @return array|null
218
     */
219
    public function getCallbackUrlRule()
220
    {
221
        if ($path = $this->callbackUrlPath) {
222
            return [
223
                $path => self::DEFAULT_CALLBACK_ROUTE
224
            ];
225
        }
226
        return null;
227
    }
228
229
230
    /*******************************************
231
     * PROVIDER SETTINGS VIEW (not currently editable)
232
     *******************************************/
233
234
    /**
235
     * @return ViewInterface'
0 ignored issues
show
Documentation introduced by
The doc-type ViewInterface' could not be parsed: Unknown type name "ViewInterface'" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
236
     */
237
    public function getProviderSettingsView(): ViewInterface
238
    {
239
        return new Template([
240
            'template' => 'patron/_cp/provider/_settings'
241
        ]);
242
    }
243
244
    /**
245
     * @inheritdoc
246
     */
247
    public function rules()
248
    {
249
        return array_merge(
250
            parent::rules(),
251
            [
252
                [
253
                    [
254
                        'callbackUrlPath'
255
                    ],
256
                    UriValidator::class
257
                ],
258
                [
259
                    [
260
                        'providers'
261
                    ],
262
                    'safe',
263
                    'on' => [
264
                        self::SCENARIO_DEFAULT
265
                    ]
266
                ]
267
            ]
268
        );
269
    }
270
271
    /**
272
     * @inheritdoc
273
     */
274
    public function attributes()
275
    {
276
        return array_merge(
277
            parent::attributes(),
278
            [
279
                'callbackUrlPath',
280
                'providers'
281
            ]
282
        );
283
    }
284
285
    /**
286
     * @inheritdocå
287
     */
288
    public function attributeLabels()
289
    {
290
        return array_merge(
291
            parent::attributeLabels(),
292
            [
293
                'callbackUrlPath' => Craft::t('patron', 'Callback Url Path'),
294
                'providers' => Craft::t('patron', 'Provider Overrides')
295
            ]
296
        );
297
    }
298
}
299