Completed
Push — develop ( 2d4657...b86812 )
by Nate
04:45
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->environments)) {
116
            $this->defaultEnvironments[] = Craft::$app->getConfig()->env;
117
        }
118
119
        return $this->defaultEnvironments;
120
    }
121
122
    /**
123
     * @param array $environments
124
     * @return $this
125
     */
126
    public function setDefaultEnvironments(array $environments)
127
    {
128
        $this->defaultEnvironments = $environments;
129
        return $this;
130
    }
131
132
    /*******************************************
133
     * PROVIDER OVERRIDE CONFIG
134
     *******************************************/
135
136
    /**
137
     * @param string $providerOverrideFileName
138
     * @return string
139
     */
140
    public function setProviderOverrideFileName(string $providerOverrideFileName): string
141
    {
142
        $this->providerOverrideFileName = $providerOverrideFileName;
143
        return $this;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getProviderOverrideFileName(): string
150
    {
151
        return $this->providerOverrideFileName;
152
    }
153
154
    /*******************************************
155
     * CALLBACK
156
     *******************************************/
157
158
    /**
159
     * @return string
160
     */
161
    public function getCallbackUrl(): string
162
    {
163
        try {
164
            if ($this->callbackUrlPath === null) {
165
                return UrlHelper::siteActionUrl(self::DEFAULT_CALLBACK_URL_PATH);
166
            }
167
168
            return UrlHelper::siteUrl($this->callbackUrlPath);
169
        } catch (Exception $e) {
170
            if ($this->callbackUrlPath === null) {
171
                return UrlHelper::actionUrl(self::DEFAULT_CALLBACK_URL_PATH);
172
            }
173
174
            return UrlHelper::url($this->callbackUrlPath);
175
        }
176
    }
177
178
    /**
179
     * @param $callbackUrlPath
180
     * @return $this
181
     * @throws \yii\base\Exception
182
     */
183
    public function setCallbackUrlPath($callbackUrlPath)
184
    {
185
        $callbackUrlPath = trim(
186
            StringHelper::removeLeft(
187
                (string)$callbackUrlPath,
188
                UrlHelper::siteUrl()
189
            ),
190
            ' /'
191
        );
192
193
        $this->callbackUrlPath = empty($callbackUrlPath) ? null : $callbackUrlPath;
194
195
        return $this;
196
    }
197
198
    /**
199
     * @return string|null
200
     */
201
    public function getCallbackUrlPath()
202
    {
203
        return $this->callbackUrlPath;
204
    }
205
206
    /**
207
     * @return array|null
208
     */
209
    public function getCallbackUrlRule()
210
    {
211
        if ($path = $this->callbackUrlPath) {
212
            return [
213
                $path => self::DEFAULT_CALLBACK_ROUTE
214
            ];
215
        }
216
        return null;
217
    }
218
219
220
    /*******************************************
221
     * PROVIDER SETTINGS VIEW (not currently editable)
222
     *******************************************/
223
224
    /**
225
     * @return ViewInterface
226
     */
227
    public function getProviderSettingsView(): ViewInterface
228
    {
229
        return new Template([
230
            'template' => 'patron/_cp/provider/_settings'
231
        ]);
232
    }
233
234
    /**
235
     * @return ViewInterface
236
     */
237
    public function getTokenView(): ViewInterface
238
    {
239
        return new Template([
240
            'template' => 'patron/_modal/token'
241
        ]);
242
    }
243
244
245
    /**
246
     * @inheritdoc
247
     */
248
    public function rules()
249
    {
250
        return array_merge(
251
            parent::rules(),
252
            [
253
                [
254
                    [
255
                        'callbackUrlPath'
256
                    ],
257
                    UriValidator::class
258
                ],
259
                [
260
                    [
261
                        'callbackUrlPath'
262
                    ],
263
                    'safe',
264
                    'on' => [
265
                        ModelHelper::SCENARIO_DEFAULT
266
                    ]
267
                ]
268
            ]
269
        );
270
    }
271
272
    /**
273
     * @inheritdoc
274
     */
275
    public function attributes()
276
    {
277
        return array_merge(
278
            parent::attributes(),
279
            [
280
                'callbackUrlPath',
281
                'defaultEnvironments',
282
                'environments',
283
                'environment'
284
            ]
285
        );
286
    }
287
288
    /**
289
     * @inheritdoc
290
     */
291
    public function attributeLabels()
292
    {
293
        return array_merge(
294
            parent::attributeLabels(),
295
            [
296
                'callbackUrlPath' => Craft::t('patron', 'Callback Url Path'),
297
            ]
298
        );
299
    }
300
}
301