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