getContainer()->getPermissionsService()->currentUserCanWrite(Entities::CUSTOMERS)) { throw new AccessDeniedException('You are not allowed to perform this action!'); } $result = new CommandResult(); $this->checkMandatoryFields($command); $user = UserFactory::create($command->getFields()); if (!$user instanceof AbstractUser) { $result->setResult(CommandResult::RESULT_ERROR); $result->setMessage('Could not create a new user entity.'); return $result; } /** @var UserRepository $userRepository */ $userRepository = $this->container->get('domain.users.repository'); $userRepository->beginTransaction(); if ($userRepository->getByEmail($user->getEmail()->getValue())) { $result->setResult(CommandResult::RESULT_ERROR); $result->setMessage('Email already exist.'); return $result; } if ($userId = $userRepository->add($user)) { $user->setId(new Id($userId)); if ($command->getField('externalId') === 0) { /** @var UserApplicationService $userAS */ $userAS = $this->getContainer()->get('application.user.service'); $userAS->setWpUserIdForNewUser($userId, $user); } $result->setResult(CommandResult::RESULT_SUCCESS); $result->setMessage('Successfully added new user.'); $result->setData([ Entities::USER => $user->toArray() ]); $userRepository->commit(); return $result; } $userRepository->rollback(); return $result; } }