| Total Complexity | 40 |
| Total Lines | 414 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Schedule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Schedule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Schedule |
||
| 25 | {
|
||
| 26 | /** |
||
| 27 | * Task name |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $name; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Cron expression |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $expression; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Task callback |
||
| 40 | * @var callable|null |
||
| 41 | */ |
||
| 42 | private $callback = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Schedule constructor |
||
| 46 | * @param string $name |
||
| 47 | */ |
||
| 48 | public function __construct(string $name) |
||
| 49 | {
|
||
| 50 | $this->name = $name; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Run the task every minute |
||
| 55 | * @return self |
||
| 56 | */ |
||
| 57 | public function everyMinute(): self |
||
| 58 | {
|
||
| 59 | $this->expression = '* * * * *'; |
||
| 60 | return $this; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Run the task every five minutes |
||
| 65 | * @return self |
||
| 66 | */ |
||
| 67 | public function everyFiveMinutes(): self |
||
| 68 | {
|
||
| 69 | $this->expression = '*/5 * * * *'; |
||
| 70 | return $this; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Run the task every ten minutes |
||
| 75 | * @return self |
||
| 76 | */ |
||
| 77 | public function everyTenMinutes(): self |
||
| 78 | {
|
||
| 79 | $this->expression = '*/10 * * * *'; |
||
| 80 | return $this; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Run the task every fifteen minutes |
||
| 85 | * @return self |
||
| 86 | */ |
||
| 87 | public function everyFifteenMinutes(): self |
||
| 88 | {
|
||
| 89 | $this->expression = '*/15 * * * *'; |
||
| 90 | return $this; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Run the task every thirty minutes |
||
| 95 | * @return self |
||
| 96 | */ |
||
| 97 | public function everyThirtyMinutes(): self |
||
| 98 | {
|
||
| 99 | $this->expression = '*/30 * * * *'; |
||
| 100 | return $this; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Run the task hourly |
||
| 105 | * @return self |
||
| 106 | */ |
||
| 107 | public function hourly(): self |
||
| 108 | {
|
||
| 109 | $this->expression = '0 * * * *'; |
||
| 110 | return $this; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Run the task hourly at a specific minute |
||
| 115 | * @param int $minute |
||
| 116 | * @return self |
||
| 117 | */ |
||
| 118 | public function hourlyAt(int $minute): self |
||
| 119 | {
|
||
| 120 | $this->expression = "{$minute} * * * *";
|
||
| 121 | return $this; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Run the task every two hours |
||
| 126 | * @return self |
||
| 127 | */ |
||
| 128 | public function everyTwoHours(): self |
||
| 129 | {
|
||
| 130 | $this->expression = '0 */2 * * *'; |
||
| 131 | return $this; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Run the task every three hours |
||
| 136 | * @return self |
||
| 137 | */ |
||
| 138 | public function everyThreeHours(): self |
||
| 139 | {
|
||
| 140 | $this->expression = '0 */3 * * *'; |
||
| 141 | return $this; |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Run the task every four hours |
||
| 146 | * @return self |
||
| 147 | */ |
||
| 148 | public function everyFourHours(): self |
||
| 149 | {
|
||
| 150 | $this->expression = '0 */4 * * *'; |
||
| 151 | return $this; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Run the task every six hours |
||
| 156 | * @return self |
||
| 157 | */ |
||
| 158 | public function everySixHours(): self |
||
| 159 | {
|
||
| 160 | $this->expression = '0 */6 * * *'; |
||
| 161 | return $this; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Run the task daily |
||
| 166 | * @return self |
||
| 167 | */ |
||
| 168 | public function daily(): self |
||
| 169 | {
|
||
| 170 | $this->expression = '0 0 * * *'; |
||
| 171 | return $this; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Run the task daily at a specific time |
||
| 176 | * @param string $time Format: "HH:MM" |
||
| 177 | * @return self |
||
| 178 | */ |
||
| 179 | public function dailyAt(string $time): self |
||
| 180 | {
|
||
| 181 | [$hour, $minute] = explode(':', $time);
|
||
| 182 | $this->expression = "{$minute} {$hour} * * *";
|
||
| 183 | return $this; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Run the task twice daily |
||
| 188 | * @param int $firstHour |
||
| 189 | * @param int $secondHour |
||
| 190 | * @return self |
||
| 191 | */ |
||
| 192 | public function twiceDaily(int $firstHour = 1, int $secondHour = 13): self |
||
| 193 | {
|
||
| 194 | $this->expression = "0 {$firstHour},{$secondHour} * * *";
|
||
| 195 | return $this; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Run the task weekly |
||
| 200 | * @return self |
||
| 201 | */ |
||
| 202 | public function weekly(): self |
||
| 203 | {
|
||
| 204 | $this->expression = '0 0 * * 0'; |
||
| 205 | return $this; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Run the task weekly on a specific day and time |
||
| 210 | * @param int $dayOfWeek 0-6 (Sunday = 0) |
||
| 211 | * @param string $time Format: "HH:MM" |
||
| 212 | * @return self |
||
| 213 | */ |
||
| 214 | public function weeklyOn(int $dayOfWeek, string $time = '0:00'): self |
||
| 215 | {
|
||
| 216 | [$hour, $minute] = explode(':', $time);
|
||
| 217 | $this->expression = "{$minute} {$hour} * * {$dayOfWeek}";
|
||
| 218 | return $this; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Run the task monthly |
||
| 223 | * @return self |
||
| 224 | */ |
||
| 225 | public function monthly(): self |
||
| 226 | {
|
||
| 227 | $this->expression = '0 0 1 * *'; |
||
| 228 | return $this; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Run the task monthly on a specific day and time |
||
| 233 | * @param int $dayOfMonth |
||
| 234 | * @param string $time Format: "HH:MM" |
||
| 235 | * @return self |
||
| 236 | */ |
||
| 237 | public function monthlyOn(int $dayOfMonth = 1, string $time = '0:00'): self |
||
| 238 | {
|
||
| 239 | [$hour, $minute] = explode(':', $time);
|
||
| 240 | $this->expression = "{$minute} {$hour} {$dayOfMonth} * *";
|
||
| 241 | return $this; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Run the task twice monthly |
||
| 246 | * @param int $firstDay |
||
| 247 | * @param int $secondDay |
||
| 248 | * @param string $time |
||
| 249 | * @return self |
||
| 250 | */ |
||
| 251 | public function twiceMonthly(int $firstDay = 1, int $secondDay = 16, string $time = '0:00'): self |
||
| 252 | {
|
||
| 253 | [$hour, $minute] = explode(':', $time);
|
||
| 254 | $this->expression = "{$minute} {$hour} {$firstDay},{$secondDay} * *";
|
||
| 255 | return $this; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Run the task quarterly |
||
| 260 | * @return self |
||
| 261 | */ |
||
| 262 | public function quarterly(): self |
||
| 263 | {
|
||
| 264 | $this->expression = '0 0 1 1-12/3 *'; |
||
| 265 | return $this; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Run the task yearly |
||
| 270 | * @return self |
||
| 271 | */ |
||
| 272 | public function yearly(): self |
||
| 273 | {
|
||
| 274 | $this->expression = '0 0 1 1 *'; |
||
| 275 | return $this; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Run the task on weekdays |
||
| 280 | * @return self |
||
| 281 | */ |
||
| 282 | public function weekdays(): self |
||
| 283 | {
|
||
| 284 | $this->expression = '0 0 * * 1-5'; |
||
| 285 | return $this; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Run the task on weekends |
||
| 290 | * @return self |
||
| 291 | */ |
||
| 292 | public function weekends(): self |
||
| 293 | {
|
||
| 294 | $this->expression = '0 0 * * 0,6'; |
||
| 295 | return $this; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Run the task on Mondays |
||
| 300 | * @return self |
||
| 301 | */ |
||
| 302 | public function mondays(): self |
||
| 303 | {
|
||
| 304 | return $this->days(1); |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Run the task on Tuesdays |
||
| 309 | * @return self |
||
| 310 | */ |
||
| 311 | public function tuesdays(): self |
||
| 312 | {
|
||
| 313 | return $this->days(2); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Run the task on Wednesdays |
||
| 318 | * @return self |
||
| 319 | */ |
||
| 320 | public function wednesdays(): self |
||
| 321 | {
|
||
| 322 | return $this->days(3); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Run the task on Thursdays |
||
| 327 | * @return self |
||
| 328 | */ |
||
| 329 | public function thursdays(): self |
||
| 330 | {
|
||
| 331 | return $this->days(4); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Run the task on Fridays |
||
| 336 | * @return self |
||
| 337 | */ |
||
| 338 | public function fridays(): self |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Run the task on Saturdays |
||
| 345 | * @return self |
||
| 346 | */ |
||
| 347 | public function saturdays(): self |
||
| 348 | {
|
||
| 349 | return $this->days(6); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Run the task on Sundays |
||
| 354 | * @return self |
||
| 355 | */ |
||
| 356 | public function sundays(): self |
||
| 357 | {
|
||
| 358 | return $this->days(0); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Run the task on specific days |
||
| 363 | * @param int|array $days |
||
| 364 | * @return self |
||
| 365 | */ |
||
| 366 | public function days($days): self |
||
| 367 | {
|
||
| 368 | $days = is_array($days) ? implode(',', $days) : $days;
|
||
| 369 | $this->expression = "0 0 * * {$days}";
|
||
| 370 | return $this; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Set the time for the task |
||
| 375 | * @param string $time Format: "HH:MM" |
||
| 376 | * @return self |
||
| 377 | */ |
||
| 378 | public function at(string $time): self |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Set custom cron expression |
||
| 393 | * @param string $expression |
||
| 394 | * @return self |
||
| 395 | */ |
||
| 396 | public function cron(string $expression): self |
||
| 397 | {
|
||
| 398 | $this->expression = $expression; |
||
| 399 | return $this; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Set the callback for the task |
||
| 404 | * @param callable $callback |
||
| 405 | * @return self |
||
| 406 | */ |
||
| 407 | public function call(callable $callback): self |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Build and return the CronTask |
||
| 415 | * @return CronTask |
||
| 416 | * @throws CronException |
||
| 417 | */ |
||
| 418 | public function build(): CronTask |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Get the cron expression |
||
| 433 | * @return string|null |
||
| 434 | */ |
||
| 435 | public function getExpression(): ?string |
||
| 436 | {
|
||
| 437 | return $this->expression; |
||
| 438 | } |
||
| 439 | } |
||
| 440 |