Completed
Push — master ( 79186c...935f65 )
by Yaro
04:21
created

DeleteHandlerTrait::handleDelete()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 5.3374

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 16
cts 21
cp 0.7619
rs 9.0488
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 5.3374
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)
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 introduced by
The property idEntity does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
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([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
58
    abstract protected function init();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
59
    abstract protected function bound();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
60
    abstract protected function crud(): CRUD;
61
    abstract protected function can($action): bool;
62
}
63