Completed
Push — master ( 4ca583...a67af2 )
by Song
02:43
created

HandleController::resolveForm()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 21
loc 21
rs 9.584
c 0
b 0
f 0
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 Encore\Admin\Widgets\Selectable\Selectable;
11
use Exception;
12
use Illuminate\Database\Eloquent\Model;
13
use Illuminate\Http\Request;
14
use Illuminate\Routing\Controller;
15
use Illuminate\Support\Collection;
16
17
class HandleController extends Controller
18
{
19
    /**
20
     * @param Request $request
21
     *
22
     * @return $this|mixed
23
     */
24
    public function handleForm(Request $request)
25
    {
26
        $form = $this->resolveForm($request);
27
28
        if ($errors = $form->validate($request)) {
29
            return back()->withInput()->withErrors($errors);
30
        }
31
32
        return $form->sanitize()->handle($request);
33
    }
34
35
    /**
36
     * @param Request $request
37
     *
38
     * @throws Exception
39
     *
40
     * @return Form
41
     */
42 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...
43
    {
44
        if (!$request->has('_form_')) {
45
            throw new Exception('Invalid form request.');
46
        }
47
48
        $formClass = $request->get('_form_');
49
50
        if (!class_exists($formClass)) {
51
            throw new Exception("Form [{$formClass}] does not exist.");
52
        }
53
54
        /** @var Form $form */
55
        $form = app($formClass);
56
57
        if (!method_exists($form, 'handle')) {
58
            throw new Exception("Form method {$formClass}::handle() does not exist.");
59
        }
60
61
        return $form;
62
    }
63
64
    /**
65
     * @param Request $request
66
     *
67
     * @return $this|\Illuminate\Http\JsonResponse
68
     */
69
    public function handleAction(Request $request)
70
    {
71
        $action = $this->resolveActionInstance($request);
72
73
        $model = null;
74
        $arguments = [];
75
76
        if ($action instanceof GridAction) {
77
            $model = $action->retrieveModel($request);
78
            $arguments[] = $model;
79
        }
80
81
        if (!$action->passesAuthorization($model)) {
82
            return $action->failedAuthorization();
83
        }
84
85
        if ($action instanceof RowAction) {
86
            $action->setRow($model);
87
        }
88
89
        try {
90
            $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...
91
                ...$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...
92
            );
93
        } catch (Exception $exception) {
94
            return Response::withException($exception)->send();
95
        }
96
97
        if ($response instanceof Response) {
98
            return $response->send();
99
        }
100
    }
101
102
    /**
103
     * @param Request $request
104
     *
105
     * @throws Exception
106
     *
107
     * @return Action
108
     */
109 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...
110
    {
111
        if (!$request->has('_action')) {
112
            throw new Exception('Invalid action request.');
113
        }
114
115
        $actionClass = str_replace('_', '\\', $request->get('_action'));
116
117
        if (!class_exists($actionClass)) {
118
            throw new Exception("Form [{$actionClass}] does not exist.");
119
        }
120
121
        /** @var GridAction $form */
122
        $action = app($actionClass);
123
124
        if (!method_exists($action, 'handle')) {
125
            throw new Exception("Action method {$actionClass}::handle() does not exist.");
126
        }
127
128
        return $action;
129
    }
130
131
    /**
132
     * @param Request               $request
133
     * @param Model|Collection|bool $model
134
     *
135
     * @return array
136
     */
137
    protected function resolveActionArgs(Request $request, $model = null)
138
    {
139
        $args = [$request];
140
141
        if (!empty($model)) {
142
            array_unshift($args, $model);
143
        }
144
145
        return $args;
146
    }
147
148
    public function handleSelectable(Request $request)
149
    {
150
        $class = $request->get('selectable');
151
        $multiple = $request->get('multiple', 0);
152
153
        $class = str_replace('_', '\\', $class);
154
155
        if (class_exists($class)) {
156
            /** @var Selectable $selectable */
157
            $selectable = new $class;
158
159
            return $selectable->render($multiple);
160
        }
161
162
        return $class;
163
    }
164
}
165