Conditions | 26 |
Paths | 3365 |
Total Lines | 157 |
Code Lines | 87 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
109 | public function httpSubmission($request) |
||
110 | { |
||
111 | // Strict method check |
||
112 | if ($this->form->getStrictFormMethodCheck()) { |
||
113 | // Throws an error if the method is bad... |
||
114 | $allowedMethod = $this->form->FormMethod(); |
||
115 | if ($allowedMethod !== $request->httpMethod()) { |
||
116 | $response = Controller::curr()->getResponse(); |
||
117 | $response->addHeader('Allow', $allowedMethod); |
||
118 | $this->httpError(405, _t( |
||
119 | "SilverStripe\\Forms\\Form.BAD_METHOD", |
||
120 | "This form requires a {method} submission", |
||
121 | ['method' => $allowedMethod] |
||
122 | )); |
||
123 | } |
||
124 | |||
125 | // ...and only uses the variables corresponding to that method type |
||
126 | $vars = $allowedMethod === 'GET' |
||
127 | ? $request->getVars() |
||
128 | : $request->postVars(); |
||
129 | } else { |
||
130 | $vars = $request->requestVars(); |
||
131 | } |
||
132 | |||
133 | // Ensure we only process saveable fields (non structural, readonly, or disabled) |
||
134 | $allowedFields = array_keys($this->form->Fields()->saveableFields()); |
||
135 | |||
136 | // Populate the form |
||
137 | $this->form->loadDataFrom($vars, true, $allowedFields); |
||
138 | |||
139 | // Protection against CSRF attacks |
||
140 | // @todo Move this to SecurityTokenField::validate() |
||
141 | $token = $this->form->getSecurityToken(); |
||
142 | if (! $token->checkRequest($request)) { |
||
143 | $securityID = $token->getName(); |
||
144 | if (empty($vars[$securityID])) { |
||
145 | $this->httpError(400, _t( |
||
146 | "SilverStripe\\Forms\\Form.CSRF_FAILED_MESSAGE", |
||
147 | "There seems to have been a technical problem. Please click the back button, ". |
||
148 | "refresh your browser, and try again." |
||
149 | )); |
||
150 | } else { |
||
151 | // Clear invalid token on refresh |
||
152 | $this->form->clearFormState(); |
||
153 | $data = $this->form->getData(); |
||
154 | unset($data[$securityID]); |
||
155 | $this->form |
||
156 | ->setSessionData($data) |
||
157 | ->sessionError(_t( |
||
158 | "SilverStripe\\Forms\\Form.CSRF_EXPIRED_MESSAGE", |
||
159 | "Your session has expired. Please re-submit the form." |
||
160 | )); |
||
161 | |||
162 | // Return the user |
||
163 | return $this->redirectBack(); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | // Determine the action button clicked |
||
168 | $funcName = null; |
||
169 | foreach ($vars as $paramName => $paramVal) { |
||
170 | if (substr($paramName, 0, 7) == 'action_') { |
||
171 | // Break off querystring arguments included in the action |
||
172 | if (strpos($paramName, '?') !== false) { |
||
173 | list($paramName, $paramVars) = explode('?', $paramName, 2); |
||
174 | $newRequestParams = array(); |
||
175 | parse_str($paramVars, $newRequestParams); |
||
176 | $vars = array_merge((array)$vars, (array)$newRequestParams); |
||
177 | } |
||
178 | |||
179 | // Cleanup action_, _x and _y from image fields |
||
180 | $funcName = preg_replace(array('/^action_/','/_x$|_y$/'), '', $paramName); |
||
181 | break; |
||
182 | } |
||
183 | } |
||
184 | |||
185 | // If the action wasn't set, choose the default on the form. |
||
186 | if (!isset($funcName) && $defaultAction = $this->form->defaultAction()) { |
||
187 | $funcName = $defaultAction->actionName(); |
||
188 | } |
||
189 | |||
190 | if (isset($funcName)) { |
||
191 | $this->setButtonClicked($funcName); |
||
192 | } |
||
193 | |||
194 | // Permission checks (first on controller, then falling back to request handler) |
||
195 | $controller = $this->form->getController(); |
||
196 | if (// Ensure that the action is actually a button or method on the form, |
||
197 | // and not just a method on the controller. |
||
198 | $controller |
||
199 | && $controller->hasMethod($funcName) |
||
200 | && !$controller->checkAccessAction($funcName) |
||
201 | // If a button exists, allow it on the controller |
||
202 | // buttonClicked() validates that the action set above is valid |
||
203 | && !$this->buttonClicked() |
||
204 | ) { |
||
205 | return $this->httpError( |
||
206 | 403, |
||
207 | sprintf('Action "%s" not allowed on controller (Class: %s)', $funcName, get_class($controller)) |
||
208 | ); |
||
209 | } elseif (// No checks for button existence or $allowed_actions is performed - |
||
210 | // all form methods are callable (e.g. the legacy "callfieldmethod()") |
||
211 | $this->hasMethod($funcName) |
||
212 | && !$this->checkAccessAction($funcName) |
||
213 | ) { |
||
214 | return $this->httpError( |
||
215 | 403, |
||
216 | sprintf('Action "%s" not allowed on form request handler (Class: "%s")', $funcName, static::class) |
||
217 | ); |
||
218 | } |
||
219 | |||
220 | // Action handlers may throw ValidationExceptions. |
||
221 | try { |
||
222 | // Or we can use the Valiator attached to the form |
||
223 | $result = $this->form->validationResult(); |
||
224 | if (!$result->isValid()) { |
||
225 | return $this->getValidationErrorResponse($result); |
||
226 | } |
||
227 | |||
228 | // First, try a handler method on the controller (has been checked for allowed_actions above already) |
||
229 | $controller = $this->form->getController(); |
||
230 | if ($controller && $controller->hasMethod($funcName)) { |
||
231 | return $controller->$funcName($vars, $this->form, $request, $this); |
||
232 | } |
||
233 | |||
234 | // Otherwise, try a handler method on the form request handler. |
||
235 | if ($this->hasMethod($funcName)) { |
||
236 | return $this->$funcName($vars, $this->form, $request, $this); |
||
237 | } |
||
238 | |||
239 | // Otherwise, try a handler method on the form itself |
||
240 | if ($this->form->hasMethod($funcName)) { |
||
241 | return $this->form->$funcName($vars, $this->form, $request, $this); |
||
242 | } |
||
243 | |||
244 | // Check for inline actions |
||
245 | $field = $this->checkFieldsForAction($this->form->Fields(), $funcName); |
||
246 | if ($field) { |
||
247 | return $field->$funcName($vars, $this->form, $request, $this); |
||
248 | } |
||
249 | } catch (ValidationException $e) { |
||
250 | // The ValdiationResult contains all the relevant metadata |
||
251 | $result = $e->getResult(); |
||
252 | $this->form->loadMessagesFrom($result); |
||
253 | return $this->getValidationErrorResponse($result); |
||
254 | } |
||
255 | |||
256 | // Determine if legacy form->allowed_actions is set |
||
257 | $legacyActions = $this->form->config()->get('allowed_actions'); |
||
258 | if ($legacyActions) { |
||
259 | throw new BadMethodCallException( |
||
260 | "allowed_actions are not valid on Form class " . get_class($this->form) . |
||
261 | ". Implement these in subclasses of " . static::class . " instead" |
||
262 | ); |
||
263 | } |
||
264 | |||
265 | return $this->httpError(404); |
||
266 | } |
||
521 |