Completed
Push — master ( 3fed4b...dafce2 )
by Nate
16:31 queued 05:53
created

Update::stateValuesFromBody()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 0
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\cp\actions\general;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use craft\helpers\StringHelper;
14
use flipbox\ember\actions\model\ModelCreate;
15
use flipbox\ember\exceptions\ModelNotFoundException;
16
use flipbox\organizations\models\Settings;
17
use flipbox\organizations\models\SiteSettings;
18
use flipbox\organizations\Organizations;
19
use yii\base\BaseObject;
20
use yii\base\Model;
21
22
/**
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 *
26
 * @method array parentNormalizeSiteConfig($config = [])
27
 */
28
class Update extends ModelCreate
29
{
30
    use traits\SiteSettingAttributes {
31
        normalizeSiteConfig as parentNormalizeSiteConfig;
32
    }
33
34
    /**
35
     * @param BaseObject $settings
36
     * @return bool
37
     * @throws ModelNotFoundException
38
     */
39
    protected function ensureSettings(BaseObject $settings): bool
40
    {
41
        if (!$settings instanceof Settings) {
42
            throw new ModelNotFoundException(sprintf(
43
                "Settings must be an instance of '%s', '%s' given.",
44
                Settings::class,
45
                get_class($settings)
46
            ));
47
        }
48
49
        return true;
50
    }
51
52
    /**
53
     * These are the default body params that we're accepting.  You can lock down specific Client attributes this way.
54
     *
55
     * @return array
56
     */
57
    protected function validBodyParams(): array
58
    {
59
        return [
60
            'requireOwner',
61
            'uniqueOwner'
62
        ];
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function statusCodeSuccess(): int
69
    {
70
        return 200;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76
    protected function attributeValuesFromBody(): array
77
    {
78
        $attributes = parent::attributeValuesFromBody();
79
        return $attributes;
80
    }
81
82
    /**
83
     * @param array $config
84
     * @return array
85
     */
86
    protected function normalizeSiteConfig($config = []): array
87
    {
88
        return array_merge(
89
            $this->parentNormalizeSiteConfig($config),
90
            [
91
                'enabledByDefault' => (bool)$config['enabledByDefault'] ?? false
92
            ]
93
        );
94
    }
95
96
    /**
97
     * @param string|array $config
98
     * @return array
99
     */
100
    protected function normalizeStateConfig($config = []): array
101
    {
102
        if (!is_array($config)) {
103
            $config = [
104
                'label' => StringHelper::toTitleCase((string)$config),
105
                'value' => $config
106
            ];
107
        }
108
109
        return [ArrayHelper::getValue($config, 'value') => ArrayHelper::getValue($config, 'label')];
110
    }
111
112
    /**
113
     * @inheritdoc
114
     * @param Settings $model
115
     */
116
    protected function performAction(Model $model): bool
117
    {
118
        return Organizations::getInstance()->getCp()->getSettings()->save($model);
119
    }
120
121
    /**
122
     * @inheritdoc
123
     * @return Settings
124
     */
125
    protected function newModel(array $config = []): Model
126
    {
127
        return Organizations::getInstance()->getSettings();
128
    }
129
130
131
    /*******************************************
132
     * POPULATE
133
     *******************************************/
134
135
    /**
136
     * @inheritdoc
137
     * @param Settings $object
138
     * @return Settings
139
     */
140
    protected function populate(BaseObject $object): BaseObject
141
    {
142
        if (true === $this->ensureSettings($object)) {
143
            parent::populate($object);
144
            $this->populateSiteSettings($object);
145
            $this->populateSiteLayout($object);
146
        }
147
148
        return $object;
149
    }
150
151
    /**
152
     * @param Settings $model
153
     * @return Settings
154
     */
155
    private function populateSiteLayout(Settings $model): Settings
156
    {
157
        if ($fieldLayout = Craft::$app->getFields()->assembleLayoutFromPost()) {
158
            $model->setFieldLayout($fieldLayout);
159
        }
160
161
        return $model;
162
    }
163
164
    /**
165
     * @param Settings $model
166
     * @return Settings
167
     */
168
    private function populateSiteSettings(Settings $model): Settings
169
    {
170
        if (null !== ($sites = $this->sitesSettingsFromBody())) {
171
            $enabledSites = [];
172
173
            foreach ($sites as $siteId => $siteConfig) {
174
                if (!($siteConfig['enabled'] ?? false)) {
175
                    continue;
176
                }
177
178
                $siteConfig['siteId'] = $siteId;
179
                $siteConfig['class'] = SiteSettings::class;
180
                $enabledSites[$siteId] = $siteConfig;
181
            }
182
183
            $model->setSiteSettings($enabledSites);
184
        }
185
        return $model;
186
    }
187
}
188