Completed
Push — master ( 85961d...dff2dd )
by Michael
10s
created

CascadeSoftDeletes::getCascadingDeletes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Iatstuti\Database\Support;
4
5
use Illuminate\Database\Eloquent\Relations\Relation;
6
use LogicException;
7
8
trait CascadeSoftDeletes
9
{
10
    /**
11
     * Boot the trait.
12
     *
13
     * Listen for the deleting event of a soft deleting model, and run
14
     * the delete operation for any configured relationship methods.
15
     *
16
     * @throws \LogicException
17
     */
18 6
    protected static function bootCascadeSoftDeletes()
19
    {
20
        static::deleting(function ($model) {
21 6
            if (! $model->implementsSoftDeletes()) {
22 1
                throw new LogicException(sprintf(
23 1
                    '%s does not implement Illuminate\Database\Eloquent\SoftDeletes',
24
                    get_called_class()
25
                ));
26
            }
27
28 5
            if ($invalidCascadingRelationships = $model->hasInvalidCascadingRelationships()) {
29 3
                throw new LogicException(sprintf(
30 3
                    '%s [%s] must exist and return an object of type Illuminate\Database\Eloquent\Relations\Relation',
31 3
                    str_plural('Relationship', count($invalidCascadingRelationships)),
32 3
                    join(', ', $invalidCascadingRelationships)
33
                ));
34
            }
35
36 2
            foreach ($model->getCascadingDeletes() as $relationship) {
37 2
                $model->{$relationship}()->delete();
38
            }
39 5
        });
40 5
    }
41
42
43
    /**
44
     * Determine if the current model implements soft deletes.
45
     *
46
     * @return bool
47
     */
48 6
    protected function implementsSoftDeletes()
49
    {
50 6
        return in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this));
51
    }
52
53
54
    /**
55
     * Determine if the current model has any invalid cascading relationships defined.
56
     *
57
     * A relationship is considered invalid when the method does not exist, or the relationship
58
     * method does not return an instance of Illuminate\Database\Eloquent\Relations\Relation.
59
     *
60
     * @return array
61
     */
62
    protected function hasInvalidCascadingRelationships()
63
    {
64 5
        return array_filter($this->getCascadingDeletes(), function ($relationship) {
65 5
            return ! method_exists($this, $relationship) || ! $this->{$relationship}() instanceof Relation;
66 5
        });
67
    }
68
69
70
    /**
71
     * Fetch the defined cascading soft deletes for this model.
72
     *
73
     * @return array
74
     */
75 5
    protected function getCascadingDeletes()
76
    {
77 5
        return isset($this->cascadeDeletes) ? (array) $this->cascadeDeletes : [];
78
    }
79
}
80