Conditions | 40 |
Paths | 4108 |
Total Lines | 196 |
Code Lines | 110 |
Lines | 21 |
Ratio | 10.71 % |
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 |
||
204 | public function process($data, $form) |
||
205 | { |
||
206 | $submittedForm = SubmittedForm::create(); |
||
207 | $submittedForm->SubmittedByID = Security::getCurrentUser() ? Security::getCurrentUser()->ID : 0; |
||
208 | $submittedForm->ParentID = $this->ID; |
||
209 | |||
210 | // if saving is not disabled save now to generate the ID |
||
211 | if (!$this->DisableSaveSubmissions) { |
||
212 | $submittedForm->write(); |
||
213 | } |
||
214 | |||
215 | $attachments = array(); |
||
216 | $submittedFields = ArrayList::create(); |
||
217 | |||
218 | foreach ($this->Fields() as $field) { |
||
219 | if (!$field->showInReports()) { |
||
220 | continue; |
||
221 | } |
||
222 | |||
223 | $submittedField = $field->getSubmittedFormField(); |
||
224 | $submittedField->ParentID = $submittedForm->ID; |
||
225 | $submittedField->Name = $field->Name; |
||
226 | $submittedField->Title = $field->getField('Title'); |
||
227 | |||
228 | // save the value from the data |
||
229 | if ($field->hasMethod('getValueFromData')) { |
||
230 | $submittedField->Value = $field->getValueFromData($data); |
||
231 | } else { |
||
232 | if (isset($data[$field->Name])) { |
||
233 | $submittedField->Value = $data[$field->Name]; |
||
234 | } |
||
235 | } |
||
236 | |||
237 | if (!empty($data[$field->Name])) { |
||
238 | if (in_array(EditableFileField::class, $field->getClassAncestry())) { |
||
239 | if (!empty($_FILES[$field->Name]['name'])) { |
||
240 | $foldername = $field->getFormField()->getFolderName(); |
||
241 | |||
242 | // create the file from post data |
||
243 | $upload = Upload::create(); |
||
244 | $file = File::create(); |
||
245 | $file->ShowInSearch = 0; |
||
246 | try { |
||
247 | $upload->loadIntoFile($_FILES[$field->Name], $file, $foldername); |
||
248 | } catch (ValidationException $e) { |
||
249 | $validationResult = $e->getResult(); |
||
250 | $form->addErrorMessage($field->Name, $validationResult->message(), 'bad'); |
||
251 | Controller::curr()->redirectBack(); |
||
252 | return; |
||
253 | } |
||
254 | |||
255 | // write file to form field |
||
256 | $submittedField->UploadedFileID = $file->ID; |
||
257 | |||
258 | // attach a file only if lower than 1MB |
||
259 | if ($file->getAbsoluteSize() < 1024 * 1024 * 1) { |
||
260 | $attachments[] = $file; |
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | } |
||
265 | |||
266 | $submittedField->extend('onPopulationFromField', $field); |
||
267 | |||
268 | if (!$this->DisableSaveSubmissions) { |
||
269 | $submittedField->write(); |
||
270 | } |
||
271 | |||
272 | $submittedFields->push($submittedField); |
||
273 | } |
||
274 | |||
275 | $emailData = [ |
||
276 | 'Sender' => Security::getCurrentUser(), |
||
277 | 'Fields' => $submittedFields |
||
278 | ]; |
||
279 | |||
280 | $this->extend('updateEmailData', $emailData, $attachments); |
||
281 | |||
282 | // email users on submit. |
||
283 | if ($recipients = $this->FilteredEmailRecipients($data, $form)) { |
||
284 | foreach ($recipients as $recipient) { |
||
285 | $email = Email::create() |
||
286 | ->setHTMLTemplate('email/SubmittedFormEmail.ss') |
||
287 | ->setPlainTemplate('email/SubmittedFormEmail.ss'); |
||
288 | |||
289 | $mergeFields = $this->getMergeFieldsMap($emailData['Fields']); |
||
290 | |||
291 | if ($attachments) { |
||
292 | foreach ($attachments as $file) { |
||
293 | if (!$file->ID != 0) { |
||
294 | continue; |
||
295 | } |
||
296 | |||
297 | $email->attachFile( |
||
298 | $file->Filename, |
||
299 | $file->Filename, |
||
300 | HTTP::get_mime_type($file->Filename) |
||
301 | ); |
||
302 | } |
||
303 | } |
||
304 | |||
305 | $parsedBody = SSViewer::execute_string($recipient->getEmailBodyContent(), $mergeFields); |
||
306 | |||
307 | if (!$recipient->SendPlain && $recipient->emailTemplateExists()) { |
||
308 | $email->setHTMLTemplate($recipient->EmailTemplate); |
||
309 | } |
||
310 | |||
311 | $email->setData($recipient); |
||
312 | foreach ($emailData as $key => $value) { |
||
313 | $email->addData($key, $value); |
||
314 | } |
||
315 | |||
316 | $email->setFrom($recipient->EmailFrom); |
||
317 | $email->setBody($parsedBody); |
||
318 | $email->setTo($recipient->EmailAddress); |
||
319 | $email->setSubject($recipient->EmailSubject); |
||
320 | |||
321 | if ($recipient->EmailReplyTo) { |
||
322 | $email->setReplyTo($recipient->EmailReplyTo); |
||
323 | } |
||
324 | |||
325 | // check to see if they are a dynamic reply to. eg based on a email field a user selected |
||
326 | View Code Duplication | if ($recipient->SendEmailFromField()) { |
|
327 | $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailFromField()->Name); |
||
328 | |||
329 | if ($submittedFormField && is_string($submittedFormField->Value)) { |
||
330 | $email->setReplyTo($submittedFormField->Value); |
||
331 | } |
||
332 | } |
||
333 | // check to see if they are a dynamic reciever eg based on a dropdown field a user selected |
||
334 | View Code Duplication | if ($recipient->SendEmailToField()) { |
|
335 | $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name); |
||
336 | |||
337 | if ($submittedFormField && is_string($submittedFormField->Value)) { |
||
338 | $email->setTo($submittedFormField->Value); |
||
339 | } |
||
340 | } |
||
341 | |||
342 | // check to see if there is a dynamic subject |
||
343 | View Code Duplication | if ($recipient->SendEmailSubjectField()) { |
|
344 | $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailSubjectField()->Name); |
||
345 | |||
346 | if ($submittedFormField && trim($submittedFormField->Value)) { |
||
347 | $email->setSubject($submittedFormField->Value); |
||
348 | } |
||
349 | } |
||
350 | |||
351 | $this->extend('updateEmail', $email, $recipient, $emailData); |
||
352 | |||
353 | if ($recipient->SendPlain) { |
||
354 | $body = strip_tags($recipient->getEmailBodyContent()) . "\n"; |
||
355 | if (isset($emailData['Fields']) && !$recipient->HideFormData) { |
||
356 | foreach ($emailData['Fields'] as $Field) { |
||
357 | $body .= $Field->Title . ': ' . $Field->Value . " \n"; |
||
358 | } |
||
359 | } |
||
360 | |||
361 | $email->setBody($body); |
||
362 | $email->sendPlain(); |
||
363 | } else { |
||
364 | $email->send(); |
||
365 | } |
||
366 | } |
||
367 | } |
||
368 | |||
369 | $submittedForm->extend('updateAfterProcess'); |
||
370 | |||
371 | $session = $this->getRequest()->getSession(); |
||
372 | $session->clear("FormInfo.{$form->FormName()}.errors"); |
||
373 | $session->clear("FormInfo.{$form->FormName()}.data"); |
||
374 | |||
375 | $referrer = (isset($data['Referrer'])) ? '?referrer=' . urlencode($data['Referrer']) : ""; |
||
376 | |||
377 | // set a session variable from the security ID to stop people accessing |
||
378 | // the finished method directly. |
||
379 | if (!$this->DisableAuthenicatedFinishAction) { |
||
380 | if (isset($data['SecurityID'])) { |
||
381 | $session->set('FormProcessed', $data['SecurityID']); |
||
382 | } else { |
||
383 | // if the form has had tokens disabled we still need to set FormProcessed |
||
384 | // to allow us to get through the finshed method |
||
385 | if (!$this->Form()->getSecurityToken()->isEnabled()) { |
||
386 | $randNum = rand(1, 1000); |
||
387 | $randHash = md5($randNum); |
||
388 | $session->set('FormProcessed', $randHash); |
||
389 | $session->set('FormProcessedNum', $randNum); |
||
390 | } |
||
391 | } |
||
392 | } |
||
393 | |||
394 | if (!$this->DisableSaveSubmissions) { |
||
395 | $session->set('userformssubmission'. $this->ID, $submittedForm->ID); |
||
396 | } |
||
397 | |||
398 | return $this->redirect($this->Link('finished') . $referrer . $this->config()->get('finished_anchor')); |
||
399 | } |
||
400 | |||
502 |
This check marks private properties in classes that are never used. Those properties can be removed.