| Conditions | 19 |
| Paths | 3592 |
| Total Lines | 111 |
| Code 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 |
||
| 187 | public function doPostComment($data, $form) |
||
| 188 | { |
||
| 189 | // Load class and parent from data |
||
| 190 | if (isset($data['ParentClassName'])) { |
||
| 191 | $this->controller->setParentClass($data['ParentClassName']); |
||
| 192 | } |
||
| 193 | if (isset($data['ParentID']) && ($class = $this->controller->getParentClass())) { |
||
| 194 | $this->controller->setOwnerRecord($class::get()->byID($data['ParentID'])); |
||
| 195 | } |
||
| 196 | if (!$this->controller->getOwnerRecord()) { |
||
| 197 | return $this->getRequestHandler()->httpError(404); |
||
| 198 | } |
||
| 199 | |||
| 200 | // cache users data |
||
| 201 | $form->setSessionData([ |
||
| 202 | 'UserData' => json_encode($data), |
||
| 203 | 'Comment' => $data['Comment'] |
||
| 204 | ]); |
||
| 205 | |||
| 206 | // extend hook to allow extensions. Also see onAfterPostComment |
||
| 207 | $this->controller->extend('onBeforePostComment', $form); |
||
| 208 | |||
| 209 | // If commenting can only be done by logged in users, make sure the user is logged in |
||
| 210 | if (!$this->controller->getOwnerRecord()->canPostComment()) { |
||
| 211 | return Security::permissionFailure( |
||
| 212 | $this->controller, |
||
| 213 | _t( |
||
| 214 | 'SilverStripe\\Comments\\Controllers\\CommentingController.PERMISSIONFAILURE', |
||
| 215 | "You're not able to post comments to this page. Please ensure you are logged in and have an " |
||
| 216 | . 'appropriate permission level.' |
||
| 217 | ) |
||
| 218 | ); |
||
| 219 | } |
||
| 220 | |||
| 221 | if ($member = Security::getCurrentUser()) { |
||
| 222 | $form->Fields()->push(HiddenField::create('AuthorID', 'Author ID', $member->ID)); |
||
| 223 | } |
||
| 224 | |||
| 225 | // What kind of moderation is required? |
||
| 226 | switch ($this->controller->getOwnerRecord()->ModerationRequired) { |
||
| 227 | case 'Required': |
||
| 228 | $requireModeration = true; |
||
| 229 | break; |
||
| 230 | case 'NonMembersOnly': |
||
| 231 | $requireModeration = empty($member); |
||
| 232 | break; |
||
| 233 | case 'None': |
||
| 234 | default: |
||
| 235 | $requireModeration = false; |
||
| 236 | break; |
||
| 237 | } |
||
| 238 | |||
| 239 | $comment = Comment::create(); |
||
| 240 | $form->saveInto($comment); |
||
| 241 | |||
| 242 | $comment->ParentID = $data['ParentID']; |
||
| 243 | $comment->ParentClass = $data['ParentClassName']; |
||
| 244 | |||
| 245 | $comment->AllowHtml = $this->controller->getOption('html_allowed'); |
||
| 246 | $comment->Moderated = !$requireModeration; |
||
| 247 | |||
| 248 | // Save into DB, or call pre-save hooks to give accurate preview |
||
| 249 | $usePreview = $this->controller->getOption('use_preview'); |
||
| 250 | $isPreview = $usePreview && !empty($data['IsPreview']); |
||
| 251 | if ($isPreview) { |
||
| 252 | $comment->extend('onBeforeWrite'); |
||
| 253 | } else { |
||
| 254 | $comment->write(); |
||
| 255 | |||
| 256 | // extend hook to allow extensions. Also see onBeforePostComment |
||
| 257 | $this->controller->extend('onAfterPostComment', $comment); |
||
| 258 | } |
||
| 259 | |||
| 260 | // we want to show a notification if comments are moderated |
||
| 261 | if ($requireModeration && !$comment->IsSpam) { |
||
| 262 | $this->getRequest()->getSession()->set('CommentsModerated', 1); |
||
| 263 | } |
||
| 264 | |||
| 265 | // clear the users comment since the comment was successful. |
||
| 266 | if ($comment->exists()) { |
||
| 267 | // Remove the comment data as it's been saved already. |
||
| 268 | unset($data['Comment']); |
||
| 269 | } |
||
| 270 | |||
| 271 | // cache users data (name, email, etc to prepopulate on other forms). |
||
| 272 | $form->setSessionData([ |
||
| 273 | 'UserData' => json_encode($data), |
||
| 274 | ]); |
||
| 275 | |||
| 276 | // Find parent link |
||
| 277 | if (!empty($data['ReturnURL'])) { |
||
| 278 | $url = $data['ReturnURL']; |
||
| 279 | } elseif ($parent = $comment->Parent()) { |
||
| 280 | $url = $parent->Link(); |
||
| 281 | } else { |
||
| 282 | return $this->controller->redirectBack(); |
||
| 283 | } |
||
| 284 | |||
| 285 | // Given a redirect page exists, attempt to link to the correct anchor |
||
| 286 | if ($comment->IsSpam) { |
||
| 287 | // Link to the form with the error message contained |
||
| 288 | $hash = $form->FormName(); |
||
| 289 | } elseif (!$comment->Moderated) { |
||
| 290 | // Display the "awaiting moderation" text |
||
| 291 | $hash = 'moderated'; |
||
| 292 | } else { |
||
| 293 | // Link to the moderated, non-spam comment |
||
| 294 | $hash = $comment->Permalink(); |
||
| 295 | } |
||
| 296 | |||
| 297 | return $this->controller->redirect(Controller::join_links($url, "#{$hash}")); |
||
| 298 | } |
||
| 300 |