Complex classes like Event 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Event, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Event extends Component |
||
23 | { |
||
24 | const EVENT_BEFORE_RUN = 'beforeRun'; |
||
25 | const EVENT_AFTER_RUN = 'afterRun'; |
||
26 | |||
27 | /** |
||
28 | * @var string The command to be executed. |
||
29 | */ |
||
30 | public $command; |
||
31 | |||
32 | /** |
||
33 | * @var string The crontab expression of the event's frequency. |
||
34 | */ |
||
35 | protected $_expression = '* * * * * *'; |
||
36 | |||
37 | /** |
||
38 | * @var \DateTimeZone|string The timezone the date should be evaluated. |
||
39 | */ |
||
40 | protected $_timezone; |
||
41 | |||
42 | /** |
||
43 | * @var string The user should run the command. |
||
44 | */ |
||
45 | protected $_user; |
||
46 | |||
47 | /** |
||
48 | * @var \Closure The filter callback. |
||
49 | */ |
||
50 | protected $_filter; |
||
51 | |||
52 | /** |
||
53 | * @var \Closure The reject callback. |
||
54 | */ |
||
55 | protected $_reject; |
||
56 | |||
57 | /** |
||
58 | * @var string The location that output should be send to. |
||
59 | */ |
||
60 | protected $_output; |
||
61 | |||
62 | /** |
||
63 | * @var array The array of callbacks of to be run after event is finished. |
||
64 | */ |
||
65 | protected $_afterCallbacks = []; |
||
66 | |||
67 | /** |
||
68 | * @var string The human readable description of the event. |
||
69 | */ |
||
70 | protected $_description; |
||
71 | |||
72 | /** |
||
73 | * Create a new event instance. |
||
74 | * |
||
75 | * @param string $command |
||
76 | * @param array $config |
||
77 | */ |
||
78 | 32 | public function __construct($command, $config = []) |
|
84 | |||
85 | /** |
||
86 | * Get the expression. |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | 52 | public function getExpression() |
|
94 | |||
95 | /** |
||
96 | * Set the timezone. |
||
97 | * |
||
98 | * @param \DateTimeZone|string $timezone |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | 2 | public function timezone($timezone) |
|
108 | |||
109 | /** |
||
110 | * Set the execute user. |
||
111 | * |
||
112 | * @param string $user |
||
113 | * |
||
114 | * @return $this |
||
115 | */ |
||
116 | public function user($user) |
||
122 | |||
123 | /** |
||
124 | * Set the command output message export location. |
||
125 | * |
||
126 | * @param string $path |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | 2 | public function output($path) |
|
136 | |||
137 | /** |
||
138 | * Set the description. |
||
139 | * |
||
140 | * @param string $description |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | 2 | public function description($description) |
|
150 | |||
151 | /** |
||
152 | * Get the summary of the event for display. |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | 1 | public function getSummaryForDisplay() |
|
164 | |||
165 | /** |
||
166 | * Register the callback function to execute after schedule. |
||
167 | * |
||
168 | * @param \Closure $callback |
||
169 | * |
||
170 | * @return $this |
||
171 | */ |
||
172 | public function then(\Closure $callback) |
||
178 | |||
179 | /** |
||
180 | * Register a callback to the ping a given URL after the job runs. |
||
181 | * |
||
182 | * @param string $url |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | public function thenPing($url) |
||
192 | |||
193 | /** |
||
194 | * Register a callback to further filter the schedule. |
||
195 | * |
||
196 | * @param \Closure $callback |
||
197 | * |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function when(\Closure $callback) |
||
206 | |||
207 | /** |
||
208 | * Register a callback to further filter the schedule. |
||
209 | * |
||
210 | * @param \Closure $callback |
||
211 | * |
||
212 | * @return $this |
||
213 | */ |
||
214 | public function skip(\Closure $callback) |
||
220 | |||
221 | /** |
||
222 | * Register the send email logic. |
||
223 | * |
||
224 | * @param array $address |
||
225 | * |
||
226 | * @return $this |
||
227 | */ |
||
228 | public function emailOutput($address) |
||
241 | |||
242 | /** |
||
243 | * Run the event. |
||
244 | * |
||
245 | * @param \yii\base\Application $app |
||
246 | */ |
||
247 | public function run(Application $app) |
||
257 | |||
258 | /** |
||
259 | * @param \yii\base\Application $app |
||
260 | */ |
||
261 | protected function runCommandInForeground(Application $app) |
||
268 | |||
269 | /** |
||
270 | * Call all of the "after" callbacks for the event. |
||
271 | * |
||
272 | * @param Application $app |
||
273 | */ |
||
274 | 1 | protected function callAfterCallbacks(Application $app) |
|
280 | |||
281 | /** |
||
282 | * Run the command in the background using exec. |
||
283 | * |
||
284 | * @param Application $app |
||
285 | */ |
||
286 | protected function runCommandInBackground(Application $app) |
||
291 | |||
292 | /** |
||
293 | * Determine if the given event should run based on the Cron expression. |
||
294 | * |
||
295 | * @param Application $app |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | 1 | public function isDue(Application $app) |
|
303 | |||
304 | /** |
||
305 | * Determine if the Cron expression passes. |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | 1 | protected function expressionPasses() |
|
318 | |||
319 | /** |
||
320 | * Determine if the filters pass for the event. |
||
321 | * |
||
322 | * @param Application $app |
||
323 | * |
||
324 | * @return bool |
||
325 | */ |
||
326 | 1 | protected function filtersPass(Application $app) |
|
336 | |||
337 | /** |
||
338 | * The Cron expression representing the event's frequency. |
||
339 | * |
||
340 | * @param string $expression |
||
341 | * |
||
342 | * @return $this |
||
343 | */ |
||
344 | 50 | public function cron($expression) |
|
350 | |||
351 | /** |
||
352 | * Schedule the event to run hourly. |
||
353 | * |
||
354 | * @return $this |
||
355 | */ |
||
356 | 2 | public function hourly() |
|
360 | |||
361 | /** |
||
362 | * Schedule the event to run daily. |
||
363 | * |
||
364 | * @return $this |
||
365 | */ |
||
366 | 2 | public function daily() |
|
370 | |||
371 | /** |
||
372 | * Schedule the command at a given time. |
||
373 | * |
||
374 | * @param string $time |
||
375 | * |
||
376 | * @return $this |
||
377 | */ |
||
378 | 2 | public function at($time) |
|
382 | |||
383 | /** |
||
384 | * Schedule the event to run daily at a given time (10:00, 19:30, etc). |
||
385 | * |
||
386 | * @param string $time |
||
387 | * |
||
388 | * @return $this |
||
389 | */ |
||
390 | 8 | public function dailyAt($time) |
|
397 | |||
398 | /** |
||
399 | * Schedule the event to run twice daily. |
||
400 | * |
||
401 | * @return $this |
||
402 | */ |
||
403 | 2 | public function twiceDaily() |
|
407 | |||
408 | /** |
||
409 | * Schedule the event to run only on weekdays. |
||
410 | * |
||
411 | * @return $this |
||
412 | */ |
||
413 | 2 | public function weekdays() |
|
417 | |||
418 | /** |
||
419 | * Schedule the event to run only on Mondays. |
||
420 | * |
||
421 | * @return $this |
||
422 | */ |
||
423 | 2 | public function mondays() |
|
427 | |||
428 | /** |
||
429 | * Set the days of the week the command should run on. |
||
430 | * |
||
431 | * @param array|int $days |
||
432 | * |
||
433 | * @return $this |
||
434 | */ |
||
435 | 16 | public function days($days) |
|
441 | |||
442 | /** |
||
443 | * Schedule the event to run only on Tuesdays. |
||
444 | * |
||
445 | * @return $this |
||
446 | */ |
||
447 | 2 | public function tuesdays() |
|
451 | |||
452 | /** |
||
453 | * Schedule the event to run only on Wednesdays. |
||
454 | * |
||
455 | * @return $this |
||
456 | */ |
||
457 | 2 | public function wednesdays() |
|
461 | |||
462 | /** |
||
463 | * Schedule the event to run only on Thursdays. |
||
464 | * |
||
465 | * @return $this |
||
466 | */ |
||
467 | 2 | public function thursdays() |
|
471 | |||
472 | /** |
||
473 | * Schedule the event to run only on Fridays. |
||
474 | * |
||
475 | * @return $this |
||
476 | */ |
||
477 | 2 | public function fridays() |
|
481 | |||
482 | /** |
||
483 | * Schedule the event to run only on Saturdays. |
||
484 | * |
||
485 | * @return $this |
||
486 | */ |
||
487 | 2 | public function saturdays() |
|
491 | |||
492 | /** |
||
493 | * Schedule the event to run only on Sundays. |
||
494 | * |
||
495 | * @return $this |
||
496 | */ |
||
497 | 2 | public function sundays() |
|
501 | |||
502 | /** |
||
503 | * Schedule the event to run weekly. |
||
504 | * |
||
505 | * @return $this |
||
506 | */ |
||
507 | 2 | public function weekly() |
|
511 | |||
512 | /** |
||
513 | * Schedule the event to run weekly on a given day and time. |
||
514 | * |
||
515 | * @param int $day |
||
516 | * @param string $time |
||
517 | * |
||
518 | * @return $this |
||
519 | */ |
||
520 | 4 | public function weeklyOn($day, $time = '0:0') |
|
526 | |||
527 | /** |
||
528 | * Schedule the event to run monthly. |
||
529 | * |
||
530 | * @return $this |
||
531 | */ |
||
532 | 2 | public function monthly() |
|
536 | |||
537 | /** |
||
538 | * Schedule the event to run yearly. |
||
539 | * |
||
540 | * @return $this |
||
541 | */ |
||
542 | 2 | public function yearly() |
|
546 | |||
547 | /** |
||
548 | * Schedule the event to run every minute. |
||
549 | * |
||
550 | * @return $this |
||
551 | */ |
||
552 | 2 | public function everyMinute() |
|
556 | |||
557 | /** |
||
558 | * Schedule the event to run every N minutes. |
||
559 | * |
||
560 | * @param int|string $minutes |
||
561 | * |
||
562 | * @return $this |
||
563 | */ |
||
564 | 6 | public function everyNMinutes($minutes) |
|
568 | |||
569 | /** |
||
570 | * Schedule the event to run every five minutes. |
||
571 | * |
||
572 | * @return $this |
||
573 | */ |
||
574 | 2 | public function everyFiveMinutes() |
|
578 | |||
579 | /** |
||
580 | * Schedule the event to run every ten minutes. |
||
581 | * |
||
582 | * @return $this |
||
583 | */ |
||
584 | 2 | public function everyTenMinutes() |
|
588 | |||
589 | /** |
||
590 | * Schedule the event to run every thirty minutes. |
||
591 | * |
||
592 | * @return $this |
||
593 | */ |
||
594 | 2 | public function everyThirtyMinutes() |
|
598 | |||
599 | /** |
||
600 | * Splice the given value into the given position of the expression. |
||
601 | * |
||
602 | * @param int $position |
||
603 | * @param string $value |
||
604 | * |
||
605 | * @return Event |
||
606 | */ |
||
607 | 26 | protected function spliceIntoPosition($position, $value) |
|
614 | |||
615 | /** |
||
616 | * Build the execute command. |
||
617 | * |
||
618 | * @return string |
||
619 | */ |
||
620 | 3 | public function buildCommand() |
|
626 | |||
627 | /** |
||
628 | * Send email logic. |
||
629 | * |
||
630 | * @param MailerInterface $mailer |
||
631 | * @param array $address |
||
632 | */ |
||
633 | protected function sendEmail(MailerInterface $mailer, $address) |
||
642 | |||
643 | /** |
||
644 | * Return the email subject. |
||
645 | * |
||
646 | * @return string |
||
647 | */ |
||
648 | protected function getEmailSubject() |
||
656 | |||
657 | 32 | public function getDefaultOutput() |
|
665 | } |
||
666 |