Passed
Push — master ( 5a9c7e...e00e24 )
by Nate
04:01 queued 01:22
created

ModelInsertTrait::save()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 75
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 6.5862
c 0
b 0
f 0
cc 7
eloc 29
nc 20
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @package    Spark
5
 * @author     Flipbox Factory <[email protected]>
6
 * @copyright  2010-2016 Flipbox Digital Limited
7
 * @license    https://github.com/FlipboxFactory/Craft3-Spark/blob/master/LICENSE
8
 * @link       https://github.com/FlipboxFactory/Craft3-Spark
9
 * @since      Class available since Release 1.0.0
10
 */
11
12
namespace flipbox\spark\services\traits;
13
14
use craft\events\ModelEvent;
15
use flipbox\spark\helpers\RecordHelper;
16
use flipbox\spark\models\Model;
17
use flipbox\spark\records\Record;
18
19
trait ModelInsertTrait
20
{
21
22
    /*******************************************
23
     * ABSTRACTS
24
     *******************************************/
25
26
    /**
27
     * @param Model $model
28
     * @param bool $mirrorScenario
29
     * @return Record
30
     */
31
    abstract public function toRecord(Model $model, bool $mirrorScenario = true): Record;
32
33
34
    /*******************************************
35
     * EVENT
36
     *******************************************/
37
38
    /**
39
     * @param Model $model
40
     * @return ModelEvent
41
     */
42
    protected function createEvent(Model $model): ModelEvent
43
    {
44
        return new ModelEvent([
45
            'isNew' => null !== $model->id
46
        ]);
47
    }
48
49
    /*******************************************
50
     * SAVE
51
     *******************************************/
52
53
    /**
54
     * @param Model $model
55
     * @param null $attributes
56
     * @param bool $mirrorScenario
57
     * @return bool
58
     * @throws \Exception
59
     */
60
    public function save(Model $model, $attributes = null, bool $mirrorScenario = true)
61
    {
62
63
        // Create event
64
        $event = $this->createEvent($model);
65
66
        // Db transaction
67
        $transaction = RecordHelper::beginTransaction();
68
69
        try {
70
71
            // The 'before' event
72
            if (!$model->beforeSave($event)) {
73
74
                $transaction->rollBack();
75
76
                return false;
77
            }
78
79
            $record = $this->toRecord($model, $mirrorScenario);
80
81
            // Validate
82
            if (!$record->validate($attributes)) {
83
84
                $model->addErrors($record->getErrors());
85
86
                $transaction->rollBack();
87
88
                return false;
89
90
            }
91
92
            // Insert record
93
            if (!$record->save($attributes)) {
0 ignored issues
show
Documentation introduced by
$attributes is of type null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
95
                // Transfer errors to model
96
                $model->addErrors($record->getErrors());
97
98
                $transaction->rollBack();
99
100
                return false;
101
102
            }
103
104
            // Transfer record to model
105
            if ($event->isNew) {
106
                $model->id = $record->id;
107
                $model->dateCreated = $record->dateCreated;
108
                $model->uid = $record->uid;
109
            }
110
            $model->dateUpdated = $record->dateUpdated;
111
112
113
            // The 'after' event
114
            if (!$model->afterSave($event)) {
115
116
                $transaction->rollBack();
117
118
                return false;
119
120
            }
121
122
        } catch (\Exception $e) {
123
124
            $transaction->rollBack();
125
126
            throw $e;
127
128
        }
129
130
        $transaction->commit();
131
132
        return true;
133
134
    }
135
136
}
137