| Conditions | 26 |
| Paths | 3525 |
| Total Lines | 156 |
| 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, " . "refresh your browser, and try again." |
||
| 148 | )); |
||
| 149 | } else { |
||
| 150 | // Clear invalid token on refresh |
||
| 151 | $this->form->clearFormState(); |
||
| 152 | $data = $this->form->getData(); |
||
| 153 | unset($data[$securityID]); |
||
| 154 | $this->form |
||
| 155 | ->setSessionData($data) |
||
| 156 | ->sessionError(_t( |
||
| 157 | "SilverStripe\\Forms\\Form.CSRF_EXPIRED_MESSAGE", |
||
| 158 | "Your session has expired. Please re-submit the form." |
||
| 159 | )); |
||
| 160 | // Return the user |
||
| 161 | return $this->redirectBack(); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | // Determine the action button clicked |
||
| 166 | $funcName = null; |
||
| 167 | foreach ($vars as $paramName => $paramVal) { |
||
| 168 | if (substr($paramName, 0, 7) == 'action_') { |
||
| 169 | // Break off querystring arguments included in the action |
||
| 170 | if (strpos($paramName, '?') !== false) { |
||
| 171 | list($paramName, $paramVars) = explode('?', $paramName, 2); |
||
| 172 | $newRequestParams = array(); |
||
| 173 | parse_str($paramVars, $newRequestParams); |
||
| 174 | $vars = array_merge((array)$vars, (array)$newRequestParams); |
||
| 175 | } |
||
| 176 | |||
| 177 | // Cleanup action_, _x and _y from image fields |
||
| 178 | $funcName = preg_replace(array('/^action_/','/_x$|_y$/'), '', $paramName); |
||
| 179 | break; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | // If the action wasn't set, choose the default on the form. |
||
| 184 | if (!isset($funcName) && $defaultAction = $this->form->defaultAction()) { |
||
| 185 | $funcName = $defaultAction->actionName(); |
||
| 186 | } |
||
| 187 | |||
| 188 | if (isset($funcName)) { |
||
| 189 | $this->setButtonClicked($funcName); |
||
| 190 | } |
||
| 191 | |||
| 192 | // Permission checks (first on controller, then falling back to request handler) |
||
| 193 | $controller = $this->form->getController(); |
||
| 194 | if (// Ensure that the action is actually a button or method on the form, |
||
| 195 | // and not just a method on the controller. |
||
| 196 | $controller |
||
| 197 | && $controller->hasMethod($funcName) |
||
| 198 | && !$controller->checkAccessAction($funcName) |
||
| 199 | // If a button exists, allow it on the controller |
||
| 200 | // buttonClicked() validates that the action set above is valid |
||
| 201 | && !$this->buttonClicked() |
||
| 202 | ) { |
||
| 203 | return $this->httpError( |
||
| 204 | 403, |
||
| 205 | sprintf('Action "%s" not allowed on controller (Class: %s)', $funcName, get_class($controller)) |
||
| 206 | ); |
||
| 207 | } elseif (// No checks for button existence or $allowed_actions is performed - |
||
| 208 | // all form methods are callable (e.g. the legacy "callfieldmethod()") |
||
| 209 | $this->hasMethod($funcName) |
||
| 210 | && !$this->checkAccessAction($funcName) |
||
| 211 | ) { |
||
| 212 | return $this->httpError( |
||
| 213 | 403, |
||
| 214 | sprintf('Action "%s" not allowed on form request handler (Class: "%s")', $funcName, static::class) |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | |||
| 218 | // Action handlers may throw ValidationExceptions. |
||
| 219 | try { |
||
| 220 | // Or we can use the Valiator attached to the form |
||
| 221 | $result = $this->form->validationResult(); |
||
| 222 | if (!$result->isValid()) { |
||
| 223 | return $this->getValidationErrorResponse($result); |
||
| 224 | } |
||
| 225 | |||
| 226 | // First, try a handler method on the controller (has been checked for allowed_actions above already) |
||
| 227 | $controller = $this->form->getController(); |
||
| 228 | $args = [$funcName, $request, $vars]; |
||
| 229 | if ($controller && $controller->hasMethod($funcName)) { |
||
| 230 | $controller->setRequest($request); |
||
| 231 | return $this->invokeFormHandler($controller, ...$args); |
||
| 232 | } |
||
| 233 | |||
| 234 | // Otherwise, try a handler method on the form request handler. |
||
| 235 | if ($this->hasMethod($funcName)) { |
||
| 236 | return $this->invokeFormHandler($this, ...$args); |
||
| 237 | } |
||
| 238 | |||
| 239 | // Otherwise, try a handler method on the form itself |
||
| 240 | if ($this->form->hasMethod($funcName)) { |
||
| 241 | return $this->invokeFormHandler($this->form, ...$args); |
||
| 242 | } |
||
| 243 | |||
| 244 | // Check for inline actions |
||
| 245 | $field = $this->checkFieldsForAction($this->form->Fields(), $funcName); |
||
| 246 | if ($field) { |
||
| 247 | return $this->invokeFormHandler($field, ...$args); |
||
| 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) . ". Implement these in subclasses of " . static::class . " instead" |
||
| 261 | ); |
||
| 262 | } |
||
| 263 | |||
| 264 | return $this->httpError(404, "Could not find a suitable form-action callback function"); |
||
| 265 | } |
||
| 536 |