| Conditions | 48 |
| Paths | 5656 |
| Total Lines | 242 |
| Code Lines | 137 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 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 |
||
| 224 | public function process($data, $form) |
||
| 225 | { |
||
| 226 | $submittedForm = SubmittedForm::create(); |
||
| 227 | $submittedForm->SubmittedByID = Security::getCurrentUser() ? Security::getCurrentUser()->ID : 0; |
||
| 228 | $submittedForm->ParentClass = get_class($this->data()); |
||
| 229 | $submittedForm->ParentID = $this->ID; |
||
| 230 | |||
| 231 | // if saving is not disabled save now to generate the ID |
||
| 232 | if (!$this->DisableSaveSubmissions) { |
||
| 233 | $submittedForm->write(); |
||
| 234 | } |
||
| 235 | |||
| 236 | $attachments = []; |
||
| 237 | $submittedFields = ArrayList::create(); |
||
| 238 | |||
| 239 | foreach ($this->data()->Fields() as $field) { |
||
| 240 | if (!$field->showInReports()) { |
||
| 241 | continue; |
||
| 242 | } |
||
| 243 | |||
| 244 | $submittedField = $field->getSubmittedFormField(); |
||
| 245 | $submittedField->ParentID = $submittedForm->ID; |
||
| 246 | $submittedField->Name = $field->Name; |
||
| 247 | $submittedField->Title = $field->getField('Title'); |
||
| 248 | |||
| 249 | // save the value from the data |
||
| 250 | if ($field->hasMethod('getValueFromData')) { |
||
| 251 | $submittedField->Value = $field->getValueFromData($data); |
||
| 252 | } else { |
||
| 253 | if (isset($data[$field->Name])) { |
||
| 254 | $submittedField->Value = $data[$field->Name]; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | if (!empty($data[$field->Name])) { |
||
| 259 | if (in_array(EditableFileField::class, $field->getClassAncestry())) { |
||
| 260 | if (!empty($_FILES[$field->Name]['name'])) { |
||
| 261 | $foldername = $field->getFormField()->getFolderName(); |
||
| 262 | |||
| 263 | // create the file from post data |
||
| 264 | $upload = Upload::create(); |
||
| 265 | try { |
||
| 266 | $upload->loadIntoFile($_FILES[$field->Name], null, $foldername); |
||
| 267 | } catch (ValidationException $e) { |
||
| 268 | $validationResult = $e->getResult(); |
||
| 269 | foreach ($validationResult->getMessages() as $message) { |
||
| 270 | $form->sessionMessage($message['message'], ValidationResult::TYPE_ERROR); |
||
| 271 | } |
||
| 272 | Controller::curr()->redirectBack(); |
||
| 273 | return; |
||
| 274 | } |
||
| 275 | /** @var AssetContainer|File $file */ |
||
| 276 | $file = $upload->getFile(); |
||
| 277 | $file->ShowInSearch = 0; |
||
| 278 | $file->UserFormUpload = UserFormFileExtension::USER_FORM_UPLOAD_TRUE; |
||
| 279 | $file->write(); |
||
| 280 | |||
| 281 | // generate image thumbnail to show in asset-admin |
||
| 282 | // you can run userforms without asset-admin, so need to ensure asset-admin is installed |
||
| 283 | if (class_exists(AssetAdmin::class)) { |
||
| 284 | AssetAdmin::singleton()->generateThumbnails($file); |
||
| 285 | } |
||
| 286 | |||
| 287 | // write file to form field |
||
| 288 | $submittedField->UploadedFileID = $file->ID; |
||
| 289 | |||
| 290 | // attach a file only if lower than 1MB |
||
| 291 | if ($file->getAbsoluteSize() < 1024 * 1024 * 1) { |
||
| 292 | $attachments[] = $file; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | $submittedField->extend('onPopulationFromField', $field); |
||
| 299 | |||
| 300 | if (!$this->DisableSaveSubmissions) { |
||
| 301 | $submittedField->write(); |
||
| 302 | } |
||
| 303 | |||
| 304 | $submittedFields->push($submittedField); |
||
| 305 | } |
||
| 306 | |||
| 307 | $emailData = [ |
||
| 308 | 'Sender' => Security::getCurrentUser(), |
||
| 309 | 'HideFormData' => false, |
||
| 310 | 'Fields' => $submittedFields, |
||
| 311 | 'Body' => '', |
||
| 312 | ]; |
||
| 313 | |||
| 314 | $this->extend('updateEmailData', $emailData, $attachments); |
||
| 315 | |||
| 316 | // email users on submit. |
||
| 317 | if ($recipients = $this->FilteredEmailRecipients($data, $form)) { |
||
| 318 | foreach ($recipients as $recipient) { |
||
| 319 | $email = Email::create() |
||
| 320 | ->setHTMLTemplate('email/SubmittedFormEmail') |
||
| 321 | ->setPlainTemplate('email/SubmittedFormEmailPlain'); |
||
| 322 | |||
| 323 | // Merge fields are used for CMS authors to reference specific form fields in email content |
||
| 324 | $mergeFields = $this->getMergeFieldsMap($emailData['Fields']); |
||
| 325 | |||
| 326 | if ($attachments) { |
||
| 327 | foreach ($attachments as $file) { |
||
| 328 | /** @var File $file */ |
||
| 329 | if ((int) $file->ID === 0) { |
||
| 330 | continue; |
||
| 331 | } |
||
| 332 | |||
| 333 | $email->addAttachmentFromData( |
||
| 334 | $file->getString(), |
||
| 335 | $file->getFilename(), |
||
| 336 | $file->getMimeType() |
||
| 337 | ); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | |||
| 341 | if (!$recipient->SendPlain && $recipient->emailTemplateExists()) { |
||
| 342 | $email->setHTMLTemplate($recipient->EmailTemplate); |
||
| 343 | } |
||
| 344 | |||
| 345 | // Add specific template data for the current recipient |
||
| 346 | $emailData['HideFormData'] = (bool) $recipient->HideFormData; |
||
| 347 | // Include any parsed merge field references from the CMS editor - this is already escaped |
||
| 348 | $emailData['Body'] = SSViewer::execute_string($recipient->getEmailBodyContent(), $mergeFields); |
||
| 349 | |||
| 350 | // Push the template data to the Email's data |
||
| 351 | foreach ($emailData as $key => $value) { |
||
| 352 | $email->addData($key, $value); |
||
| 353 | } |
||
| 354 | |||
| 355 | // check to see if they are a dynamic reply to. eg based on a email field a user selected |
||
| 356 | $emailFrom = $recipient->SendEmailFromField(); |
||
| 357 | if ($emailFrom && $emailFrom->exists()) { |
||
| 358 | $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailFromField()->Name); |
||
| 359 | |||
| 360 | if ($submittedFormField && is_string($submittedFormField->Value)) { |
||
| 361 | $email->setReplyTo(explode(',', $submittedFormField->Value)); |
||
| 362 | } |
||
| 363 | } elseif ($recipient->EmailReplyTo) { |
||
| 364 | $email->setReplyTo(explode(',', $recipient->EmailReplyTo)); |
||
| 365 | } |
||
| 366 | |||
| 367 | // check for a specified from; otherwise fall back to server defaults |
||
| 368 | if ($recipient->EmailFrom) { |
||
| 369 | $email->setFrom(explode(',', $recipient->EmailFrom)); |
||
| 370 | } |
||
| 371 | |||
| 372 | // check to see if they are a dynamic reciever eg based on a dropdown field a user selected |
||
| 373 | $emailTo = $recipient->SendEmailToField(); |
||
| 374 | |||
| 375 | try { |
||
| 376 | if ($emailTo && $emailTo->exists()) { |
||
| 377 | $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name); |
||
| 378 | |||
| 379 | if ($submittedFormField && is_string($submittedFormField->Value)) { |
||
| 380 | $email->setTo(explode(',', $submittedFormField->Value)); |
||
| 381 | } else { |
||
| 382 | $email->setTo(explode(',', $recipient->EmailAddress)); |
||
| 383 | } |
||
| 384 | } else { |
||
| 385 | $email->setTo(explode(',', $recipient->EmailAddress)); |
||
| 386 | } |
||
| 387 | } catch (Swift_RfcComplianceException $e) { |
||
| 388 | // The sending address is empty and/or invalid. Log and skip sending. |
||
| 389 | $error = sprintf( |
||
| 390 | 'Failed to set sender for userform submission %s: %s', |
||
| 391 | $submittedForm->ID, |
||
| 392 | $e->getMessage() |
||
| 393 | ); |
||
| 394 | |||
| 395 | Injector::inst()->get(LoggerInterface::class)->notice($error); |
||
| 396 | |||
| 397 | continue; |
||
| 398 | } |
||
| 399 | |||
| 400 | // check to see if there is a dynamic subject |
||
| 401 | $emailSubject = $recipient->SendEmailSubjectField(); |
||
| 402 | if ($emailSubject && $emailSubject->exists()) { |
||
| 403 | $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailSubjectField()->Name); |
||
| 404 | |||
| 405 | if ($submittedFormField && trim($submittedFormField->Value)) { |
||
| 406 | $email->setSubject($submittedFormField->Value); |
||
| 407 | } else { |
||
| 408 | $email->setSubject(SSViewer::execute_string($recipient->EmailSubject, $mergeFields)); |
||
| 409 | } |
||
| 410 | } else { |
||
| 411 | $email->setSubject(SSViewer::execute_string($recipient->EmailSubject, $mergeFields)); |
||
| 412 | } |
||
| 413 | |||
| 414 | $this->extend('updateEmail', $email, $recipient, $emailData); |
||
| 415 | |||
| 416 | if ((bool)$recipient->SendPlain) { |
||
| 417 | $body = strip_tags($recipient->getEmailBodyContent()) . "\n"; |
||
| 418 | if (isset($emailData['Fields']) && !$emailData['HideFormData']) { |
||
| 419 | foreach ($emailData['Fields'] as $field) { |
||
| 420 | if ($field instanceof SubmittedFileField) { |
||
| 421 | $body .= $field->Title . ': ' . $field->ExportValue ." \n"; |
||
| 422 | } else { |
||
| 423 | $body .= $field->Title . ': ' . $field->Value . " \n"; |
||
| 424 | } |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | $email->setBody($body); |
||
| 429 | $email->sendPlain(); |
||
| 430 | } else { |
||
| 431 | $email->send(); |
||
| 432 | } |
||
| 433 | } |
||
| 434 | } |
||
| 435 | |||
| 436 | $submittedForm->extend('updateAfterProcess'); |
||
| 437 | |||
| 438 | $session = $this->getRequest()->getSession(); |
||
| 439 | $session->clear("FormInfo.{$form->FormName()}.errors"); |
||
| 440 | $session->clear("FormInfo.{$form->FormName()}.data"); |
||
| 441 | |||
| 442 | $referrer = (isset($data['Referrer'])) ? '?referrer=' . urlencode($data['Referrer']) : ""; |
||
| 443 | |||
| 444 | // set a session variable from the security ID to stop people accessing |
||
| 445 | // the finished method directly. |
||
| 446 | if (!$this->DisableAuthenicatedFinishAction) { |
||
| 447 | if (isset($data['SecurityID'])) { |
||
| 448 | $session->set('FormProcessed', $data['SecurityID']); |
||
| 449 | } else { |
||
| 450 | // if the form has had tokens disabled we still need to set FormProcessed |
||
| 451 | // to allow us to get through the finshed method |
||
| 452 | if (!$this->Form()->getSecurityToken()->isEnabled()) { |
||
| 453 | $randNum = rand(1, 1000); |
||
| 454 | $randHash = md5($randNum); |
||
| 455 | $session->set('FormProcessed', $randHash); |
||
| 456 | $session->set('FormProcessedNum', $randNum); |
||
| 457 | } |
||
| 458 | } |
||
| 459 | } |
||
| 460 | |||
| 461 | if (!$this->DisableSaveSubmissions) { |
||
| 462 | $session->set('userformssubmission'. $this->ID, $submittedForm->ID); |
||
| 463 | } |
||
| 464 | |||
| 465 | return $this->redirect($this->Link('finished') . $referrer . $this->config()->get('finished_anchor')); |
||
| 466 | } |
||
| 572 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths