/**
  * Update storage to the configuration
  *
  * @param StorageConfig $updatedStorage storage attributes
  *
  * @return StorageConfig storage config
  * @throws NotFoundException if the given storage does not exist in the config
  */
 public function updateStorage(StorageConfig $updatedStorage)
 {
     $id = $updatedStorage->getId();
     $existingMount = $this->dbConfig->getMountById($id);
     if (!is_array($existingMount)) {
         throw new NotFoundException('Storage with id "' . $id . '" not found while updating storage');
     }
     $oldStorage = $this->getStorageConfigFromDBMount($existingMount);
     $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers());
     $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups());
     $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers());
     $addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups());
     $oldUserCount = count($oldStorage->getApplicableUsers());
     $oldGroupCount = count($oldStorage->getApplicableGroups());
     $newUserCount = count($updatedStorage->getApplicableUsers());
     $newGroupCount = count($updatedStorage->getApplicableGroups());
     $wasGlobal = $oldUserCount + $oldGroupCount === 0;
     $isGlobal = $newUserCount + $newGroupCount === 0;
     foreach ($removedUsers as $user) {
         $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user);
     }
     foreach ($removedGroups as $group) {
         $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group);
     }
     foreach ($addedUsers as $user) {
         $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user);
     }
     foreach ($addedGroups as $group) {
         $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group);
     }
     if ($wasGlobal && !$isGlobal) {
         $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
     } else {
         if (!$wasGlobal && $isGlobal) {
             $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null);
         }
     }
     $changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions());
     $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions());
     foreach ($changedConfig as $key => $value) {
         $this->dbConfig->setConfig($id, $key, $value);
     }
     foreach ($changedOptions as $key => $value) {
         $this->dbConfig->setOption($id, $key, $value);
     }
     if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) {
         $this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint());
     }
     if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) {
         $this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier());
     }
     $this->triggerChangeHooks($oldStorage, $updatedStorage);
     if ($wasGlobal && !$isGlobal || count($removedGroups) > 0) {
         // to expensive to properly handle these on the fly
         $this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage));
     } else {
         $storageId = $this->getStorageId($updatedStorage);
         foreach ($removedUsers as $userId) {
             $this->userMountCache->removeUserStorageMount($storageId, $userId);
         }
     }
     return $this->getStorage($id);
 }
 /**
  * Cache mounts for user
  *
  * @param IUser $user
  * @param IMountPoint[] $mountPoints
  */
 public function registerMounts(IUser $user, array $mountPoints)
 {
     $this->mountCache->registerMounts($user, $mountPoints);
 }