| Conditions | 8 |
| Paths | 8 |
| Total Lines | 62 |
| 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 |
||
| 101 | public function create($server, $user, $data) |
||
| 102 | { |
||
| 103 | $server = Server::findOrFail($server); |
||
| 104 | $user = User::findOrFail($user); |
||
| 105 | |||
| 106 | $validator = Validator::make($data, [ |
||
| 107 | 'action' => 'string|required', |
||
| 108 | 'data' => 'string|required', |
||
| 109 | 'year' => 'string|sometimes', |
||
| 110 | 'day_of_week' => 'string|sometimes', |
||
| 111 | 'month' => 'string|sometimes', |
||
| 112 | 'day_of_month' => 'string|sometimes', |
||
| 113 | 'hour' => 'string|sometimes', |
||
| 114 | 'minute' => 'string|sometimes', |
||
| 115 | ]); |
||
| 116 | |||
| 117 | if ($validator->fails()) { |
||
| 118 | throw new DisplayValidationException(json_encode($validator->errors())); |
||
| 119 | } |
||
| 120 | |||
| 121 | if (! in_array($data['action'], $this->actions)) { |
||
| 122 | throw new DisplayException('The action provided is not valid.'); |
||
| 123 | } |
||
| 124 | |||
| 125 | $cron = $this->defaults; |
||
| 126 | foreach ($this->defaults as $setting => $value) { |
||
| 127 | if (array_key_exists($setting, $data) && ! is_null($data[$setting]) && $data[$setting] !== '') { |
||
| 128 | $cron[$setting] = $data[$setting]; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | // Check that is this a valid Cron Entry |
||
| 133 | try { |
||
| 134 | $buildCron = Cron::factory(sprintf('%s %s %s %s %s %s', |
||
| 135 | $cron['minute'], |
||
| 136 | $cron['hour'], |
||
| 137 | $cron['day_of_month'], |
||
| 138 | $cron['month'], |
||
| 139 | $cron['day_of_week'], |
||
| 140 | $cron['year'] |
||
| 141 | )); |
||
| 142 | } catch (\Exception $ex) { |
||
| 143 | throw $ex; |
||
| 144 | } |
||
| 145 | |||
| 146 | return Task::create([ |
||
| 147 | 'user_id' => $user->id, |
||
| 148 | 'server_id' => $server->id, |
||
| 149 | 'active' => 1, |
||
| 150 | 'action' => $data['action'], |
||
| 151 | 'data' => $data['data'], |
||
| 152 | 'queued' => 0, |
||
| 153 | 'year' => $cron['year'], |
||
| 154 | 'day_of_week' => $cron['day_of_week'], |
||
| 155 | 'month' => $cron['month'], |
||
| 156 | 'day_of_month' => $cron['day_of_month'], |
||
| 157 | 'hour' => $cron['hour'], |
||
| 158 | 'minute' => $cron['minute'], |
||
| 159 | 'last_run' => null, |
||
| 160 | 'next_run' => $buildCron->getNextRunDate(), |
||
| 161 | ]); |
||
| 162 | } |
||
| 163 | } |
||
| 164 |