Passed
Push — master ( e00e24...e046f3 )
by Nate
06:00 queued 02:00
created

ModelDelete::createEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 1
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 flipbox\spark\helpers\RecordHelper;
15
use flipbox\spark\models\Model;
16
use flipbox\spark\records\Record;
17
use yii\base\ModelEvent;
18
19
trait ModelDelete
20
{
21
22
    /*******************************************
23
     * ABSTRACTS
24
     *******************************************/
25
26
    /**
27
     * @param int $id
28
     * @param string|null $toScenario
29
     * @return Record
30
     */
31
    abstract public function getRecordById(int $id, string $toScenario = null): Record;
32
33
34
    /*******************************************
35
     * EVENT
36
     *******************************************/
37
38
    /**
39
     * @param Model $model
40
     * @return ModelEvent
41
     */
42
    protected function createEvent(Model $model): ModelEvent
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    {
44
        return new ModelEvent();
45
    }
46
47
    /*******************************************
48
     * DELETE
49
     *******************************************/
50
51
    /**
52
     * @param Model $model
53
     * @return bool
54
     * @throws \Exception
55
     */
56
    public function delete(Model $model): bool
57
    {
58
59
        // The event to trigger
60
        $event = $this->createEvent();
0 ignored issues
show
Bug introduced by
The call to createEvent() misses a required argument $model.

This check looks for function calls that miss required arguments.

Loading history...
61
62
        // Db transaction
63
        $transaction = RecordHelper::beginTransaction();
64
65
        try {
66
67
            // The 'before' event
68
            if (!$model->beforeDelete($event)) {
69
70
                $transaction->rollBack();
71
72
                return false;
73
            }
74
75
            // Get record
76
            $record = $this->getRecordById($model->id);
77
78
            // Insert record
79
            if (!$record->delete()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $record->delete() of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
80
81
                // Transfer errors to model
82
                $model->addErrors($record->getErrors());
83
84
                // Roll back db transaction
85
                $transaction->rollBack();
86
87
                return false;
88
89
            }
90
91
            // The 'after' event
92
            if (!$model->afterDelete($event)) {
93
94
                // Roll back db transaction
95
                $transaction->rollBack();
96
97
                return false;
98
99
            }
100
101
        } catch (\Exception $e) {
102
103
            // Roll back all db actions (fail)
104
            $transaction->rollback();
105
106
            throw $e;
107
108
        }
109
110
        $transaction->commit();
111
112
        return true;
113
114
    }
115
116
}
117