SoftwareTable   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 72
c 1
b 0
f 0
dl 0
loc 115
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A filters() 0 27 5
A configure() 0 11 1
A builder() 0 4 1
A reorder() 0 4 2
A columns() 0 46 5
A bulkDelete() 0 3 1
1
<?php
2
3
namespace Adminetic\Website\Http\Livewire\Admin\Software;
4
5
use Adminetic\Website\Models\Admin\Software;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Support\Facades\Blade;
8
use Rappasoft\LaravelLivewireTables\DataTableComponent;
9
use Rappasoft\LaravelLivewireTables\Views\Column;
10
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
11
12
class SoftwareTable extends DataTableComponent
13
{
14
    public function builder(): Builder
15
    {
16
        return Software::query()
0 ignored issues
show
Bug Best Practice introduced by
The expression return Adminetic\Website...')->orderBy('position') could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
17
            ->with('category')->orderBy('position'); // Eager load anything; // Select some things
18
    }
19
20
    public array $bulkActions = [
21
        'bulkDelete' => 'Bulk Delete',
22
    ];
23
24
    public function bulkDelete()
25
    {
26
        Software::whereIn('id', $this->getSelected())->delete();
27
    }
28
29
    public function filters(): array
30
    {
31
        return [
32
            SelectFilter::make('Active')
33
                ->options([
34
                    '' => 'All',
35
                    '1' => 'Active',
36
                    '0' => 'Inactive',
37
                ])
38
                ->filter(function (Builder $builder, string $value) {
39
                    if ($value === '1') {
40
                        $builder->where('active', true);
41
                    } elseif ($value === '0') {
42
                        $builder->where('active', false);
43
                    }
44
                }),
45
            SelectFilter::make('Featured')
46
                ->options([
47
                    '' => 'All',
48
                    '1' => 'Featured',
49
                    '0' => 'Not Featured',
50
                ])
51
                ->filter(function (Builder $builder, string $value) {
52
                    if ($value === '1') {
53
                        $builder->where('featured', true);
54
                    } elseif ($value === '0') {
55
                        $builder->where('featured', false);
56
                    }
57
                }),
58
        ];
59
    }
60
61
    public function configure(): void
62
    {
63
        $this->setPrimaryKey('id');
64
65
        $this->setEagerLoadAllRelationsEnabled();
66
67
        $this->setEmptyMessage('No software found');
68
69
        $this->setReorderStatus(true);
70
71
        $this->setDefaultReorderSort('position', 'asc');
72
    }
73
74
    public function reorder($items): void
75
    {
76
        foreach ($items as $item) {
77
            Software::find((int) $item['value'])->update(['position' => (int) $item['order']]);
78
        }
79
    }
80
81
    public function columns(): array
82
    {
83
        return [
84
            Column::make('ID', 'id')
85
                ->sortable()
86
                ->searchable(),
87
            Column::make('Name', 'name')
88
                ->sortable()
89
                ->searchable(),
90
            Column::make('Category', 'category_id')
91
                ->format(
92
                    fn ($value, $row, Column $column) => $row->category->name ?? '-'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

92
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => $row->category->name ?? '-'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
                )
94
                ->sortable()
95
                ->searchable()
96
                ->collapseOnTablet(),
97
            Column::make('Active', 'active')
98
                ->format(
99
                    fn ($value, $row, Column $column) => '<span class="badge badge-'.($row->getRawOriginal('active') ? 'success' : 'danger').' ">'.($row->getRawOriginal('active') ? 'Active' : 'Inactive').'</span>'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

99
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span class="badge badge-'.($row->getRawOriginal('active') ? 'success' : 'danger').' ">'.($row->getRawOriginal('active') ? 'Active' : 'Inactive').'</span>'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
                )
101
                ->html()
102
                ->collapseOnTablet(),
103
            Column::make('Featured', 'featured')
104
                ->format(
105
                    fn ($value, $row, Column $column) => '<span class="badge badge-'.($row->getRawOriginal('featured') ? 'success' : 'primary').' ">'.($row->getRawOriginal('featured') ? 'Featured' : 'Not Featured').'</span>'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

105
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span class="badge badge-'.($row->getRawOriginal('featured') ? 'success' : 'primary').' ">'.($row->getRawOriginal('featured') ? 'Featured' : 'Not Featured').'</span>'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
                )
107
                ->html()
108
                ->collapseOnTablet(),
109
            Column::make('Icon', 'icon')
110
                ->format(
111
                    fn ($value, $row, Column $column) => '<span class="'.$row->icon.' "></span>'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

111
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span class="'.$row->icon.' "></span>'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
                )
113
                ->html()
114
                ->collapseOnTablet(),
115
            Column::make('Color', 'color')
116
                ->format(
117
                    fn ($value, $row, Column $column) => '<span style="height:30px;width:50px;background-color:'.$row->color.'"></span>'
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

117
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span style="height:30px;width:50px;background-color:'.$row->color.'"></span>'

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
                )
119
                ->html()
120
                ->collapseOnTablet(),
121
            Column::make('Action')
122
                ->label(
123
                    fn ($row, Column $column) => Blade::render('<x-adminetic-action :model="$model" route="software" />', ['model' => $row])
0 ignored issues
show
Unused Code introduced by
The parameter $column is not used and could be removed. ( Ignorable by Annotation )

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

123
                    fn ($row, /** @scrutinizer ignore-unused */ Column $column) => Blade::render('<x-adminetic-action :model="$model" route="software" />', ['model' => $row])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124
                )
125
                ->html()
126
                ->collapseOnTablet(),
127
        ];
128
    }
129
}
130