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

UpdateHandlerTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 36
ccs 9
cts 11
cp 0.8182
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handleUpdate() 0 19 3
init() 0 1 ?
bound() 0 1 ?
crud() 0 1 ?
can() 0 1 ?
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 UpdateHandlerTrait
11
{
12
    /**
13
     * Handle update action.
14
     *
15
     * @param Request $request
16
     * @param $id
17
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
18
     * @throws PermissionDenied
19
     * @throws UnauthorizedException
20
     */
21 1
    public function handleUpdate(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('update')) {
32
            throw UnauthorizedException::forPermissions(['update']);
33
        }
34
35 1
        $this->crud()->repo()->update($id, $request);
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
        return redirect($this->crud()->listUrl());
39
    }
40
41
    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...
42
    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...
43
    abstract protected function crud(): CRUD;
44
    abstract protected function can($action): bool;
45
}
46