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

CascadeRestore::handle()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
nc 6
nop 0
dl 0
loc 24
rs 8.5125
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 CascadeRestore
8
 *
9
 * @link   http://pyrocms.com/
10
 * @author PyroCMS, Inc. <[email protected]>
11
 * @author Ryan Thompson <[email protected]>
12
 */
13
class CascadeRestore
14
{
15
16
    /**
17
     * The eloquent model.
18
     *
19
     * @var EloquentModel
20
     */
21
    protected $model;
22
23
    /**
24
     * Create a new CascadeRestore 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
        foreach ($this->model->getCascades() as $relation => $actions) {
39
            if (in_array('restore', $actions)) {
40
41
                $relation = $this->model
42
                    ->{camel_case($relation)}()
43
                    ->onlyTrashed()
44
                    ->getResults();
45
46
                if ($relation instanceof EloquentModel) {
47
                    $relation->restore();
0 ignored issues
show
Documentation Bug introduced by
The method restore does not exist on object<Anomaly\Streams\P...rm\Model\EloquentModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
48
                }
49
50
                if ($relation instanceof EloquentCollection) {
51
                    $relation->each(
52
                        function (EloquentModel $item) {
53
                            $item->restore();
0 ignored issues
show
Documentation Bug introduced by
The method restore does not exist on object<Anomaly\Streams\P...rm\Model\EloquentModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
54
                        }
55
                    );
56
                }
57
            }
58
        }
59
    }
60
}
61