ModelDelete::getRecordByModel()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, flipbox\spark\services\traits\Model.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use flipbox\spark\records\Record;
15
use yii\base\ModelEvent;
16
17
/**
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 Model $model
30
     * @return Record
31
     */
32
    abstract public function getRecordByModel(Model $model): Record;
33
34
35
    /*******************************************
36
     * DELETE
37
     *******************************************/
38
39
    /**
40
     * @param Model $model
41
     * @return bool
42
     * @throws \Exception
43
     */
44
    public function delete(Model $model): bool
45
    {
46
47
        // a 'beforeSave' event
48
        if (!$this->beforeDelete($model)) {
49
            return false;
50
        }
51
52
        // The event to trigger
53
        $event = new ModelEvent();
54
55
        // Db transaction
56
        $transaction = RecordHelper::beginTransaction();
57
58
        try {
59
            // The 'before' event
60
            if (!$model->beforeDelete($event)) {
61
                $transaction->rollBack();
62
63
                return false;
64
            }
65
66
            // Get record
67
            /** @var Record $record */
68
            $record = $this->getRecordByModel($model);
69
70
            // Insert record
71
            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...
72
                // Transfer errors to model
73
                $model->addErrors($record->getErrors());
74
75
                // Roll back db transaction
76
                $transaction->rollBack();
77
78
                return false;
79
            }
80
81
            // The 'after' event
82
            if (!$model->afterDelete($event)) {
83
                // Roll back db transaction
84
                $transaction->rollBack();
85
86
                return false;
87
            }
88
        } catch (\Exception $e) {
89
            // Roll back all db actions (fail)
90
            $transaction->rollback();
91
92
            throw $e;
93
        }
94
95
        $transaction->commit();
96
97
        // an 'afterDelete' event
98
        $this->afterDelete($model);
99
100
        return true;
101
    }
102
103
    /**
104
     * @param Model $model
105
     * @return bool
106
     */
107
    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...
108
    {
109
        return true;
110
    }
111
112
    /**
113
     * @param Model $model
114
     */
115
    protected function afterDelete(Model $model)
116
    {
117
118
        Craft::info(sprintf(
119
            "Model '%s' was deleted successfully.",
120
            (string)get_class($model)
121
        ), __METHOD__);
122
    }
123
}
124