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