DeleteHandlerTrait::handleDelete()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.3374

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 21
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 34
ccs 16
cts 21
cp 0.7619
crap 5.3374
rs 9.2728
1
<?php
2
3
namespace Yaro\Jarboe\Http\Controllers\Traits\Handlers;
4
5
use Illuminate\Http\Request;
6
use Spatie\Permission\Exceptions\UnauthorizedException;
7
use Yaro\Jarboe\Exceptions\PermissionDenied;
8
use Yaro\Jarboe\Table\CRUD;
9
10
trait DeleteHandlerTrait
11
{
12
    /**
13
     * Handle delete action.
14
     *
15
     * @param Request $request
16
     * @param $id
17
     * @return \Illuminate\Http\JsonResponse
18
     * @throws PermissionDenied
19
     * @throws UnauthorizedException
20
     */
21 4
    public function handleDelete(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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

21
    public function handleDelete(/** @scrutinizer ignore-unused */ Request $request, $id)

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...
22
    {
23 4
        $this->beforeInit();
24 4
        $this->init();
25 4
        $this->bound();
26
27 4
        $model = $this->crud()->repo()->find($id);
28 4
        if (!$this->crud()->actions()->isAllowed('delete', $model)) {
29 1
            throw new PermissionDenied();
30
        }
31
32 3
        if (!$this->can('delete')) {
33 2
            throw UnauthorizedException::forPermissions(['delete']);
34
        }
35
36 1
        $this->idEntity = $model->getKey();
0 ignored issues
show
Bug Best Practice introduced by
The property idEntity does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
38 1
        if ($this->crud()->repo()->delete($id)) {
39 1
            $type = 'hidden';
40
            try {
41 1
                $this->crud()->repo()->find($id);
42
            } catch (\Exception $e) {
43
                $type = 'removed';
44
            }
45
46 1
            return response()->json([
47 1
                'type' => $type,
48 1
                'message' => __('jarboe::common.list.delete_success_message', ['id' => $id]),
49
            ]);
50
        }
51
52
        return response()->json([
53
            'message' => __('jarboe::common.list.delete_failed_message', ['id' => $id]),
54
        ], 422);
55
    }
56
57
    abstract protected function beforeInit();
58
    abstract protected function init();
59
    abstract protected function bound();
60
    abstract protected function crud(): CRUD;
61
    abstract protected function can($action): bool;
62
}
63