UpdateHandlerTrait::handleUpdate()   B
last analyzed

Complexity

Conditions 11
Paths 26

Size

Total Lines 56
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 11.0036

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 31
c 1
b 0
f 0
nc 26
nop 2
dl 0
loc 56
ccs 31
cts 32
cp 0.9688
crap 11.0036
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
0 ignored issues
show
Bug introduced by
The type Yaro\Jarboe\Table\Fields\AbstractField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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->beforeInit();
25 4
        $this->init();
26 4
        $this->bound();
27
28 4
        if (!$this->can('update')) {
29 2
            throw UnauthorizedException::forPermissions(['update']);
30
        }
31
32 2
        $model = $this->crud()->repo()->find($id);
33 2
        if (!$this->crud()->actions()->isAllowed('edit', $model)) {
34 1
            throw new PermissionDenied();
35
        }
36
37 1
        $fields = $this->crud()->getFieldsWithoutMarkup();
38
39 1
        $inputs = [];
40
        /** @var AbstractField $field */
41 1
        foreach ($fields as $field) {
42 1
            if ($field->belongsToArray()) {
43 1
                $inputs += [$field->name() => $request->input($field->getDotPatternName())];
44
            }
45
        }
46 1
        $request->replace(
47 1
            $request->all() + $inputs
48
        );
49
50 1
        $data = [];
51 1
        $additional = [];
52
        /** @var AbstractField $field */
53 1
        foreach ($fields as $field) {
54 1
            if ($field->hidden('edit') || $field->isReadonly() || $field->shouldSkip($request)) {
55
                continue;
56
            }
57
58 1
            $field->beforeUpdate($model);
59
60 1
            if ($field->belongsToArray()) {
61 1
                $additional[$field->getAncestorName()][$field->getDescendantName()] = $field->value($request);
62 1
                continue;
63
            }
64
65 1
            $data += [$field->name() => $field->value($request)];
66
        }
67
68 1
        $data = $data + $additional;
69
70 1
        $model = $this->crud()->repo()->update($id, $data);
71
        /** @var AbstractField $field */
72 1
        foreach ($fields as $field) {
73 1
            $field->afterUpdate($model, $request);
74
        }
75 1
        $this->idEntity = $model->getKey();
0 ignored issues
show
Bug Best Practice introduced by
The property idEntity does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
76
77 1
        return redirect($this->crud()->listUrl());
78
    }
79
80
    abstract protected function beforeInit();
81
    abstract protected function init();
82
    abstract protected function bound();
83
    abstract protected function crud(): CRUD;
84
    abstract protected function can($action): bool;
85
}
86