Completed
Push — develop ( 55c38a...c1b735 )
by Nate
12:53
created

Settings::getProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
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\craft\ember\helpers\UrlHelper;
16
use flipbox\craft\ember\views\Template;
17
use flipbox\craft\ember\views\ViewInterface;
18
use yii\base\Exception;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
class Settings extends Model
25
{
26
    /**
27
     * The callback url path
28
     */
29
    const DEFAULT_CALLBACK_URL_PATH = 'patron/authorization/callback';
30
31
    /**
32
     * Tge callback url route
33
     */
34
    const DEFAULT_CALLBACK_ROUTE = self::DEFAULT_CALLBACK_URL_PATH;
35
36
    /**
37
     * @var string|null
38
     */
39
    private $callbackUrlPath;
40
41
    /**
42
     * @var array|null
43
     */
44
    private $providers = [];
45
46
47
    /*******************************************
48
     * PROVIDER OVERRIDES
49
     *******************************************/
50
51
    /**
52
     * Get an array provider override configurations
53
     *
54
     * @return array
55
     */
56
    public function getProviders(): array
57
    {
58
        return $this->providers;
59
    }
60
61
    /**
62
     * Set an array provider override configurations
63
     *
64
     * @param array $providers
65
     * @return static
66
     */
67
    public function setProviders(array $providers)
68
    {
69
        $this->providers = $providers;
70
        return $this;
71
    }
72
73
    /**
74
     * Get a provider override configuration by the provider handle
75
     *
76
     * @param string $handle
77
     * @return array
78
     */
79
    public function getProvider(string $handle): array
80
    {
81
        return $this->providers[$handle] ?? [];
82
    }
83
84
85
    /*******************************************
86
     * ENCRYPTION [DEPRECATED]
87
     *******************************************/
88
89
    /**
90
     * @param bool $value
91
     * @return $this
92
     *
93
     * @deprecated
94
     */
95
    public function setEncryptStorageData(bool $value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
    {
97
        return $this;
98
    }
99
100
    /*******************************************
101
     * TOKEN ENVIRONMENTS [DEPRECATED]
102
     *******************************************/
103
104
    /**
105
     * @param bool $value
106
     * @return $this
107
     *
108
     * @deprecated
109
     */
110
    public function setAutoPopulateTokenEnvironments(bool $value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112
        return $this;
113
    }
114
115
    /**
116
     * @param bool $value
117
     * @return $this
118
     *
119
     * @deprecated
120
     */
121
    public function setApplyProviderEnvironmentsToToken(bool $value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
    {
123
        return $this;
124
    }
125
126
    /*******************************************
127
     * ENVIRONMENTS [DEPRECATED]
128
     *******************************************/
129
130
    /**
131
     * @param string $environment
132
     * @return $this
133
     *
134
     * @deprecated
135
     */
136
    public function setEnvironment(string $environment)
0 ignored issues
show
Unused Code introduced by
The parameter $environment is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
    {
138
        return $this;
139
    }
140
141
    /**
142
     * @param array $environments
143
     * @return $this
144
     *
145
     * @deprecated
146
     */
147
    public function setEnvironments(array $environments)
0 ignored issues
show
Unused Code introduced by
The parameter $environments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
    {
149
        return $this;
150
    }
151
152
153
    /**
154
     * @param array $environments
155
     * @return $this
156
     *
157
     * @deprecated
158
     */
159
    public function setDefaultEnvironments(array $environments)
0 ignored issues
show
Unused Code introduced by
The parameter $environments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
    {
161
        return $this;
162
    }
163
164
    /*******************************************
165
     * CALLBACK
166
     *******************************************/
167
168
    /**
169
     * @return string
170
     */
171
    public function getCallbackUrl(): string
172
    {
173
        try {
174
            if ($this->callbackUrlPath === null) {
175
                return UrlHelper::siteActionUrl(self::DEFAULT_CALLBACK_URL_PATH);
176
            }
177
178
            return UrlHelper::siteUrl($this->callbackUrlPath);
179
        } catch (Exception $e) {
180
            if ($this->callbackUrlPath === null) {
181
                return UrlHelper::actionUrl(self::DEFAULT_CALLBACK_URL_PATH);
182
            }
183
184
            return UrlHelper::url($this->callbackUrlPath);
185
        }
186
    }
187
188
    /**
189
     * @param $callbackUrlPath
190
     * @return $this
191
     * @throws Exception
192
     */
193
    public function setCallbackUrlPath($callbackUrlPath)
194
    {
195
        $callbackUrlPath = trim(
196
            StringHelper::removeLeft(
197
                (string)$callbackUrlPath,
198
                UrlHelper::siteUrl()
199
            ),
200
            ' /'
201
        );
202
203
        $this->callbackUrlPath = empty($callbackUrlPath) ? null : $callbackUrlPath;
204
205
        return $this;
206
    }
207
208
    /**
209
     * @return string|null
210
     */
211
    public function getCallbackUrlPath()
212
    {
213
        return $this->callbackUrlPath;
214
    }
215
216
    /**
217
     * @return array|null
218
     */
219
    public function getCallbackUrlRule()
220
    {
221
        if ($path = $this->callbackUrlPath) {
222
            return [
223
                $path => self::DEFAULT_CALLBACK_ROUTE
224
            ];
225
        }
226
        return null;
227
    }
228
229
230
    /*******************************************
231
     * PROVIDER SETTINGS VIEW (not currently editable)
232
     *******************************************/
233
234
    /**
235
     * @return ViewInterface
236
     * @deprecated
237
     */
238
    public function getProviderEnvironmentView(): ViewInterface
239
    {
240
        return new Template([
241
            'template' => 'patron/_cp/provider/_environment'
242
        ]);
243
    }
244
245
    /**
246
     * @return ViewInterface'
0 ignored issues
show
Documentation introduced by
The doc-type ViewInterface' could not be parsed: Unknown type name "ViewInterface'" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
247
     */
248
    public function getProviderSettingsView(): ViewInterface
249
    {
250
        return new Template([
251
            'template' => 'patron/_cp/provider/_settings'
252
        ]);
253
    }
254
255
    /**
256
     * @return ViewInterface
257
     */
258
    public function getTokenView(): ViewInterface
259
    {
260
        return new Template([
261
            'template' => 'patron/_modal/token'
262
        ]);
263
    }
264
265
266
    /**
267
     * @inheritdoc
268
     */
269
    public function rules()
270
    {
271
        return array_merge(
272
            parent::rules(),
273
            [
274
                [
275
                    [
276
                        'callbackUrlPath'
277
                    ],
278
                    UriValidator::class
279
                ],
280
                [
281
                    [
282
                        'providers'
283
                    ],
284
                    'safe',
285
                    'on' => [
286
                        self::SCENARIO_DEFAULT
287
                    ]
288
                ]
289
            ]
290
        );
291
    }
292
293
    /**
294
     * @inheritdoc
295
     */
296
    public function attributes()
297
    {
298
        return array_merge(
299
            parent::attributes(),
300
            [
301
                'callbackUrlPath',
302
                'providers'
303
            ]
304
        );
305
    }
306
307
    /**
308
     * @inheritdocå
309
     */
310
    public function attributeLabels()
311
    {
312
        return array_merge(
313
            parent::attributeLabels(),
314
            [
315
                'callbackUrlPath' => Craft::t('patron', 'Callback Url Path'),
316
                'providers' => Craft::t('patron', 'Provider Overrides')
317
            ]
318
        );
319
    }
320
}
321