| Conditions | 17 |
| Paths | 49 |
| Total Lines | 61 |
| Code Lines | 35 |
| 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 |
||
| 167 | public function run() |
||
| 168 | { |
||
| 169 | $this->triggerUser = User::getById($this->job->getTriggerUserId(), $this->getDatabase()); |
||
| 170 | |||
| 171 | if ($this->triggerUser === false) { |
||
|
|
|||
| 172 | throw new ApplicationLogicException('Cannot locate trigger user'); |
||
| 173 | } |
||
| 174 | |||
| 175 | $this->request = Request::getById($this->job->getRequest(), $this->getDatabase()); |
||
| 176 | |||
| 177 | if ($this->request === false) { |
||
| 178 | throw new ApplicationLogicException('Cannot locate request'); |
||
| 179 | } |
||
| 180 | |||
| 181 | if ($this->job->getEmailTemplate() !== null) { |
||
| 182 | $this->emailTemplate = EmailTemplate::getById($this->job->getEmailTemplate(), $this->getDatabase()); |
||
| 183 | |||
| 184 | if ($this->emailTemplate === false) { |
||
| 185 | throw new ApplicationLogicException('Cannot locate email template'); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | if ($this->job->getParameters() !== null) { |
||
| 190 | $this->parameters = json_decode($this->job->getParameters()); |
||
| 191 | |||
| 192 | if (json_last_error() !== JSON_ERROR_NONE) { |
||
| 193 | throw new ApplicationLogicException('JSON decode: ' . json_last_error_msg()); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | // Should we wait for a parent job? |
||
| 198 | if ($this->job->getParent() !== null) { |
||
| 199 | /** @var JobQueue $parentJob */ |
||
| 200 | $parentJob = JobQueue::getById($this->job->getParent(), $this->getDatabase()); |
||
| 201 | |||
| 202 | if ($parentJob === false) { |
||
| 203 | $this->markFailed("Parent job could not be found"); |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | |||
| 207 | switch ($parentJob->getStatus()) { |
||
| 208 | case JobQueue::STATUS_CANCELLED: |
||
| 209 | case JobQueue::STATUS_FAILED: |
||
| 210 | $this->markCancelled('Parent job failed/cancelled'); |
||
| 211 | return; |
||
| 212 | case JobQueue::STATUS_WAITING: |
||
| 213 | case JobQueue::STATUS_READY: |
||
| 214 | case JobQueue::STATUS_QUEUED: |
||
| 215 | case JobQueue::STATUS_RUNNING: |
||
| 216 | case JobQueue::STATUS_HELD: |
||
| 217 | // Defer to next execution |
||
| 218 | $this->job->setStatus(JobQueue::STATUS_READY); |
||
| 219 | $this->job->save(); |
||
| 220 | return; |
||
| 221 | case JobQueue::STATUS_COMPLETE: |
||
| 222 | // do nothing |
||
| 223 | break; |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | $this->execute(); |
||
| 228 | } |
||
| 292 |