Completed
Push — develop ( 45ab37...1fc05f )
by Nate
16:44
created

Settings::getDefaultEnvironments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
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\ember\helpers\ModelHelper;
16
use flipbox\ember\helpers\UrlHelper;
17
use flipbox\ember\views\Template;
18
use flipbox\ember\views\ViewInterface;
19
use yii\base\Exception;
20
21
/**
22
 * @author Flipbox Factory <[email protected]>
23
 * @since 1.0.0
24
 */
25
class Settings extends Model
26
{
27
    /**
28
     * The callback url path
29
     */
30
    const DEFAULT_CALLBACK_URL_PATH = 'patron/authorization/callback';
31
32
    /**
33
     * Tge callback url route
34
     */
35
    const DEFAULT_CALLBACK_ROUTE = self::DEFAULT_CALLBACK_URL_PATH;
36
37
    /**
38
     * @var string|null
39
     */
40
    private $callbackUrlPath;
41
42
    /**
43
     * @var string
44
     */
45
    private $providerOverrideFileName = 'providers';
46
47
    /**
48
     * @var array
49
     */
50
    private $environments = [];
51
52
    /**
53
     * @var string
54
     */
55
    private $environment = null;
56
57
    /**
58
     * Encrypt data in storage
59
     *
60
     * @var bool
61
     */
62
    private $encryptStorageData = true;
63
64
    /**
65
     * @var array
66
     */
67
    private $defaultEnvironments = [];
68
69
    /**
70
     * @var bool
71
     */
72
    public $applyProviderEnvironmentsToToken = true;
73
74
75
    /*******************************************
76
     * ENCRYPTION
77
     *******************************************/
78
79
    /**
80
     * @return bool
81
     */
82
    public function getEncryptStorageData(): bool
83
    {
84
        return (bool)$this->encryptStorageData;
85
    }
86
87
    /**
88
     * @param bool $value
89
     * @return $this
90
     */
91
    public function setEncryptStorageData(bool $value)
92
    {
93
        $this->encryptStorageData = $value;
94
        return $this;
95
    }
96
97
    /*******************************************
98
     * ENVIRONMENTS
99
     *******************************************/
100
101
    /**
102
     * @return string
103
     */
104
    public function getEnvironment(): string
105
    {
106
        if ($this->environment === null) {
107
            $this->environment = Craft::$app->getConfig()->env;
108
        }
109
110
        return $this->environment;
111
    }
112
113
    /**
114
     * @param string $environment
115
     * @return $this
116
     */
117
    public function setEnvironment(string $environment)
118
    {
119
        $this->environment = $environment;
120
        return $this;
121
    }
122
123
    /**
124
     * @return array
125
     */
126 18
    public function getEnvironments(): array
127
    {
128 18
        if (empty($this->environments)) {
129 18
            $this->environments[] = Craft::$app->getConfig()->env;
130
        }
131
132 18
        return $this->environments;
133
    }
134
135
    /**
136
     * @param array $environments
137
     * @return $this
138
     */
139
    public function setEnvironments(array $environments)
140
    {
141
        $this->environments = $environments;
142
        return $this;
143
    }
144
145
    /**
146
     * @return array
147
     */
148 18
    public function getDefaultEnvironments(): array
149
    {
150 18
        if (empty($this->defaultEnvironments)) {
151 18
            $this->defaultEnvironments[] = Craft::$app->getConfig()->env;
152
        }
153
154 18
        return array_intersect(
155 18
            $this->getEnvironments(),
156 18
            $this->defaultEnvironments
157
        );
158
    }
159
160
    /**
161
     * @param array $environments
162
     * @return $this
163
     */
164
    public function setDefaultEnvironments(array $environments)
165
    {
166
        $this->defaultEnvironments = $environments;
167
        return $this;
168
    }
169
170
    /*******************************************
171
     * PROVIDER OVERRIDE CONFIG
172
     *******************************************/
173
174
    /**
175
     * @param string $providerOverrideFileName
176
     * @return string
177
     */
178
    public function setProviderOverrideFileName(string $providerOverrideFileName): string
179
    {
180
        $this->providerOverrideFileName = $providerOverrideFileName;
181
        return $this;
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getProviderOverrideFileName(): string
188
    {
189
        return $this->providerOverrideFileName;
190
    }
191
192
    /*******************************************
193
     * CALLBACK
194
     *******************************************/
195
196
    /**
197
     * @return string
198
     */
199
    public function getCallbackUrl(): string
200
    {
201
        try {
202
            if ($this->callbackUrlPath === null) {
203
                return UrlHelper::siteActionUrl(self::DEFAULT_CALLBACK_URL_PATH);
204
            }
205
206
            return UrlHelper::siteUrl($this->callbackUrlPath);
207
        } catch (Exception $e) {
208
            if ($this->callbackUrlPath === null) {
209
                return UrlHelper::actionUrl(self::DEFAULT_CALLBACK_URL_PATH);
210
            }
211
212
            return UrlHelper::url($this->callbackUrlPath);
213
        }
214
    }
215
216
    /**
217
     * @param $callbackUrlPath
218
     * @return $this
219
     * @throws \yii\base\Exception
220
     */
221
    public function setCallbackUrlPath($callbackUrlPath)
222
    {
223
        $callbackUrlPath = trim(
224
            StringHelper::removeLeft(
225
                (string)$callbackUrlPath,
226
                UrlHelper::siteUrl()
227
            ),
228
            ' /'
229
        );
230
231
        $this->callbackUrlPath = empty($callbackUrlPath) ? null : $callbackUrlPath;
232
233
        return $this;
234
    }
235
236
    /**
237
     * @return string|null
238
     */
239
    public function getCallbackUrlPath()
240
    {
241
        return $this->callbackUrlPath;
242
    }
243
244
    /**
245
     * @return array|null
246
     */
247
    public function getCallbackUrlRule()
248
    {
249
        if ($path = $this->callbackUrlPath) {
250
            return [
251
                $path => self::DEFAULT_CALLBACK_ROUTE
252
            ];
253
        }
254
        return null;
255
    }
256
257
258
    /*******************************************
259
     * PROVIDER SETTINGS VIEW (not currently editable)
260
     *******************************************/
261
262
    /**
263
     * @return ViewInterface
264
     */
265
    public function getProviderEnvironmentView(): ViewInterface
266
    {
267
        return new Template([
268
            'template' => 'patron/_cp/provider/_environment'
269
        ]);
270
    }
271
272
    /**
273
     * @return ViewInterface
274
     */
275
    public function getProviderSettingsView(): ViewInterface
276
    {
277
        return new Template([
278
            'template' => 'patron/_cp/provider/_settings'
279
        ]);
280
    }
281
282
    /**
283
     * @return ViewInterface
284
     */
285
    public function getTokenView(): ViewInterface
286
    {
287
        return new Template([
288
            'template' => 'patron/_modal/token'
289
        ]);
290
    }
291
292
293
    /**
294
     * @inheritdoc
295
     */
296
    public function rules()
297
    {
298
        return array_merge(
299
            parent::rules(),
300
            [
301
                [
302
                    [
303
                        'callbackUrlPath'
304
                    ],
305
                    UriValidator::class
306
                ],
307
                [
308
                    [
309
                        'encryptStorageData'
310
                    ],
311
                    'boolean'
312
                ],
313
                [
314
                    [
315
                        'encryptStorageData',
316
                        'callbackUrlPath'
317
                    ],
318
                    'safe',
319
                    'on' => [
320
                        ModelHelper::SCENARIO_DEFAULT
321
                    ]
322
                ]
323
            ]
324
        );
325
    }
326
327
    /**
328
     * @inheritdoc
329
     */
330
    public function attributes()
331
    {
332
        return array_merge(
333
            parent::attributes(),
334
            [
335
                'callbackUrlPath',
336
                'defaultEnvironments',
337
                'encryptStorageData',
338
                'environments',
339
                'environment'
340
            ]
341
        );
342
    }
343
344
    /**
345
     * @inheritdoc
346
     */
347
    public function attributeLabels()
348
    {
349
        return array_merge(
350
            parent::attributeLabels(),
351
            [
352
                'callbackUrlPath' => Craft::t('patron', 'Callback Url Path'),
353
            ]
354
        );
355
    }
356
}
357