Conditions | 11 |
Paths | 8 |
Total Lines | 60 |
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 | $this->dataRecord = DataObject::get_by_id($partial->UserDefinedFormClass, $partial->UserDefinedFormID); |
||
73 | $this->setFailover($this->dataRecord); |
||
74 | |||
75 | $form = $this->Form(); |
||
76 | $fields = $partial->PartialFields()->map('Name', 'Value')->toArray(); |
||
77 | $form->loadDataFrom($fields); |
||
78 | |||
79 | // Copied from {@link UserDefinedFormController} |
||
80 | if ($this->Content && $form && !$this->config()->disable_form_content_shortcode) { |
||
81 | $hasLocation = stristr($this->Content, '$UserDefinedForm'); |
||
82 | if ($hasLocation) { |
||
83 | /** @see Requirements_Backend::escapeReplacement */ |
||
84 | $formEscapedForRegex = addcslashes($form->forTemplate(), '\\$'); |
||
85 | $content = preg_replace( |
||
86 | '/(<p[^>]*>)?\\$UserDefinedForm(<\\/p>)?/i', |
||
87 | $formEscapedForRegex, |
||
88 | $this->Content |
||
89 | ); |
||
90 | |||
91 | return $this->customise([ |
||
92 | 'Content' => DBField::create_field('HTMLText', $content), |
||
93 | 'Form' => '', |
||
94 | 'PartialLink' => $partial->getPartialLink() |
||
95 | ])->renderWith([static::class, Page::class]); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | return $this->customise([ |
||
100 | 'Content' => DBField::create_field('HTMLText', $this->Content), |
||
101 | 'Form' => $form, |
||
102 | 'PartialLink' => $partial->getPartialLink() |
||
103 | ])->renderWith([static::class, Page::class]); |
||
104 | } |
||
105 | } |
||
106 |