Completed
Push — master ( 5cbd8c...10b8c5 )
by Song
02:23
created

HasActions::batchActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Concerns;
4
5
use Closure;
6
use Encore\Admin\Grid;
7
8
trait HasActions
9
{
10
    /**
11
     * Callback for grid actions.
12
     *
13
     * @var Closure
14
     */
15
    protected $actionsCallback;
16
17
    /**
18
     * Actions column display class.
19
     *
20
     * @var string
21
     */
22
    protected $actionsClass;
23
24
    /**
25
     * Set grid action callback.
26
     *
27
     * @param Closure|string $actions
28
     *
29
     * @return $this
30
     */
31
    public function actions($actions)
32
    {
33
        if ($actions instanceof Closure) {
34
            $this->actionsCallback = $actions;
35
        }
36
37
        return $this;
38
    }
39
40
    /**
41
     * Get action display class.
42
     *
43
     * @return \Illuminate\Config\Repository|mixed|string
44
     */
45
    public function getActionClass()
46
    {
47
        if ($this->actionsClass) {
48
            return $this->actionsClass;
49
        }
50
51
        if ($class = config('admin.grid_action_class')) {
52
            return $class;
53
        }
54
55
        return Grid\Displayers\Actions::class;
56
    }
57
58
    /**
59
     * @param string $actionClass
60
     *
61
     * @return $this
62
     */
63
    public function setActionClass(string $actionClass)
64
    {
65
        if (is_subclass_of($actionClass, Grid\Displayers\Actions::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Encore\Admin\Grid\Displayers\Actions::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
66
            $this->actionsClass = $actionClass;
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * Disable all actions.
74
     *
75
     * @return $this
76
     */
77
    public function disableActions(bool $disable = true)
78
    {
79
        return $this->option('show_actions', !$disable);
0 ignored issues
show
Bug introduced by
It seems like option() 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...
80
    }
81
82
    /**
83
     * Set grid batch-action callback.
84
     *
85
     * @param Closure $closure
86
     *
87
     * @return $this
88
     */
89
    public function batchActions(Closure $closure)
90
    {
91
        $this->tools(function (Grid\Tools $tools) use ($closure) {
0 ignored issues
show
Bug introduced by
It seems like tools() 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...
92
            $tools->batch($closure);
93
        });
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param bool $disable
100
     *
101
     * @return Grid|mixed
102
     */
103
    public function disableBatchActions(bool $disable = true)
104
    {
105
        $this->tools->disableBatchActions($disable);
0 ignored issues
show
Bug introduced by
The property tools does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
106
107
        return $this->option('show_row_selector', !$disable);
0 ignored issues
show
Bug introduced by
It seems like option() 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...
108
    }
109
110
    /**
111
     * Add `actions` column for grid.
112
     *
113
     * @return void
114
     */
115
    protected function appendActionsColumn()
116
    {
117
        if (!$this->option('show_actions')) {
0 ignored issues
show
Bug introduced by
It seems like option() 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...
118
            return;
119
        }
120
121
        $this->addColumn(Grid\Column::ACTION_COLUMN_NAME, trans('admin.action'))
0 ignored issues
show
Bug introduced by
It seems like addColumn() 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...
122
            ->displayUsing($this->getActionClass(), [$this->actionsCallback]);
123
    }
124
}