Completed
Push — develop ( 94b72f...27afa3 )
by Nate
03:15
created

Settings::getEnvironments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
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\craft\ember\helpers\ModelHelper;
16
use flipbox\craft\ember\helpers\UrlHelper;
17
use flipbox\craft\ember\views\Template;
18
use flipbox\craft\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 array
44
     */
45
    private $environments = [];
46
47
    /**
48
     * @var string
49
     */
50
    private $environment = null;
51
52
    /**
53
     * Encrypt data in storage
54
     *
55
     * @var bool
56
     */
57
    private $encryptStorageData = true;
58
59
    /**
60
     * Default environments to apply to new Providers when they're created
61
     *
62
     * @var array
63
     */
64
    private $defaultEnvironments = [];
65
66
    /**
67
     * Auto populate token enviornments upon creation.
68
     *
69
     * @var bool
70
     */
71
    private $autoPopulateTokenEnvironments = true;
72
73
    /**
74
     * If [[Settings::$autoPopulateTokenEnvironments]] is true, and this is enabled, the environments
75
     * will mirror the provider environments.
76
     *
77
     * @var bool
78
     */
79
    private $applyProviderEnvironmentsToToken = false;
80
81
82
    /*******************************************
83
     * ENCRYPTION
84
     *******************************************/
85
86
    /**
87
     * @return bool
88
     */
89
    public function getEncryptStorageData(): bool
90
    {
91
        return (bool)$this->encryptStorageData;
92
    }
93
94
    /**
95
     * @param bool $value
96
     * @return $this
97
     */
98
    public function setEncryptStorageData(bool $value)
99
    {
100
        $this->encryptStorageData = $value;
101
        return $this;
102
    }
103
104
    /*******************************************
105
     * TOKEN ENVIORNMENTS
106
     *******************************************/
107
108
    /**
109
     * @return bool
110
     */
111 3
    public function getAutoPopulateTokenEnvironments(): bool
112
    {
113 3
        return (bool)$this->autoPopulateTokenEnvironments;
114
    }
115
116
    /**
117
     * @param bool $value
118
     * @return $this
119
     */
120
    public function setAutoPopulateTokenEnvironments(bool $value)
121
    {
122
        $this->autoPopulateTokenEnvironments = $value;
123
        return $this;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    public function getApplyProviderEnvironmentsToToken(): bool
130
    {
131
        return (bool)$this->applyProviderEnvironmentsToToken;
132
    }
133
134
    /**
135
     * @param bool $value
136
     * @return $this
137
     */
138
    public function setApplyProviderEnvironmentsToToken(bool $value)
139
    {
140
        $this->applyProviderEnvironmentsToToken = $value;
141
        return $this;
142
    }
143
144
    /*******************************************
145
     * ENVIRONMENTS
146
     *******************************************/
147
148
    /**
149
     * @return string
150
     */
151
    public function getEnvironment(): string
152
    {
153
        if ($this->environment === null) {
154
            $this->environment = Craft::$app->getConfig()->env;
155
        }
156
157
        return $this->environment;
158
    }
159
160
    /**
161
     * @param string $environment
162
     * @return $this
163
     */
164
    public function setEnvironment(string $environment)
165
    {
166
        $this->environment = $environment;
167
        return $this;
168
    }
169
170
    /**
171
     * @return array
172
     */
173 3
    public function getEnvironments(): array
174
    {
175 3
        if (empty($this->environments)) {
176 3
            $this->environments[] = Craft::$app->getConfig()->env;
177
        }
178
179 3
        return $this->environments;
180
    }
181
182
    /**
183
     * @param array $environments
184
     * @return $this
185
     */
186
    public function setEnvironments(array $environments)
187
    {
188
        $this->environments = $environments;
189
        return $this;
190
    }
191
192
    /**
193
     * @return array
194
     */
195 3
    public function getDefaultEnvironments(): array
196
    {
197 3
        if (empty($this->defaultEnvironments)) {
198 3
            $this->defaultEnvironments[] = Craft::$app->getConfig()->env;
199
        }
200
201 3
        return array_intersect(
202 3
            $this->getEnvironments(),
203 3
            $this->defaultEnvironments
204
        );
205
    }
206
207
    /**
208
     * @param array $environments
209
     * @return $this
210
     */
211
    public function setDefaultEnvironments(array $environments)
212
    {
213
        $this->defaultEnvironments = $environments;
214
        return $this;
215
    }
216
217
    /*******************************************
218
     * PROVIDER OVERRIDE CONFIG
219
     *******************************************/
220
221
    /**
222
     * @param string $providerOverrideFileName
223
     * @return string
224
     */
225
    public function setProviderOverrideFileName(string $providerOverrideFileName): string
226
    {
227
        $this->providerOverrideFileName = $providerOverrideFileName;
0 ignored issues
show
Documentation introduced by
The property providerOverrideFileName does not exist on object<flipbox\patron\models\Settings>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
228
        return $this;
229
    }
230
231
    /**
232
     * @return string
233
     */
234
    public function getProviderOverrideFileName(): string
235
    {
236
        return $this->providerOverrideFileName;
0 ignored issues
show
Documentation introduced by
The property providerOverrideFileName does not exist on object<flipbox\patron\models\Settings>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
237
    }
238
239
    /*******************************************
240
     * CALLBACK
241
     *******************************************/
242
243
    /**
244
     * @return string
245
     */
246
    public function getCallbackUrl(): string
247
    {
248
        try {
249
            if ($this->callbackUrlPath === null) {
250
                return UrlHelper::siteActionUrl(self::DEFAULT_CALLBACK_URL_PATH);
251
            }
252
253
            return UrlHelper::siteUrl($this->callbackUrlPath);
254
        } catch (Exception $e) {
255
            if ($this->callbackUrlPath === null) {
256
                return UrlHelper::actionUrl(self::DEFAULT_CALLBACK_URL_PATH);
257
            }
258
259
            return UrlHelper::url($this->callbackUrlPath);
260
        }
261
    }
262
263
    /**
264
     * @param $callbackUrlPath
265
     * @return $this
266
     * @throws \yii\base\Exception
267
     */
268
    public function setCallbackUrlPath($callbackUrlPath)
269
    {
270
        $callbackUrlPath = trim(
271
            StringHelper::removeLeft(
272
                (string)$callbackUrlPath,
273
                UrlHelper::siteUrl()
274
            ),
275
            ' /'
276
        );
277
278
        $this->callbackUrlPath = empty($callbackUrlPath) ? null : $callbackUrlPath;
279
280
        return $this;
281
    }
282
283
    /**
284
     * @return string|null
285
     */
286
    public function getCallbackUrlPath()
287
    {
288
        return $this->callbackUrlPath;
289
    }
290
291
    /**
292
     * @return array|null
293
     */
294
    public function getCallbackUrlRule()
295
    {
296
        if ($path = $this->callbackUrlPath) {
297
            return [
298
                $path => self::DEFAULT_CALLBACK_ROUTE
299
            ];
300
        }
301
        return null;
302
    }
303
304
305
    /*******************************************
306
     * PROVIDER SETTINGS VIEW (not currently editable)
307
     *******************************************/
308
309
    /**
310
     * @return ViewInterface
311
     */
312
    public function getProviderEnvironmentView(): ViewInterface
313
    {
314
        return new Template([
315
            'template' => 'patron/_cp/provider/_environment'
316
        ]);
317
    }
318
319
    /**
320
     * @return ViewInterface
321
     */
322
    public function getProviderSettingsView(): ViewInterface
323
    {
324
        return new Template([
325
            'template' => 'patron/_cp/provider/_settings'
326
        ]);
327
    }
328
329
    /**
330
     * @return ViewInterface
331
     */
332
    public function getTokenView(): ViewInterface
333
    {
334
        return new Template([
335
            'template' => 'patron/_modal/token'
336
        ]);
337
    }
338
339
340
    /**
341
     * @inheritdoc
342
     */
343
    public function rules()
344
    {
345
        return array_merge(
346
            parent::rules(),
347
            [
348
                [
349
                    [
350
                        'callbackUrlPath'
351
                    ],
352
                    UriValidator::class
353
                ],
354
                [
355
                    [
356
                        'encryptStorageData',
357
                        'applyProviderEnvironmentsToToken',
358
                        'autoPopulateTokenEnvironments'
359
                    ],
360
                    'boolean'
361
                ],
362
                [
363
                    [
364
                        'callbackUrlPath',
365
                        'defaultEnvironments',
366
                        'encryptStorageData',
367
                        'environments',
368
                        'applyProviderEnvironmentsToToken',
369
                        'autoPopulateTokenEnvironments',
370
                    ],
371
                    'safe',
372
                    'on' => [
373
                        ModelHelper::SCENARIO_DEFAULT
374
                    ]
375
                ]
376
            ]
377
        );
378
    }
379
380
    /**
381
     * @inheritdoc
382
     */
383
    public function attributes()
384
    {
385
        return array_merge(
386
            parent::attributes(),
387
            [
388
                'callbackUrlPath',
389
                'encryptStorageData',
390
                'environments',
391
                'applyProviderEnvironmentsToToken',
392
                'autoPopulateTokenEnvironments',
393
            ]
394
        );
395
    }
396
397
    /**
398
     * @inheritdocå
399
     */
400
    public function attributeLabels()
401
    {
402
        return array_merge(
403
            parent::attributeLabels(),
404
            [
405
                'callbackUrlPath' => Craft::t('patron', 'Callback Url Path'),
406
                'defaultEnvironments' => Craft::t('patron', 'Default Enviornments'),
407
                'encryptStorageData' => Craft::t('patron', 'Encrypt Storage Data'),
408
                'environments' => Craft::t('patron', 'Environments'),
409
                'autoPopulateTokenEnvironments' => Craft::t('patron', 'Auto Populate Token Environments'),
410
                'applyProviderEnvironmentsToToken' => Craft::t('patron', 'Apply Provider Environments to Token')
411
            ]
412
        );
413
    }
414
}
415