Completed
Push — master ( 5c4b87...207d41 )
by Yaro
06:22
created

UpdateHandlerTrait::handleUpdate()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8.0093

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 18
cts 19
cp 0.9474
rs 8.1475
c 0
b 0
f 0
cc 8
nc 8
nop 2
crap 8.0093
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
use Yaro\Jarboe\Table\Fields\AbstractField;
10
11
trait UpdateHandlerTrait
12
{
13
    /**
14
     * Handle update action.
15
     *
16
     * @param Request $request
17
     * @param $id
18
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
19
     * @throws PermissionDenied
20
     * @throws UnauthorizedException
21
     */
22 4
    public function handleUpdate(Request $request, $id)
23
    {
24 4
        $this->init();
25 4
        $this->bound();
26
27 4
        if (!$this->can('update')) {
28 2
            throw UnauthorizedException::forPermissions(['update']);
29
        }
30
31 2
        $model = $this->crud()->repo()->find($id);
32 2
        if (!$this->crud()->actions()->isAllowed('edit', $model)) {
33 1
            throw new PermissionDenied();
34
        }
35
36 1
        $fields = $this->crud()->getFieldsWithoutMarkup();
37 1
        $data = [];
38
        /** @var AbstractField $field */
39 1
        foreach ($fields as $field) {
40 1
            if ($field->hidden('edit') || $field->isReadonly() || $field->shouldSkip($request)) {
41
                continue;
42
            }
43 1
            $data += [$field->name() => $field->value($request)];
44
        }
45
46 1
        $model = $this->crud()->repo()->update($id, $data);
47
        /** @var AbstractField $field */
48 1
        foreach ($fields as $field) {
49 1
            $field->afterUpdate($model, $request);
50
        }
51 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...
52
53 1
        return redirect($this->crud()->listUrl());
54
    }
55
56
    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...
57
    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...
58
    abstract protected function crud(): CRUD;
59
    abstract protected function can($action): bool;
60
}
61