private function processOneCalendar(Google_Service_Calendar_CalendarListEntry $calendarListEntry)
 {
     $this->info('processing this calendar:: ' . $calendarListEntry->getId() . ' ' . $calendarListEntry->getSummary());
     //if calendar is deleted, softDelete else just save the
     try {
         $calendar = Calendar::where('calendar_id', $calendarListEntry->getId())->firstOrFail();
         $this->comment('Calendar found');
     } catch (ModelNotFoundException $e) {
         if ($calendarListEntry->getDeleted()) {
             $this->comment('Calendar doesn\\`t exist anyway' . print_r($calendarListEntry->getDeleted(), true));
             return;
         }
         $this->comment('New Calendar!');
         $calendar = new Calendar();
     }
     if ($calendarListEntry->getDeleted()) {
         $this->comment('Deleting Calendar');
         $calendar->delete();
         return;
     }
     $calendar->foreground_colour = (string) $calendarListEntry->getForegroundColor();
     $calendar->background_colour = (string) $calendarListEntry->getBackgroundColor();
     $calendar->etag = $calendarListEntry->getEtag();
     $calendar->calendar_id = $calendarListEntry->getId();
     $calendar->summary = (string) $calendarListEntry->getSummary();
     $calendar->description = (string) $calendarListEntry->getDescription();
     $calendar->save();
     $this->comment('Calendar Saved');
 }
 public function fire()
 {
     $startTime = microtime(true);
     $years_forward = $this->settings->years_forward;
     if (!is_numeric($years_forward) || $years_forward <= 0) {
         $years_forward = self::DEFAULT_YEARS_FORWARD;
     }
     $years_back = $this->settings->years_back;
     if (!is_numeric($years_back) || $years_back <= 0) {
         $years_back = self::DEFAULT_YEARS_BACK;
     }
     $timeMax = new \DateTime('+' . $years_forward . ' years');
     $timeMin = new \DateTime('-' . $years_back . ' years');
     if (boolval($this->settings->delete_old_events)) {
         $this->info('N.B. At the end of this script, all events with a start date before ' . $timeMin->format('Y-m-d H:i:s') . ' will be HARD deleted');
         $this->comment('They will still exist on your google calendar, but they will not be on the website any more');
         $this->comment('If that\'s not something you want, press Ctrl+C *now*');
     }
     //shuffle sometimes in case we get a crash in one calendar somehow..
     $calendars = Calendar::where('enabled', '=', 1)->orderBy('synced_at', 'ASC')->get();
     if (rand(1, 8) == 2) {
         $calendars->shuffle();
     }
     foreach ($calendars as $calendar) {
         $calendarStartProcessingTime = microtime(true);
         $this->info('processing ' . $calendar->calendar_id);
         /**
          * Full sync will occasionally happen on the calendars, just in case...
          */
         $newSyncDate = new \DateTime('now');
         $calendarUpdatedOk = $this->updateEventsForCalendar($calendar, $timeMin, $timeMax, rand(1, 10) == 4);
         if ($calendarUpdatedOk) {
             $calendar->synced_at = $newSyncDate;
             //Assign this to the calendar AFTER the syncing
             $calendar->save();
         }
         $calendarEndProcessingTime = microtime(true);
         $this->displayTimeDiff('Time to sync calendar', $calendarStartProcessingTime, $calendarEndProcessingTime);
     }
     if (boolval($this->settings->delete_old_events)) {
         $this->info('Deleting events before ' . $timeMin->format('Y-m-d H:i:s'));
         Event::where('start_date', '<', $timeMin)->delete();
     }
     $this->displayTimeDiff('Time to sync all calendars', $startTime, microtime(true));
 }