| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace plunner\Console\Commands\SyncCaldav; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Console\Scheduling\Schedule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use plunner\Caldav; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class SyncCaldav extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     const BACKGROUND_MOD_MEX = 'background mode'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     const BACKGROUND_COMPLETED_MEX = 'All background tasks started'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * The name and signature of the console command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $signature = 'sync:caldav {calendarId?} {--background}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * The console command description. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $description = 'Sync caldav accounts'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var Schedule laravel schedule object needed to perform command in background | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $schedule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Create a new command instance. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param Schedule $schedule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return void | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function __construct(Schedule $schedule) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->schedule = $schedule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * Execute the console command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     public function handle() | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         // | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $calendarId = $this->argument('calendarId'); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         if(is_numeric($calendarId)) | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             $this->makeForeground(Caldav::findOrFail($calendarId)); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         else | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $this->syncAll(); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     private function syncAll() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $calendars = Caldav::all(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if($this->option('background')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             \Log::debug(self::BACKGROUND_MOD_MEX); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $this->info(self::BACKGROUND_MOD_MEX); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             foreach ($calendars as $calendar) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $this->makeBackground($calendar); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             \Log::debug(self::BACKGROUND_COMPLETED_MEX); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $this->info(self::BACKGROUND_COMPLETED_MEX); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         }else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             foreach($calendars as $calendar) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 $this->makeForeground($calendar); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * sync calendars via exec command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param Caldav $calendar | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     private function makeBackground(Caldav $calendar) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $event = $this->schedule->command('sync:caldav '.$calendar->calendar_id)->withoutOverlapping(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if($event->isDue($this->laravel)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |              $event->run($this->laravel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * sync calendars foreground | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param Caldav $calendar | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     private function makeForeground(Caldav $calendar) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $this->info('Sync calendar '. $calendar->calendar_id.' started'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         (new Sync($calendar))->sync(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->info('Sync calendar '. $calendar->calendar_id.' completed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                        
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.