| Conditions | 11 |
| Paths | 8 |
| Total Lines | 62 |
| 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 |
||
| 45 | public function partial(HTTPRequest $request) |
||
| 46 | { |
||
| 47 | // Ensure this URL doesn't get picked up by HTTP caches |
||
| 48 | HTTPCacheControlMiddleware::singleton()->disableCache(); |
||
| 49 | |||
| 50 | $key = $request->param('Key'); |
||
| 51 | $token = $request->param('Token'); |
||
| 52 | if (!$key || !$token) { |
||
| 53 | return $this->httpError(404); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** @var PartialFormSubmission $partial */ |
||
| 57 | $partial = PartialFormSubmission::get()->find('Token', $token); |
||
| 58 | if (!$partial || |
||
| 59 | !$partial->UserDefinedFormID || |
||
| 60 | !hash_equals($partial->generateKey($token), $key) |
||
| 61 | ) { |
||
| 62 | return $this->httpError(404); |
||
| 63 | } |
||
| 64 | |||
| 65 | // Set the session if the last session has expired |
||
| 66 | if (!$request->getSession()->get(PartialSubmissionController::SESSION_KEY)) { |
||
| 67 | $request->getSession()->set(PartialSubmissionController::SESSION_KEY, $partial->ID); |
||
| 68 | } |
||
| 69 | |||
| 70 | // TODO: Recognize visitor with the password |
||
| 71 | // Set data record and load the form |
||
| 72 | $record = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
||
| 73 | $controller = parent::create($record); |
||
| 74 | $controller->doInit(); |
||
| 75 | $this->setFailover($this->dataRecord); |
||
| 76 | |||
| 77 | $form = $this->Form(); |
||
| 78 | $fields = $partial->PartialFields()->map('Name', 'Value')->toArray(); |
||
| 79 | $form->loadDataFrom($fields); |
||
| 80 | |||
| 81 | // Copied from {@link UserDefinedFormController} |
||
| 82 | if ($this->Content && $form && !$this->config()->disable_form_content_shortcode) { |
||
| 83 | $hasLocation = stristr($this->Content, '$UserDefinedForm'); |
||
| 84 | if ($hasLocation) { |
||
| 85 | /** @see Requirements_Backend::escapeReplacement */ |
||
| 86 | $formEscapedForRegex = addcslashes($form->forTemplate(), '\\$'); |
||
| 87 | $content = preg_replace( |
||
| 88 | '/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', |
||
| 89 | $formEscapedForRegex, |
||
| 90 | $this->Content |
||
| 91 | ); |
||
| 92 | |||
| 93 | return $this->customise([ |
||
| 94 | 'Content' => DBField::create_field('HTMLText', $content), |
||
| 95 | 'Form' => '', |
||
| 96 | 'PartialLink' => $partial->getPartialLink() |
||
| 97 | ])->renderWith([static::class, Page::class]); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | return $this->customise([ |
||
| 102 | 'Content' => DBField::create_field('HTMLText', $this->Content), |
||
| 103 | 'Form' => $form, |
||
| 104 | 'PartialLink' => $partial->getPartialLink() |
||
| 105 | ])->renderWith([static::class, Page::class]); |
||
| 106 | } |
||
| 107 | } |
||
| 108 |