| Conditions | 5 |
| Paths | 32 |
| Total Lines | 52 |
| Code Lines | 39 |
| 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 |
||
| 64 | public function handle() |
||
| 65 | { |
||
| 66 | $time = Carbon::now(); |
||
| 67 | $log = new TaskLog; |
||
| 68 | |||
| 69 | if ($this->attempts() >= 1) { |
||
| 70 | // Just delete the job, we will attempt it again later anyways. |
||
| 71 | $this->delete(); |
||
| 72 | } |
||
| 73 | |||
| 74 | try { |
||
| 75 | if ($this->task->action === 'command') { |
||
| 76 | $repo = new CommandRepository($this->task->server, $this->task->user); |
||
| 77 | $response = $repo->send($this->task->data); |
||
| 78 | } elseif ($this->task->action === 'power') { |
||
| 79 | $repo = new PowerRepository($this->task->server, $this->task->user); |
||
| 80 | $response = $repo->do($this->task->data); |
||
| 81 | } else { |
||
| 82 | throw new \Exception('Task type provided was not valid.'); |
||
| 83 | } |
||
| 84 | |||
| 85 | $log->fill([ |
||
| 86 | 'task_id' => $this->task->id, |
||
| 87 | 'run_time' => $time, |
||
| 88 | 'run_status' => 0, |
||
| 89 | 'response' => $response, |
||
| 90 | ]); |
||
| 91 | } catch (\Exception $ex) { |
||
| 92 | $log->fill([ |
||
| 93 | 'task_id' => $this->task->id, |
||
| 94 | 'run_time' => $time, |
||
| 95 | 'run_status' => 1, |
||
| 96 | 'response' => $ex->getMessage(), |
||
| 97 | ]); |
||
| 98 | } finally { |
||
| 99 | $cron = Cron::factory(sprintf('%s %s %s %s %s %s', |
||
| 100 | $this->task->minute, |
||
| 101 | $this->task->hour, |
||
| 102 | $this->task->day_of_month, |
||
| 103 | $this->task->month, |
||
| 104 | $this->task->day_of_week, |
||
| 105 | $this->task->year |
||
| 106 | )); |
||
| 107 | $this->task->fill([ |
||
| 108 | 'last_run' => $time, |
||
| 109 | 'next_run' => $cron->getNextRunDate(), |
||
| 110 | 'queued' => false, |
||
| 111 | ]); |
||
| 112 | $this->task->save(); |
||
| 113 | $log->save(); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.