Completed
Push — master ( c8a423...a23ea0 )
by Song
02:21
created

HasSelector::selector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Concerns;
4
5
use Encore\Admin\Grid;
6
use Encore\Admin\Grid\Tools\Selector;
7
8
/**
9
 * @mixin Grid
10
 */
11
trait HasSelector
12
{
13
    /**
14
     * @var Selector
15
     */
16
    protected $selector;
17
18
    /**
19
     * @param \Closure $closure
20
     *
21
     * @return $this
22
     */
23
    public function selector(\Closure $closure)
24
    {
25
        $this->selector = new Selector();
26
27
        call_user_func($closure, $this->selector);
28
29
        $this->header(function () {
0 ignored issues
show
Bug introduced by
It seems like header() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
30
            return $this->renderSelector();
31
        });
32
33
        return $this;
34
    }
35
36
    /**
37
     * Apply selector query to grid model query.
38
     *
39
     * @return $this
40
     */
41
    protected function applySelectorQuery()
42
    {
43
        if (is_null($this->selector)) {
44
            return $this;
45
        }
46
47
        $active = Selector::parseSelected();
48
49
        $this->selector->getSelectors()->each(function ($selector, $column) use ($active) {
50
            if (!array_key_exists($column, $active)) {
51
                return;
52
            }
53
54
            $values = $active[$column];
55
56
            if ($selector['type'] == 'one') {
57
                $values = current($values);
58
            }
59
60
            if (is_null($selector['query'])) {
61
                $this->model()->whereIn($column, $values);
0 ignored issues
show
Bug introduced by
It seems like model() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
            } else {
63
                call_user_func($selector['query'], $this->model(), $values);
0 ignored issues
show
Bug introduced by
It seems like model() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
64
            }
65
        });
66
67
        return $this;
68
    }
69
70
    /**
71
     * Render grid selector.
72
     *
73
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|string
74
     */
75
    public function renderSelector()
76
    {
77
        return $this->selector->render();
78
    }
79
}