Completed
Pull Request — master (#5)
by
unknown
02:09
created

CascadeSoftDeletes::getClassTraits()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 2
nop 2
crap 3
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 1
                    get_called_class()
25 1
                ));
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 3
                ));
34
            }
35
36 2
            foreach ($model->getCascadingDeletes() as $relationship) {
37 2
                $model->{$relationship}()->delete();
38 2
            }
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', $this->getClassTraits($this));
51
    }
52
    
53
54
    /**
55
     * Get all traits used by class, including those inherited from ancestor classes.
56
     *
57
     * @param      $class
58
     * @param bool $autoload
59
     *
60
     * @return array
61
     */
62 6
    protected function getClassTraits($class, $autoload = true) {
63 6
        $traits = [];
64
        do {
65 6
            $traits = array_merge(class_uses($class, $autoload), $traits);
66 6
        } while($class = get_parent_class($class));
67 6
        foreach ($traits as $trait => $same) {
68 6
            $traits = array_merge(class_uses($trait, $autoload), $traits);
69 6
        }
70 6
        return array_unique($traits);
71
    }
72
73
74
    /**
75
     * Determine if the current model has any invalid cascading relationships defined.
76
     *
77
     * A relationship is considered invalid when the method does not exist, or the relationship
78
     * method does not return an instance of Illuminate\Database\Eloquent\Relations\Relation.
79
     *
80
     * @return array
81
     */
82
    protected function hasInvalidCascadingRelationships()
83
    {
84 5
        return array_filter($this->getCascadingDeletes(), function ($relationship) {
85 5
            return ! method_exists($this, $relationship) || ! $this->{$relationship}() instanceof Relation;
86 5
        });
87
    }
88
89
90
    /**
91
     * Fetch the defined cascading soft deletes for this model.
92
     *
93
     * @return array
94
     */
95 5
    protected function getCascadingDeletes()
96
    {
97 5
        return isset($this->cascadeDeletes) ? (array) $this->cascadeDeletes : [];
98
    }
99
}
100