Completed
Push — master ( 5c55d7...79e99a )
by Fèvre
28s queued 15s
created

WithBulkActions   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 114
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A updatedSelectPage() 0 8 2
A selectPageRows() 0 3 1
A getSelectedRowsQueryProperty() 0 4 1
A selectAll() 0 3 1
A renderingWithBulkActions() 0 4 2
A updatedSelected() 0 4 1
A deleteSelected() 0 14 3
1
<?php
2
3
namespace Xetaravel\Http\Livewire\Traits;
4
5
trait WithBulkActions
6
{
7
    /**
8
     * Whatever the current page of rows are all selected or not.
9
     *
10
     * @var false
11
     */
12
    public $selectPage = false;
13
14
    /**
15
     * Whatever the user has selected all rows or not.
16
     *
17
     * @var bool
18
     */
19
    public $selectAll = false;
20
21
    /**
22
     * The id array of selected rows.
23
     *
24
     * @var array
25
     */
26
    public $selected = [];
27
28
    /**
29
     * If the selectAll is true, we need to select (and check the checkbox) of all rows
30
     * rendering in the current page.
31
     *
32
     * @return void
33
     */
34
    public function renderingWithBulkActions(): void
35
    {
36
        if ($this->selectAll) {
37
            $this->selectPageRows();
38
        }
39
    }
40
41
    /**
42
     * Whenever the user unselect a checkbox, we need to disable the selectAll option and selectPage.
43
     *
44
     * @return void
45
     */
46
    public function updatedSelected(): void
47
    {
48
        $this->selectAll = false;
49
        $this->selectPage = false;
50
    }
51
52
    /**
53
     * Whatever we have selected all rows in the current page.
54
     *
55
     * @param mixed $value The current page where all rows get selected.
56
     *
57
     * @return void
58
     */
59
    public function updatedSelectPage($value)
60
    {
61
        if ($value) {
62
            return $this->selectPageRows();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->selectPageRows() targeting Xetaravel\Http\Livewire\...tions::selectPageRows() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63
        }
64
65
        $this->selectAll = false;
66
        $this->selected = [];
67
    }
68
69
    /**
70
     * Convert the selected rows id into string type.
71
     *
72
     * @return void
73
     */
74
    public function selectPageRows(): void
75
    {
76
        $this->selected = $this->rows->pluck('id')->map(fn($id) => (string) $id);
77
    }
78
79
    /**
80
     * Set selectAll to true.
81
     *
82
     * @return void
83
     */
84
    public function selectAll(): void
85
    {
86
        $this->selectAll = true;
87
    }
88
89
    /**
90
     * Get all select rows by their id, preparing for deleting them.
91
     *
92
     * @return mixed
93
     */
94
    public function getSelectedRowsQueryProperty()
95
    {
96
        return (clone $this->rowsQuery)
97
            ->unless($this->selectAll, fn($query) => $query->whereKey($this->selected));
98
    }
99
100
    /**
101
     * Delete all selected rows and display a flash message.
102
     *
103
     * @return void
104
     */
105
    public function deleteSelected(): void
106
    {
107
        $deleteCount = $this->selectedRowsQuery->count();
0 ignored issues
show
Bug introduced by
The property selectedRowsQuery does not exist on Xetaravel\Http\Livewire\Traits\WithBulkActions. Did you mean selected?
Loading history...
108
109
        if ($deleteCount <= 0) {
110
            return;
111
        }
112
113
        if ($this->selectedRowsQuery->delete()) {
114
            $this->fireFlash('delete', 'success', $deleteCount);
0 ignored issues
show
Bug introduced by
It seems like fireFlash() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
            $this->/** @scrutinizer ignore-call */ 
115
                   fireFlash('delete', 'success', $deleteCount);
Loading history...
115
        } else {
116
            $this->fireFlash('delete', 'danger');
117
        }
118
        $this->showDeleteModal = false;
0 ignored issues
show
Bug Best Practice introduced by
The property showDeleteModal does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
119
    }
120
}
121