Completed
Push — master ( 11971b...eeafb3 )
by Nate
01:16
created

CreateRecord   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 63
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
newRecord() 0 1 ?
A validBodyParams() 0 4 1
A populate() 0 8 1
A run() 0 6 1
A performAction() 0 4 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\records;
10
11
use yii\base\Action;
12
use yii\db\ActiveRecord;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 2.0.0
17
 */
18
abstract class CreateRecord extends Action
19
{
20
    use SaveRecordTrait;
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 ActiveRecord
35
     */
36
    abstract protected function newRecord(array $config = []): ActiveRecord;
37
38
    /**
39
     * Body params that should be set on the record.
40
     *
41
     * @return array
42
     */
43
    protected function validBodyParams(): array
44
    {
45
        return $this->validBodyParams;
46
    }
47
48
    /**
49
     * @param ActiveRecord $record
50
     * @return ActiveRecord
51
     */
52
    protected function populate(ActiveRecord $record): ActiveRecord
53
    {
54
        $record->setAttributes(
55
            $this->attributeValuesFromBody()
56
        );
57
58
        return $record;
59
    }
60
61
    /**
62
     * @return ActiveRecord
63
     * @throws \yii\web\HttpException
64
     */
65
    public function run()
66
    {
67
        return $this->runInternal(
68
            $this->newRecord()
69
        );
70
    }
71
72
    /**
73
     * @inheritdoc
74
     * @param ActiveRecord $record
75
     */
76
    protected function performAction(ActiveRecord $record): bool
77
    {
78
        return $record->save();
79
    }
80
}
81