Completed
Push — master ( 49dc1a...1f58a4 )
by Yaro
06:59
created

UpdateHandlerTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 5
loc 75
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
C handleUpdate() 5 57 11
beforeInit() 0 1 ?
init() 0 1 ?
bound() 0 1 ?
crud() 0 1 ?
can() 0 1 ?

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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->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 1
39
        $inputs = [];
40 1
        /** @var AbstractField $field */
41 1 View Code Duplication
        foreach ($fields as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            if ($field->belongsToArray()) {
43
                $inputs += [$field->name() => $request->input($field->getDotPatternName())];
44
            }
45 1
        }
46
        $request->replace(
47 1
            $request->all() + $inputs
48
        );
49
50 1
        $data = [];
51
        $additional = [];
52 1
        /** @var AbstractField $field */
53 1
        foreach ($fields as $field) {
54
            if ($field->hidden('edit') || $field->isReadonly() || $field->shouldSkip($request)) {
55 1
                continue;
56
            }
57 1
58
            $field->beforeUpdate($model);
59
60
            if ($field->belongsToArray()) {
61
                $additional[$field->getAncestorName()][$field->getDescendantName()] = $field->value($request);
62
                continue;
63
            }
64
65
            $data += [$field->name() => $field->value($request)];
66
        }
67
68
        $data = $data + $additional;
69
70
        $model = $this->crud()->repo()->update($id, $data);
71
        /** @var AbstractField $field */
72
        foreach ($fields as $field) {
73
            $field->afterUpdate($model, $request);
74
        }
75
        $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...
76
77
        return redirect($this->crud()->listUrl());
78
    }
79
80
    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...
81
    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...
82
    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...
83
    abstract protected function crud(): CRUD;
84
    abstract protected function can($action): bool;
85
}
86