| Conditions | 8 |
| Paths | 7 |
| Total Lines | 62 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 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 |
||
| 38 | public function process() { |
||
| 39 | $sent = 0; |
||
| 40 | $filter = array( |
||
| 41 | 'WorkflowStatus' => array('Active', 'Paused'), |
||
| 42 | 'Definition.RemindDays:GreaterThan' => 0 |
||
| 43 | ); |
||
| 44 | |||
| 45 | $active = WorkflowInstance::get()->filter($filter); |
||
| 46 | |||
| 47 | foreach ($active as $instance) { |
||
| 48 | $edited = strtotime($instance->LastEdited); |
||
| 49 | $days = $instance->Definition()->RemindDays; |
||
| 50 | |||
| 51 | if ($edited + ($days * 3600 * 24) > time()) { |
||
| 52 | continue; |
||
| 53 | } |
||
| 54 | |||
| 55 | $email = new Email(); |
||
| 56 | $bcc = ''; |
||
| 57 | $members = $instance->getAssignedMembers(); |
||
| 58 | $target = $instance->getTarget(); |
||
| 59 | |||
| 60 | if (!$members || !count($members)) { |
||
| 61 | continue; |
||
| 62 | } |
||
| 63 | |||
| 64 | $email->setSubject("Workflow Reminder: $instance->Title"); |
||
| 65 | $email->setBcc(implode(', ', $members->column('Email'))); |
||
| 66 | $email->setTemplate('WorkflowReminderEmail'); |
||
| 67 | $email->populateTemplate(array( |
||
| 68 | 'Instance' => $instance, |
||
| 69 | 'Link' => $target instanceof SiteTree ? "admin/show/$target->ID" : '' |
||
| 70 | )); |
||
| 71 | |||
| 72 | $email->send(); |
||
| 73 | $sent++; |
||
| 74 | |||
| 75 | // add a comment to the workflow if possible |
||
| 76 | $action = $instance->CurrentAction(); |
||
| 77 | |||
| 78 | $currentComment = $action->Comment; |
||
| 79 | $action->Comment = sprintf(_t('AdvancedWorkflow.JOB_REMINDER_COMMENT', '%s: Reminder email sent\n\n'), date('Y-m-d H:i:s')) . $currentComment; |
||
| 80 | try { |
||
| 81 | $action->write(); |
||
| 82 | } catch (Exception $ex) { |
||
| 83 | SS_Log::log($ex, SS_Log::WARN); |
||
| 84 | } |
||
| 85 | |||
| 86 | $instance->LastEdited = time(); |
||
| 87 | try { |
||
| 88 | $instance->write(); |
||
| 89 | } catch (Exception $ex) { |
||
| 90 | SS_Log::log($ex, SS_Log::WARN); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->currentStep = 2; |
||
| 95 | $this->isComplete = true; |
||
| 96 | |||
| 97 | $nextDate = date('Y-m-d H:i:s', time() + $this->repeatInterval); |
||
| 98 | $this->queuedJobService->queueJob(new WorkflowReminderJob($this->repeatInterval), $nextDate); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.