Completed
Push — master ( 665333...49dc1a )
by Yaro
18:38 queued 08:40
created

RevertHandlerTrait::can()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Yaro\Jarboe\Http\Controllers\Traits\Handlers;
4
5
use Illuminate\Http\Request;
6
use RuntimeException;
7
use Spatie\Permission\Exceptions\UnauthorizedException;
8
use Yaro\Jarboe\Exceptions\PermissionDenied;
9
use Yaro\Jarboe\Table\CRUD;
10
11
trait RevertHandlerTrait
12
{
13
    /**
14
     * Revert model to specific version.
15
     *
16
     * @param Request $request
17
     * @param $id
18
     * @return \Illuminate\Http\JsonResponse
19
     * @throws PermissionDenied
20
     * @throws UnauthorizedException
21
     */
22
    public function handleRevert(Request $request, $id)
23
    {
24
        $this->beforeInit();
25
        $this->init();
26
        $this->bound();
27
28
        if (!$this->can('revert')) {
29
            throw UnauthorizedException::forPermissions(['revert']);
30
        }
31
32
        $model = $this->crud()->repo()->find($id);
33
        if (!$this->crud()->actions()->isAllowed('history', $model)) {
34
            throw new PermissionDenied();
35
        }
36
37
        $version = $model->versions()->find($request->get('version'));
38
        if (is_null($version)) {
39
            throw new RuntimeException('Version does not exist');
40
        }
41
42
        $version->revert();
43
44
        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...
45
            'ok' => true,
46
            'timeline' => view('jarboe::crud.inc.history.timeline', [
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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
                'crud' => $this->crud(),
48
                'item' => $model,
49
                'versions' => $model->versions()->latest()->paginate(8),
50
            ])->render(),
51
        ]);
52
    }
53
54
    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...
55
    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...
56
    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...
57
    abstract protected function crud(): CRUD;
58
    abstract protected function can($action): bool;
59
}
60