PostTable   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
B filters() 0 56 9
A configure() 0 17 1
A bulkChangeStatusToNotApproved() 0 5 1
A bulkDelete() 0 3 1
A bulkActions() 0 8 1
A bulkChangeStatusToPublish() 0 5 1
A bulkChangeStatusToPending() 0 5 1
A builder() 0 4 1
A reorder() 0 4 2
A bulkChangeStatusToDraft() 0 5 1
A columns() 0 40 5
1
<?php
2
3
namespace Adminetic\Website\Http\Livewire\Admin\Post;
4
5
use Adminetic\Website\Models\Admin\Post;
6
use App\Models\User;
0 ignored issues
show
Bug introduced by
The type App\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\Blade;
10
use Rappasoft\LaravelLivewireTables\DataTableComponent;
11
use Rappasoft\LaravelLivewireTables\Views\Column;
12
use Rappasoft\LaravelLivewireTables\Views\Filters\SelectFilter;
13
14
class PostTable extends DataTableComponent
15
{
16
    public function builder(): Builder
17
    {
18
        return Post::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...
19
            ->with('category')->orderBy('position'); // Eager load anything; // Select some things
20
    }
21
22
    public function bulkActions(): array
23
    {
24
        return [
25
            'bulkDelete' => 'Bulk Delete',
26
            'bulkChangeStatusToNotApproved' => 'Mark as not approved',
27
            'bulkChangeStatusToPublish' => 'Mark as published',
28
            'bulkChangeStatusToPending' => 'Mark as pending',
29
            'bulkChangeStatusToDraft' => 'Mark as draft',
30
        ];
31
    }
32
33
    public function bulkDelete()
34
    {
35
        Post::whereIn('id', $this->getSelected())->delete();
36
    }
37
38
    public function bulkChangeStatusToNotApproved()
39
    {
40
        Post::whereIn('id', $this->getSelected())->update([
41
            'status' => 0,
42
            'approved_by' => Auth::user()->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
43
        ]);
44
    }
45
46
    public function bulkChangeStatusToPublish()
47
    {
48
        Post::whereIn('id', $this->getSelected())->update([
49
            'status' => 1,
50
            'approved_by' => Auth::user()->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
51
        ]);
52
    }
53
54
    public function bulkChangeStatusToPending()
55
    {
56
        Post::whereIn('id', $this->getSelected())->update([
57
            'status' => 2,
58
            'approved_by' => Auth::user()->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
59
        ]);
60
    }
61
62
    public function bulkChangeStatusToDraft()
63
    {
64
        Post::whereIn('id', $this->getSelected())->update([
65
            'status' => 3,
66
            'approved_by' => Auth::user()->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
67
        ]);
68
    }
69
70
    public function filters(): array
71
    {
72
        return [
73
            SelectFilter::make('Active')
74
                ->options([
75
                    '' => 'All',
76
                    '1' => 'Active',
77
                    '0' => 'Inactive',
78
                ])
79
                ->filter(function (Builder $builder, string $value) {
80
                    if ($value === '1') {
81
                        $builder->where('active', true);
82
                    } elseif ($value === '0') {
83
                        $builder->where('active', false);
84
                    }
85
                }),
86
            SelectFilter::make('Featured')
87
                ->options([
88
                    '' => 'All',
89
                    '1' => 'Featured',
90
                    '0' => 'Not Featured',
91
                ])
92
                ->filter(function (Builder $builder, string $value) {
93
                    if ($value === '1') {
94
                        $builder->where('featured', true);
95
                    } elseif ($value === '0') {
96
                        $builder->where('featured', false);
97
                    }
98
                }),
99
            SelectFilter::make('Status')
100
                ->options([
101
                    '' => 'All',
102
                    '0' => 'Not Approved',
103
                    '1' => 'Published',
104
                    '2' => 'Pending',
105
                    '3' => 'Draft',
106
                ])
107
                ->filter(function (Builder $builder, string $value) {
108
                    if ($value === '0') {
109
                        $builder->where('status', 0);
110
                    } elseif ($value === '1') {
111
                        $builder->where('status', 1);
112
                    } elseif ($value === '2') {
113
                        $builder->where('status', 2);
114
                    } elseif ($value === '3') {
115
                        $builder->where('status', 3);
116
                    }
117
                }),
118
            SelectFilter::make('Author')
119
                ->options(array_merge([
120
                    '' => 'All',
121
                ], User::find(array_unique(Post::pluck('id')->toArray()))->mapWithKeys(function ($user) {
122
                    return [$user->id => $user->name];
123
                })->toArray()))
124
                ->filter(function (Builder $builder, string $value) {
125
                    $builder->where('user_id', (int) $value);
126
                }),
127
        ];
128
    }
129
130
    public function configure(): void
131
    {
132
        $this->setPrimaryKey('id');
133
134
        $this->setEagerLoadAllRelationsEnabled();
135
136
        $this->setEmptyMessage('No post found');
137
138
        $this->setReorderStatus(true);
139
140
        $this->setBulkActionsStatus(true);
141
142
        $this->setBulkActions([
143
            'exportSelected' => 'Export',
144
        ]);
145
146
        $this->setDefaultReorderSort('position', 'asc');
147
    }
148
149
    public function reorder($items): void
150
    {
151
        foreach ($items as $item) {
152
            Post::find((int) $item['value'])->update(['position' => (int) $item['order']]);
153
        }
154
    }
155
156
    public function columns(): array
157
    {
158
        return [
159
            Column::make('ID', 'id')
160
                ->sortable()
161
                ->searchable(),
162
            Column::make('Name', 'name')
163
                ->sortable()
164
                ->searchable(),
165
            Column::make('Category', 'category_id')
166
                ->format(
167
                    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

167
                    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...
168
                )
169
                ->sortable()
170
                ->searchable()
171
                ->collapseOnTablet(),
172
            Column::make('Active', 'active')
173
                ->format(
174
                    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

174
                    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...
175
                )
176
                ->html()
177
                ->collapseOnTablet(),
178
            Column::make('Featured', 'featured')
179
                ->format(
180
                    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

180
                    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...
181
                )
182
                ->html()
183
                ->collapseOnTablet(),
184
            Column::make('Status', 'status')
185
                ->format(
186
                    fn ($value, $row, Column $column) => '<span class="badge badge-'.$row->getStatusColor().' ">'.$row->status.'</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

186
                    fn ($value, $row, /** @scrutinizer ignore-unused */ Column $column) => '<span class="badge badge-'.$row->getStatusColor().' ">'.$row->status.'</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...
187
                )
188
                ->html()
189
                ->collapseOnTablet(),
190
            Column::make('Action')
191
                ->label(
192
                    fn ($row, Column $column) => Blade::render('<x-adminetic-action :model="$model" route="post" />', ['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

192
                    fn ($row, /** @scrutinizer ignore-unused */ Column $column) => Blade::render('<x-adminetic-action :model="$model" route="post" />', ['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...
193
                )
194
                ->html()
195
                ->collapseOnTablet(),
196
        ];
197
    }
198
}
199