Completed
Push — master ( 19b2ae...0e0de4 )
by Nate
04:43
created

UpdateSettings::populate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
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\cp\actions\settings;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use flipbox\craft\ember\actions\models\CreateModel;
14
use flipbox\patron\models\Settings;
15
use flipbox\patron\Patron;
16
use yii\base\Model;
17
use yii\web\NotFoundHttpException;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 *
23
 * @method array parentNormalizeSiteConfig($config = [])
24
 */
25
class UpdateSettings extends CreateModel
26
{
27
    public $validBodyParams = [
28
        'callbackUrlPath',
29
        'encryptStorageData',
30
        'autoPopulateTokenEnvironments',
31
        'applyProviderEnvironmentsToToken'
32
    ];
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public $statusCodeSuccess = 200;
38
39
    /**
40
     * @inheritdoc
41
     * @param Settings $model
42
     * @return Settings
43
     */
44
    protected function populate(Model $model): Model
45
    {
46
        $model->setEnvironments(
47
            $this->environmentValuesFromBody()
48
        );
49
50
        return parent::populate($model);
51
    }
52
53
    /**
54
     * Normalize settings from body
55
     *
56
     * @return array
57
     */
58
    protected function environmentValuesFromBody(): array
59
    {
60
        $environmentArray = [];
61
        if ($rawEnvironments = Craft::$app->getRequest()->getBodyParam('environments', [])) {
62
            foreach (ArrayHelper::toArray($rawEnvironments) as $rawEnvironment) {
63
                $environmentArray = array_merge(
64
                    $environmentArray,
65
                    $this->normalizeEnvironmentValue($rawEnvironment)
66
                );
67
            }
68
        }
69
        return array_values($environmentArray);
70
    }
71
72
    /**
73
     * @param string|array $value
74
     * @return array
75
     */
76
    protected function normalizeEnvironmentValue($value = []): array
77
    {
78
        if (is_array($value)) {
79
            $value = ArrayHelper::getValue($value, 'value');
80
        }
81
82
        return [$value => $value];
83
    }
84
85
    /**
86
     * @param Model $model
87
     * @return bool
88
     * @throws \Throwable
89
     */
90
    protected function performAction(Model $model): bool
91
    {
92
        if (!$model instanceof Settings) {
93
            throw new NotFoundHttpException(sprintf(
94
                "Settings must be an instance of '%s', '%s' given.",
95
                Settings::class,
96
                get_class($model)
97
            ));
98
        }
99
100
        return Patron::getInstance()->getCp()->getSettings()->save($model);
101
    }
102
103
    /**
104
     * @inheritdoc
105
     * @return Settings
106
     */
107
    protected function newModel(array $config = []): Model
108
    {
109
        return clone Patron::getInstance()->getSettings();
110
    }
111
}
112