SortableHandlerTrait::switchSortable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 14
ccs 0
cts 9
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Yaro\Jarboe\Http\Controllers\Traits\Handlers;
4
5
use Illuminate\Http\Request;
6
use Yaro\Jarboe\Exceptions\PermissionDenied;
7
use Yaro\Jarboe\Table\CRUD;
8
9
trait SortableHandlerTrait
10
{
11
    /**
12
     * Switch table order for making sortable table.
13
     *
14
     * @return \Illuminate\Http\RedirectResponse
15
     * @throws PermissionDenied
16
     */
17
    public function switchSortable()
18
    {
19
        $this->beforeInit();
20
        $this->init();
21
        $this->bound();
22
23
        if (!$this->crud()->isSortableByWeight()) {
24
            throw new PermissionDenied();
25
        }
26
27
        $newState = !$this->crud()->isSortableByWeightActive();
28
        $this->crud()->setSortableOrderState($newState);
29
30
        return back();
31
    }
32
33
    /**
34
     * Change sort weight of dragged row.
35
     *
36
     * @param $id
37
     * @param Request $request
38
     * @return \Illuminate\Http\JsonResponse
39
     * @throws PermissionDenied
40
     */
41
    public function moveItem($id, Request $request)
42
    {
43
        $this->beforeInit();
44
        $this->init();
45
        $this->bound();
46
47
        if (!$this->crud()->isSortableByWeight()) {
48
            throw new PermissionDenied();
49
        }
50
51
        $this->crud()->repo()->reorder($id, $request->get('prev'), $request->get('next'));
52
53
        return response()->json([
54
            'ok' => true,
55
        ]);
56
    }
57
58
    abstract protected function beforeInit();
59
    abstract protected function init();
60
    abstract protected function bound();
61
    abstract protected function crud(): CRUD;
62
    abstract protected function can($action): bool;
63
}
64