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