Completed
Push — master ( 1d3c0c...8bf0fe )
by Song
03:06
created

HandleController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 141
Duplicated Lines 29.79 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
dl 42
loc 141
rs 10
c 0
b 0
f 0
wmc 19
lcom 0
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A handleForm() 0 10 2
A resolveForm() 21 21 4
B handleAction() 0 32 6
A resolveActionInstance() 21 21 4
A resolveActionArgs() 0 19 3

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 Encore\Admin\Controllers;
4
5
use Encore\Admin\Actions\Action;
6
use Encore\Admin\Actions\GridAction;
7
use Encore\Admin\Actions\Response;
8
use Encore\Admin\Actions\RowAction;
9
use Encore\Admin\Widgets\Form;
10
use Exception;
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Http\Request;
13
use Illuminate\Routing\Controller;
14
use Illuminate\Support\Collection;
15
16
class HandleController extends Controller
17
{
18
    /**
19
     * @param Request $request
20
     *
21
     * @return $this|mixed
22
     */
23
    public function handleForm(Request $request)
24
    {
25
        $form = $this->resolveForm($request);
26
27
        if ($errors = $form->validate($request)) {
28
            return back()->withInput()->withErrors($errors);
29
        }
30
31
        return $form->sanitize()->handle($request);
32
    }
33
34
    /**
35
     * @param Request $request
36
     *
37
     * @return Form
38
     *
39
     * @throws Exception
40
     */
41 View Code Duplication
    protected function resolveForm(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
43
        if (!$request->has('_form_')) {
44
            throw new Exception('Invalid form request.');
45
        }
46
47
        $formClass = $request->get('_form_');
48
49
        if (!class_exists($formClass)) {
50
            throw new Exception("Form [{$formClass}] does not exist.");
51
        }
52
53
        /** @var Form $form */
54
        $form = app($formClass);
55
56
        if (!method_exists($form, 'handle')) {
57
            throw new Exception("Form method {$formClass}::handle() does not exist.");
58
        }
59
60
        return $form;
61
    }
62
63
    /**
64
     * @param Request $request
65
     *
66
     * @return $this|\Illuminate\Http\JsonResponse
67
     */
68
    public function handleAction(Request $request)
69
    {
70
        $action = $this->resolveActionInstance($request);
71
72
        $model     = null;
73
        $arguments = [];
74
75
        if ($action instanceof GridAction) {
76
            $model = $action->retrieveModel($request);
77
            $arguments[] = $model;
78
        }
79
80
        if (!$action->passesAuthorization($model)) {
81
            return $action->failedAuthorization();
82
        }
83
84
        if ($action instanceof RowAction) {
85
            $action->setRow($model);
86
        }
87
88
        try {
89
            $response = $action->validate($request)->handle(
0 ignored issues
show
Bug introduced by
The method handle() does not exist on Encore\Admin\Actions\Action. Did you maybe mean getHandleRoute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
90
                ...$this->resolveActionArgs($request, ...$arguments)
0 ignored issues
show
Documentation introduced by
$arguments is of type array, but the function expects a object<Illuminate\Databa...ollection>|boolean|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
            );
92
        } catch (Exception $exception) {
93
            return Response::withException($exception)->send();
94
        }
95
96
        if ($response instanceof Response) {
97
            return $response->send();
98
        }
99
    }
100
101
    /**
102
     * @param Request $request
103
     *
104
     * @return Action
105
     *
106
     * @throws Exception
107
     */
108 View Code Duplication
    protected function resolveActionInstance(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
109
    {
110
        if (!$request->has('_action')) {
111
            throw new Exception('Invalid action request.');
112
        }
113
114
        $actionClass = str_replace('_', '\\', $request->get('_action'));
115
116
        if (!class_exists($actionClass)) {
117
            throw new Exception("Form [{$actionClass}] does not exist.");
118
        }
119
120
        /** @var GridAction $form */
121
        $action = app($actionClass);
122
123
        if (!method_exists($action, 'handle')) {
124
            throw new Exception("Action method {$actionClass}::handle() does not exist.");
125
        }
126
127
        return $action;
128
    }
129
130
    /**
131
     * @param Request $request
132
     *
133
     * @param Model|Collection|bool $model
134
     *
135
     * @return array
136
     */
137
    protected function resolveActionArgs(Request $request, $model = null)
138
    {
139
        $input = $request;
140
141
        if ($request->has(GridAction::INPUT_NAME)) {
142
            $input = $request->file(
143
                GridAction::INPUT_NAME,
144
                $request->get(GridAction::INPUT_NAME)
145
            );
146
        }
147
148
        $args = [$input];
149
150
        if (!empty($model)) {
151
            array_unshift($args, $model);
152
        }
153
154
        return $args;
155
    }
156
}
157