public function unAssignUserFromUserGroup(APIUser $user, APIUserGroup $userGroup)
{
$loadedUser = $this->loadUser($user->id);
$loadedGroup = $this->loadUserGroup($userGroup->id);
$locationService = $this->repository->getLocationService();
$userLocations = $locationService->loadLocations($loadedUser->getVersionInfo()->getContentInfo());
if (empty($userLocations)) {
throw new BadStateException('user', 'user has no locations, cannot unassign from group');
}
if ($loadedGroup->getVersionInfo()->getContentInfo()->mainLocationId === null) {
throw new BadStateException('userGroup', 'user group has no main location or no locations, cannot unassign');
}
foreach ($userLocations as $userLocation) {
if ($userLocation->parentLocationId == $loadedGroup->getVersionInfo()->getContentInfo()->mainLocationId) {
// Throw this specific BadState when we know argument is valid
if (count($userLocations) === 1) {
throw new BadStateException('user', 'user only has one user group, cannot unassign from last group');
}
$this->repository->beginTransaction();
try {
$locationService->deleteLocation($userLocation);
$this->repository->commit();
return;
} catch (Exception $e) {
$this->repository->rollback();
throw $e;
}
}
}
throw new InvalidArgumentException('userGroup', 'user is not in the given user group');
}