Completed
Push — master ( e45dbf...3a73d5 )
by Nate
05:43 queued 04:08
created

Settings::getEnvironments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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
     * @var array
59
     */
60
    private $defaultEnvironments = [];
61
62
    /*******************************************
63
     * ENVIRONMENTS
64
     *******************************************/
65
66
    /**
67
     * @return string
68
     */
69
    public function getEnvironment(): string
70
    {
71
        if ($this->environment === null) {
72
            $this->environment = Craft::$app->getConfig()->env;
73
        }
74
75
        return $this->environment;
76
    }
77
78
    /**
79
     * @param string $environment
80
     * @return $this
81
     */
82
    public function setEnvironment(string $environment)
83
    {
84
        $this->environment = $environment;
85
        return $this;
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function getEnvironments(): array
92
    {
93
        if (empty($this->environments)) {
94
            $this->environments[] = Craft::$app->getConfig()->env;
95
        }
96
97
        return $this->environments;
98
    }
99
100
    /**
101
     * @param array $environments
102
     * @return $this
103
     */
104
    public function setEnvironments(array $environments)
105
    {
106
        $this->environments = $environments;
107
        return $this;
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    public function getDefaultEnvironments(): array
114
    {
115
        if (empty($this->defaultEnvironments)) {
116
            $this->defaultEnvironments[] = Craft::$app->getConfig()->env;
117
        }
118
119
        return array_intersect(
120
            $this->getEnvironments(),
121
            $this->defaultEnvironments
122
        );
123
    }
124
125
    /**
126
     * @param array $environments
127
     * @return $this
128
     */
129
    public function setDefaultEnvironments(array $environments)
130
    {
131
        $this->defaultEnvironments = $environments;
132
        return $this;
133
    }
134
135
    /*******************************************
136
     * PROVIDER OVERRIDE CONFIG
137
     *******************************************/
138
139
    /**
140
     * @param string $providerOverrideFileName
141
     * @return string
142
     */
143
    public function setProviderOverrideFileName(string $providerOverrideFileName): string
144
    {
145
        $this->providerOverrideFileName = $providerOverrideFileName;
146
        return $this;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getProviderOverrideFileName(): string
153
    {
154
        return $this->providerOverrideFileName;
155
    }
156
157
    /*******************************************
158
     * CALLBACK
159
     *******************************************/
160
161
    /**
162
     * @return string
163
     */
164
    public function getCallbackUrl(): string
165
    {
166
        try {
167
            if ($this->callbackUrlPath === null) {
168
                return UrlHelper::siteActionUrl(self::DEFAULT_CALLBACK_URL_PATH);
169
            }
170
171
            return UrlHelper::siteUrl($this->callbackUrlPath);
172
        } catch (Exception $e) {
173
            if ($this->callbackUrlPath === null) {
174
                return UrlHelper::actionUrl(self::DEFAULT_CALLBACK_URL_PATH);
175
            }
176
177
            return UrlHelper::url($this->callbackUrlPath);
178
        }
179
    }
180
181
    /**
182
     * @param $callbackUrlPath
183
     * @return $this
184
     * @throws \yii\base\Exception
185
     */
186
    public function setCallbackUrlPath($callbackUrlPath)
187
    {
188
        $callbackUrlPath = trim(
189
            StringHelper::removeLeft(
190
                (string)$callbackUrlPath,
191
                UrlHelper::siteUrl()
192
            ),
193
            ' /'
194
        );
195
196
        $this->callbackUrlPath = empty($callbackUrlPath) ? null : $callbackUrlPath;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @return string|null
203
     */
204
    public function getCallbackUrlPath()
205
    {
206
        return $this->callbackUrlPath;
207
    }
208
209
    /**
210
     * @return array|null
211
     */
212
    public function getCallbackUrlRule()
213
    {
214
        if ($path = $this->callbackUrlPath) {
215
            return [
216
                $path => self::DEFAULT_CALLBACK_ROUTE
217
            ];
218
        }
219
        return null;
220
    }
221
222
223
    /*******************************************
224
     * PROVIDER SETTINGS VIEW (not currently editable)
225
     *******************************************/
226
227
    /**
228
     * @return ViewInterface
229
     */
230
    public function getProviderSettingsView(): ViewInterface
231
    {
232
        return new Template([
233
            'template' => 'patron/_cp/provider/_settings'
234
        ]);
235
    }
236
237
    /**
238
     * @return ViewInterface
239
     */
240
    public function getTokenView(): ViewInterface
241
    {
242
        return new Template([
243
            'template' => 'patron/_modal/token'
244
        ]);
245
    }
246
247
248
    /**
249
     * @inheritdoc
250
     */
251
    public function rules()
252
    {
253
        return array_merge(
254
            parent::rules(),
255
            [
256
                [
257
                    [
258
                        'callbackUrlPath'
259
                    ],
260
                    UriValidator::class
261
                ],
262
                [
263
                    [
264
                        'callbackUrlPath'
265
                    ],
266
                    'safe',
267
                    'on' => [
268
                        ModelHelper::SCENARIO_DEFAULT
269
                    ]
270
                ]
271
            ]
272
        );
273
    }
274
275
    /**
276
     * @inheritdoc
277
     */
278
    public function attributes()
279
    {
280
        return array_merge(
281
            parent::attributes(),
282
            [
283
                'callbackUrlPath',
284
                'defaultEnvironments',
285
                'environments',
286
                'environment'
287
            ]
288
        );
289
    }
290
291
    /**
292
     * @inheritdoc
293
     */
294
    public function attributeLabels()
295
    {
296
        return array_merge(
297
            parent::attributeLabels(),
298
            [
299
                'callbackUrlPath' => Craft::t('patron', 'Callback Url Path'),
300
            ]
301
        );
302
    }
303
}
304