Completed
Push — master ( f066c0...72d72a )
by Yaro
01:50 queued 10s
created

EditHandlerTrait::handleEdit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 12
cts 14
cp 0.8571
rs 9.552
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.0261
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 EditHandlerTrait
11
{
12
    /**
13
     * Show edit form page.
14
     *
15
     * @param Request $request
16
     * @param $id
17
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
18
     * @throws PermissionDenied
19
     * @throws UnauthorizedException
20
     */
21 1
    public function handleEdit(Request $request, $id)
22
    {
23 1
        $this->init();
24 1
        $this->bound();
25
26 1
        $model = $this->crud()->repo()->find($id);
27 1
        if (!$this->crud()->actions()->isAllowed('edit', $model)) {
28
            throw new PermissionDenied();
29
        }
30
31 1
        if (!$this->can('edit')) {
32
            throw UnauthorizedException::forPermissions(['edit']);
33
        }
34
35 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...
36
37 1
        return view($this->viewCrudEdit, [
0 ignored issues
show
Bug introduced by
The property viewCrudEdit 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...
38 1
            'crud' => $this->crud(),
39 1
            'item' => $model,
40 1
            'viewsAbove' => $this->getEditViewsAbove(),
41 1
            'viewsBelow' => $this->getEditViewsBelow(),
42
        ]);
43
    }
44
45
    /**
46
     * Get array of view's objects, that should be rendered above content of `edit` view.
47
     *
48
     * @return array
49
     */
50 2
    protected function getEditViewsAbove(): array
51
    {
52 2
        return [];
53
    }
54
55
    /**
56
     * Get array of view's objects, that should be rendered below content of `edit` view.
57
     *
58
     * @return array
59
     */
60 2
    protected function getEditViewsBelow(): array
61
    {
62 2
        return [];
63
    }
64
65
    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...
66
    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...
67
    abstract protected function crud(): CRUD;
68
    abstract protected function can($action): bool;
69
}
70