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
|
|
|
* These are the default body params that we're accepting. You can lock down specific Client attributes this way. |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
protected function validBodyParams(): array |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
'callbackUrlPath' |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function statusCodeSuccess(): int |
44
|
|
|
{ |
45
|
|
|
return 200; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
protected function attributeValuesFromBody(): array |
52
|
|
|
{ |
53
|
|
|
$attributes = parent::attributeValuesFromBody(); |
54
|
|
|
$attributes['environments'] = $this->environmentValuesFromBody(); |
55
|
|
|
return $attributes; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Normalize settings from body |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
protected function environmentValuesFromBody(): array |
64
|
|
|
{ |
65
|
|
|
$environmentArray = []; |
66
|
|
|
if ($rawEnvironments = Craft::$app->getRequest()->getBodyParam('environments', [])) { |
67
|
|
|
foreach (ArrayHelper::toArray($rawEnvironments) as $rawEnvironment) { |
68
|
|
|
$environmentArray = array_merge( |
69
|
|
|
$environmentArray, |
70
|
|
|
$this->normalizeEnvironmentValue($rawEnvironment) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
return array_values($environmentArray); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string|array $value |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
protected function normalizeEnvironmentValue($value = []): array |
83
|
|
|
{ |
84
|
|
|
if (is_array($value)) { |
85
|
|
|
$value = ArrayHelper::getValue($value, 'value'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return [$value => $value]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param Model $model |
93
|
|
|
* @return bool |
94
|
|
|
* @throws \Throwable |
95
|
|
|
*/ |
96
|
|
|
protected function performAction(Model $model): bool |
97
|
|
|
{ |
98
|
|
|
if (!$model instanceof Settings) { |
99
|
|
|
throw new ModelNotFoundException(sprintf( |
100
|
|
|
"Settings must be an instance of '%s', '%s' given.", |
101
|
|
|
Settings::class, |
102
|
|
|
get_class($model) |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return Patron::getInstance()->getCp()->getSettings()->save($model); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritdoc |
111
|
|
|
* @return Settings |
112
|
|
|
*/ |
113
|
|
|
protected function newModel(array $config = []): Model |
114
|
|
|
{ |
115
|
|
|
return Patron::getInstance()->getSettings(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|