Passed
Push — master ( e046f3...04364f )
by Nate
02:43
created

ModelSave::createEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 3
nop 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/spark/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/spark
7
 */
8
9
namespace flipbox\spark\services\traits;
10
11
use Craft;
12
use craft\events\ModelEvent;
13
use flipbox\spark\helpers\RecordHelper;
14
use flipbox\spark\models\Model;
15
use flipbox\spark\records\Record;
16
17
/**
18
 * @package flipbox\spark\services\traits
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
trait ModelSave
23
{
24
25
    /*******************************************
26
     * ABSTRACTS
27
     *******************************************/
28
29
    /**
30
     * @param Model $model
31
     * @param bool $mirrorScenario
32
     * @return Record
33
     */
34
    abstract public function toRecord(Model $model, bool $mirrorScenario = true): Record;
35
36
37
    /*******************************************
38
     * SAVE
39
     *******************************************/
40
41
    /**
42
     * @param Model $model
43
     * @param bool $runValidation
44
     * @param null $attributes
45
     * @param bool $mirrorScenario
46
     * @return bool
47
     * @throws \Exception
48
     */
49
    public function save(Model $model, bool $runValidation = true, $attributes = null, bool $mirrorScenario = true)
50
    {
51
52
        // Validate
53
        if ($runValidation && !$model->validate($attributes)) {
54
            Craft::info('Model not saved due to validation error.', __METHOD__);
55
            return false;
56
        }
57
58
        // Create event
59
        $event = new ModelEvent([
60
            'isNew' => null !== $model->id
61
        ]);
62
63
        // Db transaction
64
        $transaction = RecordHelper::beginTransaction();
65
66
        try {
67
68
            // The 'before' event
69
            if (!$model->beforeSave($event)) {
70
71
                $transaction->rollBack();
72
73
                return false;
74
            }
75
76
            $record = $this->toRecord($model, $mirrorScenario);
77
78
            // Validate
79
            if (!$record->validate($attributes)) {
80
81
                $model->addErrors($record->getErrors());
82
83
                $transaction->rollBack();
84
85
                return false;
86
87
            }
88
89
            // Insert record
90
            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...
91
92
                // Transfer errors to model
93
                $model->addErrors($record->getErrors());
94
95
                $transaction->rollBack();
96
97
                return false;
98
99
            }
100
101
            // Transfer record to model
102
            if ($event->isNew) {
103
                $model->id = $record->id;
104
                $model->dateCreated = $record->dateCreated;
105
                $model->uid = $record->uid;
106
            }
107
            $model->dateUpdated = $record->dateUpdated;
108
109
110
            // The 'after' event
111
            if (!$model->afterSave($event)) {
112
113
                $transaction->rollBack();
114
115
                return false;
116
117
            }
118
119
        } catch (\Exception $e) {
120
121
            $transaction->rollBack();
122
123
            throw $e;
124
125
        }
126
127
        $transaction->commit();
128
129
        return true;
130
131
    }
132
133
}
134