Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Test Failed
Pull Request — main (#5766)
by
unknown
11:00
created

SimpleReorderOperation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 84
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setupSimpleReorderRoutes() 0 12 1
A saveReorder() 0 40 3
A reorder() 0 10 1
A setupSimpleReorderDefaults() 0 11 1
1
<?php
2
3
namespace App\Http\Controllers\Admin\Operations;
4
5
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Route;
9
use Illuminate\View\View;
10
11
trait SimpleReorderOperation
12
{
13
    protected function setupSimpleReorderRoutes(string $segment, string $routeName, string $controller): void
14
    {
15
        Route::get("{$segment}/reorder", [
16
            'as' => "{$routeName}.reorder",
17
            'uses' => "{$controller}@reorder",
18
            'operation' => 'reorder',
19
        ]);
20
21
        Route::post("{$segment}/reorder", [
22
            'as' => "{$routeName}.save.reorder",
23
            'uses' => "{$controller}@saveReorder",
24
            'operation' => 'reorder',
25
        ]);
26
    }
27
28
    protected function setupSimpleReorderDefaults()
29
    {
30
        CRUD::set('reorder.enabled', true);
0 ignored issues
show
Bug introduced by
The method set() does not exist on Backpack\CRUD\app\Librar...udPanel\CrudPanelFacade. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

30
        CRUD::/** @scrutinizer ignore-call */ 
31
              set('reorder.enabled', true);
Loading history...
31
        CRUD::allowAccess('reorder');
0 ignored issues
show
Bug introduced by
The method allowAccess() does not exist on Backpack\CRUD\app\Librar...udPanel\CrudPanelFacade. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

31
        CRUD::/** @scrutinizer ignore-call */ 
32
              allowAccess('reorder');
Loading history...
32
33
        CRUD::operation('reorder', function () {
0 ignored issues
show
Bug introduced by
The method operation() does not exist on Backpack\CRUD\app\Librar...udPanel\CrudPanelFacade. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

33
        CRUD::/** @scrutinizer ignore-call */ 
34
              operation('reorder', function () {
Loading history...
34
            CRUD::loadDefaultOperationSettingsFromConfig();
0 ignored issues
show
Bug introduced by
The method loadDefaultOperationSettingsFromConfig() does not exist on Backpack\CRUD\app\Librar...udPanel\CrudPanelFacade. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

34
            CRUD::/** @scrutinizer ignore-call */ 
35
                  loadDefaultOperationSettingsFromConfig();
Loading history...
35
        });
36
37
        CRUD::operation('list', function () {
38
            CRUD::addButton('top', 'reorder', 'view', 'crud::buttons.reorder');
0 ignored issues
show
Bug introduced by
The method addButton() does not exist on Backpack\CRUD\app\Librar...udPanel\CrudPanelFacade. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

38
            CRUD::/** @scrutinizer ignore-call */ 
39
                  addButton('top', 'reorder', 'view', 'crud::buttons.reorder');
Loading history...
39
        });
40
    }
41
42
    public function reorder(): View
43
    {
44
        CRUD::hasAccessOrFail('reorder');
0 ignored issues
show
Bug introduced by
The method hasAccessOrFail() does not exist on Backpack\CRUD\app\Librar...udPanel\CrudPanelFacade. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

44
        CRUD::/** @scrutinizer ignore-call */ 
45
              hasAccessOrFail('reorder');
Loading history...
45
46
        $column = CRUD::get('reorder.column');
0 ignored issues
show
Bug introduced by
The method get() does not exist on Backpack\CRUD\app\Librar...udPanel\CrudPanelFacade. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

46
        /** @scrutinizer ignore-call */ 
47
        $column = CRUD::get('reorder.column');
Loading history...
47
48
        return view('crud::simple-reorder', [
49
            'entries' => $this->crud->getEntries()->sortBy($column)->keyBy($this->crud->getModel()->getKeyName()),
50
            'crud' => $this->crud,
51
            'title' => $this->crud->getTitle() ?? trans('backpack::crud.reorder') . ' ' . $this->crud->entity_name,
52
        ]);
53
    }
54
55
    public function saveReorder(Request $request): string|false
56
    {
57
        CRUD::hasAccessOrFail('reorder');
58
59
        $entries = json_decode($request->input('tree'), true);
60
61
        if (empty($entries)) {
62
            return false;
63
        }
64
65
        DB::transaction(function () use ($entries) {
66
            $primaryKey = $this->crud->model->getKeyName();
67
            $table = $this->crud->model->getTable();
68
            $connection = $this->crud->model->getConnectionName();
69
            $column = $this->crud->get('reorder.column');
70
71
            $query = '';
72
            $bindings = $itemKeys = [];
73
            $query .= "UPDATE `{$table}` SET `{$column}` = CASE ";
74
            foreach ($entries as $order => $item) {
75
                $itemKey = str_replace('list_', '', $item);
76
                $itemKeys[] = $itemKey;
77
78
                $query .= "WHEN `{$primaryKey}` = ? THEN ? ";
79
                $bindings[] = $itemKey;
80
                $bindings[] = $order;
81
            }
82
            // add the bind placeholders for the item keys at the end the array of bindings
83
            array_push($bindings, ...$itemKeys);
84
            $reorderItemsBindString = implode(',', array_fill(0, count($itemKeys), '?'));
85
86
            // add the where clause to the query to help match the items
87
            $query .= "ELSE `{$column}` END WHERE `{$primaryKey}` IN ({$reorderItemsBindString})";
88
89
            DB::connection($connection)->statement($query, $bindings);
90
        });
91
92
        $updatedNumber = count($entries);
93
94
        return "success for {$updatedNumber} items";
95
    }
96
}
97