| Conditions | 10 |
| Paths | 8 |
| Total Lines | 57 |
| 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 |
||
| 131 | public function partial(HTTPRequest $request) |
||
| 132 | { |
||
| 133 | // Ensure this URL doesn't get picked up by HTTP caches |
||
| 134 | HTTPCacheControlMiddleware::singleton()->disableCache(); |
||
| 135 | |||
| 136 | $key = $request->param('Key'); |
||
| 137 | $token = $request->param('Token'); |
||
| 138 | |||
| 139 | $partial = PartialFormSubmission::get()->find('Token', $token); |
||
| 140 | if (!$token || !$partial || !$partial->UserDefinedFormID) { |
||
| 141 | return $this->httpError(404); |
||
| 142 | } |
||
| 143 | |||
| 144 | // TODO: Recognize visitor with the password |
||
| 145 | if ($partial->generateKey($token) === $key) { |
||
| 146 | // Set the session if the last session has expired |
||
| 147 | if (!$request->getSession()->get(PartialSubmissionController::SESSION_KEY)) { |
||
| 148 | $request->getSession()->set(PartialSubmissionController::SESSION_KEY, $partial->ID); |
||
| 149 | } |
||
| 150 | |||
| 151 | // Set data record and load the form |
||
| 152 | $this->dataRecord = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
||
| 153 | $this->setFailover($this->dataRecord); |
||
| 154 | |||
| 155 | $form = $this->Form(); |
||
| 156 | $fields = $partial->PartialFields()->map('Name', 'Value')->toArray(); |
||
| 157 | $form->loadDataFrom($fields); |
||
| 158 | |||
| 159 | // Copied from {@link UserDefinedFormController} |
||
| 160 | if ($this->Content && $form && !$this->config()->disable_form_content_shortcode) { |
||
| 161 | $hasLocation = stristr($this->Content, '$UserDefinedForm'); |
||
| 162 | if ($hasLocation) { |
||
| 163 | /** @see Requirements_Backend::escapeReplacement */ |
||
| 164 | $formEscapedForRegex = addcslashes($form->forTemplate(), '\\$'); |
||
| 165 | $content = preg_replace( |
||
| 166 | '/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', |
||
| 167 | $formEscapedForRegex, |
||
| 168 | $this->Content |
||
| 169 | ); |
||
| 170 | |||
| 171 | return $this->customise([ |
||
| 172 | 'Content' => DBField::create_field('HTMLText', $content), |
||
| 173 | 'Form' => '', |
||
| 174 | 'PartialLink' => $partial->getPartialLink() |
||
| 175 | ])->renderWith([static::class, Page::class]); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | return $this->customise([ |
||
| 180 | 'Content' => DBField::create_field('HTMLText', $this->Content), |
||
| 181 | 'Form' => $form, |
||
| 182 | 'PartialLink' => $partial->getPartialLink() |
||
| 183 | ])->renderWith([static::class, Page::class]); |
||
| 184 | } else { |
||
| 185 | return $this->httpError(404); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 |