| Conditions | 5 |
| Paths | 7 |
| Total Lines | 67 |
| Code Lines | 45 |
| 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 |
||
| 81 | public function process() { |
||
| 82 | $statusList = implode('\', \'', $this->config()->cleanup_statuses); |
||
| 83 | switch($this->config()->cleanup_method) { |
||
| 84 | // If Age, we need to get jobs that are at least n days old |
||
| 85 | case "age": |
||
| 86 | $cutOff = date("Y-m-d H:i:s", |
||
| 87 | strtotime(SS_Datetime::now() . |
||
| 88 | " - " . |
||
| 89 | $this->config()->cleanup_value . |
||
| 90 | " days" |
||
| 91 | ) |
||
| 92 | ); |
||
| 93 | $stale = DB::query( |
||
| 94 | 'SELECT "ID" |
||
| 95 | FROM "QueuedJobDescriptor" |
||
| 96 | WHERE "JobStatus" |
||
| 97 | IN (\'' . $statusList . '\') |
||
| 98 | AND "LastEdited" < \'' . $cutOff .'\'' |
||
| 99 | ); |
||
| 100 | $staleJobs = $stale->column("ID"); |
||
| 101 | break; |
||
| 102 | // If Number, we need to save n records, then delete from the rest |
||
| 103 | case "number": |
||
| 104 | $fresh = DB::query( |
||
| 105 | 'SELECT "ID" |
||
| 106 | FROM "QueuedJobDescriptor" |
||
| 107 | ORDER BY "LastEdited" |
||
| 108 | ASC LIMIT ' . $this->config()->cleanup_value |
||
| 109 | ); |
||
| 110 | $freshJobIDs = implode('\', \'', $fresh->column("ID")); |
||
| 111 | |||
| 112 | $stale = DB::query( |
||
| 113 | 'SELECT "ID" |
||
| 114 | FROM "QueuedJobDescriptor" |
||
| 115 | WHERE "ID" |
||
| 116 | NOT IN (\'' . $freshJobIDs . '\') |
||
| 117 | AND "JobStatus" |
||
| 118 | IN (\'' . $statusList . '\')' |
||
| 119 | ); |
||
| 120 | $staleJobs = $stale->column("ID"); |
||
| 121 | break; |
||
| 122 | default: |
||
| 123 | $this->addMessage("Incorrect configuration values set. Cleanup ignored"); |
||
| 124 | $this->isComplete = true; |
||
| 125 | return; |
||
| 126 | } |
||
| 127 | if (empty($staleJobs)) { |
||
| 128 | $this->addMessage("No jobs to clean up."); |
||
| 129 | $this->isComplete = true; |
||
| 130 | return; |
||
| 131 | } |
||
| 132 | $numJobs = count($staleJobs); |
||
| 133 | $staleJobs = implode('\', \'', $staleJobs); |
||
| 134 | DB::query('DELETE FROM "QueuedJobDescriptor" |
||
| 135 | WHERE "ID" |
||
| 136 | IN (\'' . $staleJobs . '\')' |
||
| 137 | ); |
||
| 138 | $this->addMessage($numJobs . " jobs cleaned up."); |
||
| 139 | // let's make sure there is a cleanupJob in the queue |
||
| 140 | if (Config::inst()->get('CleanupJob', 'is_enabled')) { |
||
| 141 | $this->addMessage("Queueing the next Cleanup Job."); |
||
| 142 | $cleanup = new CleanupJob(); |
||
| 143 | singleton('QueuedJobService')->queueJob($cleanup, date('Y-m-d H:i:s', time() + 86400)); |
||
| 144 | } |
||
| 145 | $this->isComplete = true; |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | } |
||
| 149 |
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.