Completed
Push — develop ( e45dbf...2d4657 )
by Nate
04:34
created

Settings::setEnvironment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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