/**
  * @param MultiTenantUserInterface $user
  * @param FormInterface $form
  *
  * @return Tenant
  * @throws \Exception
  */
 public function createTenant(MultiTenantUserInterface $user, $tenantName, $tenantSubdomain = '')
 {
     $this->logger->info(sprintf('Creating new tenant with name %s and subdomain %s', $tenantName, $tenantSubdomain));
     /** @var Tenant $tenant */
     $tenant = $this->tenantFactory->createNew();
     $tenant->setName($tenantName);
     $tenantSubdomain = $tenantSubdomain ?: $this->createSubdomainFromTenant($tenantName);
     $tenant->setSubdomain($tenantSubdomain);
     $this->entityManager->persist($tenant);
     // add as active tenant
     $user->setActiveTenant($tenant);
     $this->entityManager->flush();
     $this->tenantUserHandler->addUserToTenant($user, $tenant, array('ROLE_ADMIN'));
     $this->entityManager->flush();
     // we create a new account for gateway
     if (null !== $this->gatewayManager) {
         $this->logger->info(sprintf('Creating new account for tenant using gatewaymanager %s', get_class($this->gatewayManager)));
         $this->gatewayManager->createAccount($tenant);
     } else {
         $this->logger->info('No gatewaymanager configured');
     }
     return $tenant;
 }
 public function removeRoleFromUserInTenant($role, MultiTenantUserInterface $user, MultiTenantTenantInterface $tenant)
 {
     $tenantUser = $this->tenantUserRepository->findOneBy(array('tenant' => $tenant, 'user' => $user));
     if ($tenantUser) {
         if ($tenantUser->hasRole($role)) {
             $tenantUser->removeRole($role);
         }
         $this->entityManager->persist($tenantUser);
         $this->entityManager->flush();
         return true;
     }
     throw new \Exception(sprintf('User with id %d is not a member of tenant with id %d', $user->getId(), $tenant->getId()));
 }