CreateModel   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 53
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
newModel() 0 1 ?
A run() 0 4 1
A validBodyParams() 0 4 1
A populate() 0 9 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\actions\models;
10
11
use yii\base\Action;
12
use yii\base\Model;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 2.0.0
17
 */
18
abstract class CreateModel extends Action
19
{
20
    use SaveModelTrait;
21
22
    /**
23
     * @var array
24
     */
25
    public $validBodyParams = [];
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public $statusCodeSuccess = 201;
31
32
    /**
33
     * @param array $config
34
     * @return Model
35
     */
36
    abstract protected function newModel(array $config = []): Model;
37
38
    /**
39
     * @return Model|null
40
     * @throws \yii\web\UnauthorizedHttpException
41
     */
42
    public function run()
43
    {
44
        return $this->runInternal($this->newModel());
45
    }
46
47
    /**
48
     * Body params that should be set on the record.
49
     *
50
     * @return array
51
     */
52
    protected function validBodyParams(): array
53
    {
54
        return $this->validBodyParams;
55
    }
56
57
    /**
58
     * @param Model $model
59
     * @return Model
60
     */
61
    protected function populate(Model $model): Model
62
    {
63
        // Valid attribute values
64
        $model->setAttributes(
65
            $this->attributeValuesFromBody()
66
        );
67
68
        return $model;
69
    }
70
}
71