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

CascadeDelete::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
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