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

Update   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 5
dl 0
loc 121
ccs 0
cts 62
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A ensureSettings() 0 12 2
A validBodyParams() 0 6 1
A statusCodeSuccess() 0 4 1
A attributeValuesFromBody() 0 6 1
A environmentValuesFromBody() 0 13 3
A normalizeEnvironmentValue() 0 8 2
A performAction() 0 4 1
A newModel() 0 4 1
A populate() 0 8 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\ember\actions\model\ModelCreate;
14
use flipbox\ember\exceptions\ModelNotFoundException;
15
use flipbox\patron\models\Settings;
16
use flipbox\patron\Patron;
17
use yii\base\BaseObject;
18
use yii\base\Model;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 *
24
 * @method array parentNormalizeSiteConfig($config = [])
25
 */
26
class Update extends ModelCreate
27
{
28
    /**
29
     * @param BaseObject $settings
30
     * @return bool
31
     * @throws ModelNotFoundException
32
     */
33
    protected function ensureSettings(BaseObject $settings): bool
34
    {
35
        if (!$settings instanceof Settings) {
36
            throw new ModelNotFoundException(sprintf(
37
                "Settings must be an instance of '%s', '%s' given.",
38
                Settings::class,
39
                get_class($settings)
40
            ));
41
        }
42
43
        return true;
44
    }
45
46
    /**
47
     * These are the default body params that we're accepting.  You can lock down specific Client attributes this way.
48
     *
49
     * @return array
50
     */
51
    protected function validBodyParams(): array
52
    {
53
        return [
54
            'callbackUrlPath'
55
        ];
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function statusCodeSuccess(): int
62
    {
63
        return 200;
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    protected function attributeValuesFromBody(): array
70
    {
71
        $attributes = parent::attributeValuesFromBody();
72
        $attributes['environments'] = $this->environmentValuesFromBody();
73
        return $attributes;
74
    }
75
76
    /**
77
     * Normalize settings from body
78
     *
79
     * @return array
80
     */
81
    protected function environmentValuesFromBody(): array
82
    {
83
        $environmentArray = [];
84
        if ($rawEnvironments = Craft::$app->getRequest()->getBodyParam('environments', [])) {
85
            foreach (ArrayHelper::toArray($rawEnvironments) as $rawEnvironment) {
86
                $environmentArray = array_merge(
87
                    $environmentArray,
88
                    $this->normalizeEnvironmentValue($rawEnvironment)
89
                );
90
            }
91
        }
92
        return array_values($environmentArray);
93
    }
94
95
96
    /**
97
     * @param string|array $value
98
     * @return array
99
     */
100
    protected function normalizeEnvironmentValue($value = []): array
101
    {
102
        if (is_array($value)) {
103
            $value = ArrayHelper::getValue($value, 'value');
104
        }
105
106
        return [$value => $value];
107
    }
108
109
    /**
110
     * @param Model $model
111
     * @return bool
112
     * @throws \Throwable
113
     */
114
    protected function performAction(Model $model): bool
115
    {
116
        return Patron::getInstance()->getCp()->getSettings()->save($model);
117
    }
118
119
    /**
120
     * @inheritdoc
121
     * @return Settings
122
     */
123
    protected function newModel(array $config = []): Model
124
    {
125
        return Patron::getInstance()->getSettings();
126
    }
127
128
129
    /*******************************************
130
     * POPULATE
131
     *******************************************/
132
133
    /**
134
     * @param BaseObject $object
135
     * @return BaseObject
136
     * @throws ModelNotFoundException
137
     */
138
    protected function populate(BaseObject $object): BaseObject
139
    {
140
        if (true === $this->ensureSettings($object)) {
141
            parent::populate($object);
0 ignored issues
show
Compatibility introduced by
$object of type object<yii\base\BaseObject> is not a sub-type of object<yii\base\Model>. It seems like you assume a child class of the class yii\base\BaseObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
142
        }
143
144
        return $object;
145
    }
146
}
147