Completed
Push — master ( ee02fe...8f76f4 )
by Song
03:21
created

BatchActions   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 152
rs 10
c 0
b 0
f 0
wmc 13
lcom 2
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A appendDefaultAction() 0 4 1
A disableDelete() 0 6 1
A disableDeleteAndHodeSelectAll() 0 8 1
A add() 0 16 3
A setUpScripts() 0 10 2
A script() 0 16 1
A render() 0 20 3
1
<?php
2
3
namespace Encore\Admin\Grid\Tools;
4
5
use Encore\Admin\Admin;
6
use Illuminate\Support\Collection;
7
8
class BatchActions extends AbstractTool
9
{
10
    /**
11
     * @var Collection
12
     */
13
    protected $actions;
14
15
    /**
16
     * @var bool
17
     */
18
    protected $enableDelete = true;
19
20
    /**
21
     * @var bool
22
     */
23
    private $isHoldSelectAllCheckbox = false;
24
25
    /**
26
     * BatchActions constructor.
27
     */
28
    public function __construct()
29
    {
30
        $this->actions = new Collection();
31
32
        $this->appendDefaultAction();
33
    }
34
35
    /**
36
     * Append default action(batch delete action).
37
     *
38
     * return void
39
     */
40
    protected function appendDefaultAction()
41
    {
42
        $this->add(new BatchDelete(trans('admin.delete')));
43
    }
44
45
    /**
46
     * Disable delete.
47
     *
48
     * @return $this
49
     */
50
    public function disableDelete()
51
    {
52
        $this->enableDelete = false;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Disable delete And Hode SelectAll Checkbox
59
     *
60
     * @return $this
61
     */
62
    public function disableDeleteAndHodeSelectAll()
63
    {
64
        $this->enableDelete = false;
65
66
        $this->isHoldSelectAllCheckbox = true;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Add a batch action.
73
     *
74
     * @param $title
75
     * @param BatchAction|null $action
76
     *
77
     * @return $this
78
     */
79
    public function add($title, BatchAction $action = null)
80
    {
81
        $id = $this->actions->count();
82
83
        if (func_num_args() == 1) {
84
            $action = $title;
85
            $action->setId($id);
86
        } elseif (func_num_args() == 2) {
87
            $action->setId($id);
0 ignored issues
show
Bug introduced by
It seems like $action is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
88
            $action->setTitle($title);
89
        }
90
91
        $this->actions->push($action);
92
93
        return $this;
94
    }
95
96
    /**
97
     * Setup scripts of batch actions.
98
     *
99
     * @return void
100
     */
101
    protected function setUpScripts()
102
    {
103
        Admin::script($this->script());
104
105
        foreach ($this->actions as $action) {
106
            $action->setGrid($this->grid);
107
108
            Admin::script($action->script());
109
        }
110
    }
111
112
    /**
113
     * Scripts of BatchActions button groups.
114
     *
115
     * @return string
116
     */
117
    protected function script()
118
    {
119
        return <<<EOT
120
121
$('.{$this->grid->getSelectAllName()}').iCheck({checkboxClass:'icheckbox_minimal-blue'});
122
123
$('.{$this->grid->getSelectAllName()}').on('ifChanged', function(event) {
124
    if (this.checked) {
125
        $('.{$this->grid->getGridRowName()}-checkbox').iCheck('check');
126
    } else {
127
        $('.{$this->grid->getGridRowName()}-checkbox').iCheck('uncheck');
128
    }
129
});
130
131
EOT;
132
    }
133
134
    /**
135
     * Render BatchActions button groups.
136
     *
137
     * @return string
138
     */
139
    public function render()
140
    {
141
        if (!$this->enableDelete) {
142
            $this->actions->shift();
143
        }
144
145
        if ($this->actions->isEmpty()) {
146
            return '';
147
        }
148
149
        $this->setUpScripts();
150
151
        $data = [
152
            'actions'       => $this->actions,
153
            'selectAllName' => $this->grid->getSelectAllName(),
154
            'isHoldSelectAllCheckbox' => $this->isHoldSelectAllCheckbox,
155
        ];
156
157
        return view('admin::grid.batch-actions', $data)->render();
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
158
    }
159
}
160