Completed
Branch master (38df34)
by Yaro
08:01 queued 02:03
created

StoreHandlerTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 6.76 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 96.67%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 5
loc 74
ccs 29
cts 30
cp 0.9667
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
beforeInit() 0 1 ?
init() 0 1 ?
bound() 0 1 ?
crud() 0 1 ?
can() 0 1 ?
B handleStore() 5 57 10

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 StoreHandlerTrait
12
{
13
    /**
14
     * Handle store action.
15
     *
16
     * @param Request $request
17
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
18
     * @throws PermissionDenied
19
     * @throws UnauthorizedException
20
     */
21 4
    public function handleStore(Request $request)
22
    {
23 4
        $this->beforeInit();
24 4
        $this->init();
25 4
        $this->bound();
26
27 4
        if (!$this->crud()->actions()->isAllowed('create')) {
28 1
            throw new PermissionDenied();
29
        }
30
31 3
        if (!$this->can('store')) {
32 2
            throw UnauthorizedException::forPermissions(['store']);
33
        }
34
35
36 1
        $fields = $this->crud()->getFieldsWithoutMarkup();
37
38 1
        $inputs = [];
39
        /** @var AbstractField $field */
40 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...
41 1
            if ($field->belongsToArray()) {
42 1
                $inputs += [$field->name() => $request->input($field->getDotPatternName())];
43
            }
44
        }
45 1
        $request->replace(
46 1
            $request->all() + $inputs
47
        );
48
49
50 1
        $data = [];
51 1
        $additional = [];
52
        /** @var AbstractField $field */
53 1
        foreach ($fields as $field) {
54 1
            if ($field->hidden('create') || $field->shouldSkip($request)) {
55
                continue;
56
            }
57
58 1
            if ($field->belongsToArray()) {
59 1
                $additional[$field->getAncestorName()][$field->getDescendantName()] = $field->value($request);
60 1
                continue;
61
            }
62
63 1
            $data += [$field->name() => $field->value($request)];
64
        }
65
66 1
        $data = $data + $additional;
67
68 1
        $model = $this->crud()->repo()->store($data);
69
        /** @var AbstractField $field */
70 1
        foreach ($fields as $field) {
71 1
            $field->afterStore($model, $request);
72
        }
73
74 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...
75
76 1
        return redirect($this->crud()->listUrl());
77
    }
78
79
    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...
80
    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...
81
    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...
82
    abstract protected function crud(): CRUD;
83
    abstract protected function can($action): bool;
84
}
85