/**
  * @param string $backendId
  * @param string $privateUri
  * @param string $userId
  * @param ICalendar $usersCalendar
  * @return mixed
  */
 public function scanCalendar($backendId, $privateUri, $userId, ICalendar &$usersCalendar = null)
 {
     $backend = $this->backends->find($backendId);
     if (!$backend instanceof IBackend) {
         $this->logger->debug('Backend \'' . $backendId . '\' not found');
         return null;
     }
     $calendar = $this->getRemoteAndDeleteIfNecessary($backend, $privateUri, $userId);
     if (!$calendar) {
         return null;
     }
     $cachedCalendar = $this->getCached($backendId, $privateUri, $userId);
     if ($cachedCalendar) {
         if ($usersCalendar) {
             $calendar = $this->resetUnsupportedProperties($backend, $calendar, $usersCalendar);
         } else {
             $calendar = $this->resetUnsupportedProperties($backend, $calendar, $cachedCalendar);
         }
         $cachedCalendar->overwriteWith($calendar);
         $this->updateCache($cachedCalendar);
     } else {
         CalendarUtility::generateURI($calendar, function ($newUri) use($calendar) {
             return $this->cache->doesExist($newUri, $calendar->getUserId());
         }, true);
         $calendar = $this->addToCache($calendar);
         if ($usersCalendar) {
             $usersCalendar->setId($calendar->getId());
         }
     }
 }
 /**
  * @param ICalendar $n
  * @param ICalendar $o
  * @throws Exception
  */
 private function checkForIllegalChanges(ICalendar $n, ICalendar $o)
 {
     if ($n->getUserId() !== $o->getUserId()) {
         throw new Exception('Not allowed to transfer calendar to another user!');
     }
     if ($n->getBackend()->getId() !== $o->getBackend()->getId()) {
         throw new Exception('Not allowed to transfer calendar to another backend!');
     }
     if ($n->getPublicUri() !== $o->getPublicUri()) {
         if ($this->cache->doesExist($n->getPublicUri(), $n->getUserId())) {
             throw new Exception('New URI is already assigned to another calendar!');
         }
     }
 }