Completed
Push — master ( 38efb8...5cc558 )
by Ryan
05:49
created

CascadeDelete   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 47
rs 10
c 1
b 0
f 0
wmc 7
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B handle() 0 23 6
1
<?php namespace Anomaly\Streams\Platform\Model\Command;
2
3
use Anomaly\Streams\Platform\Model\EloquentCollection;
4
use Anomaly\Streams\Platform\Model\EloquentModel;
5
6
/**
7
 * Class CascadeDelete
8
 *
9
 * @link   http://pyrocms.com/
10
 * @author PyroCMS, Inc. <[email protected]>
11
 * @author Ryan Thompson <[email protected]>
12
 */
13
class CascadeDelete
14
{
15
16
    /**
17
     * The eloquent model.
18
     *
19
     * @var EloquentModel
20
     */
21
    protected $model;
22
23
    /**
24
     * Create a new CascadeDelete instance.
25
     *
26
     * @param EloquentModel $model
27
     */
28
    public function __construct(EloquentModel $model)
29
    {
30
        $this->model = $model;
31
    }
32
33
    /**
34
     * Handle the command.
35
     */
36
    public function handle()
37
    {
38
        $action = $this->model->isForceDeleting() ? 'forceDelete' : 'delete';
39
40
        foreach ($this->model->getCascades() as $relation => $actions) {
41
            if (in_array($action, $actions)) {
42
43
                $relation = $this->model->{$relation};
44
45
                if ($relation instanceof EloquentModel) {
46
                    $relation->{$action}();
47
                }
48
49
                if ($relation instanceof EloquentCollection) {
50
                    $relation->each(
51
                        function (EloquentModel $item) use ($action) {
52
                            $item->{$action}();
53
                        }
54
                    );
55
                }
56
            }
57
        }
58
    }
59
}
60