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

ModelDelete   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 85
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
getRecordById() 0 1 ?
B delete() 0 59 5
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 flipbox\spark\helpers\RecordHelper;
12
use flipbox\spark\models\Model;
13
use flipbox\spark\records\Record;
14
use yii\base\ModelEvent;
15
16
/**
17
 * @package flipbox\spark\services\traits
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
trait ModelDelete
22
{
23
24
    /*******************************************
25
     * ABSTRACTS
26
     *******************************************/
27
28
    /**
29
     * @param int $id
30
     * @param string|null $toScenario
31
     * @return Record
32
     */
33
    abstract public function getRecordById(int $id, string $toScenario = null): Record;
34
35
36
    /*******************************************
37
     * DELETE
38
     *******************************************/
39
40
    /**
41
     * @param Model $model
42
     * @return bool
43
     * @throws \Exception
44
     */
45
    public function delete(Model $model): bool
46
    {
47
48
        // The event to trigger
49
        $event = new ModelEvent();
50
51
        // Db transaction
52
        $transaction = RecordHelper::beginTransaction();
53
54
        try {
55
56
            // The 'before' event
57
            if (!$model->beforeDelete($event)) {
58
59
                $transaction->rollBack();
60
61
                return false;
62
            }
63
64
            // Get record
65
            $record = $this->getRecordById($model->id);
66
67
            // Insert record
68
            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...
69
70
                // Transfer errors to model
71
                $model->addErrors($record->getErrors());
72
73
                // Roll back db transaction
74
                $transaction->rollBack();
75
76
                return false;
77
78
            }
79
80
            // The 'after' event
81
            if (!$model->afterDelete($event)) {
82
83
                // Roll back db transaction
84
                $transaction->rollBack();
85
86
                return false;
87
88
            }
89
90
        } catch (\Exception $e) {
91
92
            // Roll back all db actions (fail)
93
            $transaction->rollback();
94
95
            throw $e;
96
97
        }
98
99
        $transaction->commit();
100
101
        return true;
102
103
    }
104
105
}
106