Passed
Push — master ( 36ec95...f2dea1 )
by Nate
07:31
created

ModelDelete::afterDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
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 flipbox\spark\helpers\RecordHelper;
13
use flipbox\spark\models\Model;
14
use flipbox\spark\records\Record;
15
use yii\base\ModelEvent;
16
17
/**
18
 * @package flipbox\spark\services\traits
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
trait ModelDelete
23
{
24
25
    /*******************************************
26
     * ABSTRACTS
27
     *******************************************/
28
29
    /**
30
     * @param int $id
31
     * @param string|null $toScenario
32
     * @return Record
33
     */
34
    abstract public function getRecordById(int $id, string $toScenario = null): Record;
35
36
37
    /*******************************************
38
     * DELETE
39
     *******************************************/
40
41
    /**
42
     * @param Model $model
43
     * @return bool
44
     * @throws \Exception
45
     */
46
    public function delete(Model $model): bool
47
    {
48
49
        // a 'beforeSave' event
50
        if(!$this->beforeDelete($model)) {
51
            return false;
52
        }
53
54
        // The event to trigger
55
        $event = new ModelEvent();
56
57
        // Db transaction
58
        $transaction = RecordHelper::beginTransaction();
59
60
        try {
61
62
            // The 'before' event
63
            if (!$model->beforeDelete($event)) {
64
65
                $transaction->rollBack();
66
67
                return false;
68
            }
69
70
            // Get record
71
            $record = $this->getRecordById($model->id);
72
73
            // Insert record
74
            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...
75
76
                // Transfer errors to model
77
                $model->addErrors($record->getErrors());
78
79
                // Roll back db transaction
80
                $transaction->rollBack();
81
82
                return false;
83
84
            }
85
86
            // The 'after' event
87
            if (!$model->afterDelete($event)) {
88
89
                // Roll back db transaction
90
                $transaction->rollBack();
91
92
                return false;
93
94
            }
95
96
        } catch (\Exception $e) {
97
98
            // Roll back all db actions (fail)
99
            $transaction->rollback();
100
101
            throw $e;
102
103
        }
104
105
        $transaction->commit();
106
107
        // an 'afterDelete' event
108
        $this->afterDelete($model);
109
110
        return true;
111
112
    }
113
114
    /**
115
     * @param Model $model
116
     * @return bool
117
     */
118
    protected function beforeDelete(Model $model): bool
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...
119
    {
120
        return true;
121
    }
122
123
    /**
124
     * @param Model $model
125
     */
126
    protected function afterDelete(Model $model)
127
    {
128
129
        Craft::info(sprintf(
130
            "Model '%s' with ID '%s' was deleted successfully.",
131
            (string) get_class($model),
132
            (string) $model->id
133
        ), __METHOD__);
134
135
    }
136
137
}
138