getContainer()->getPermissionsService()->currentUserCanDelete(Entities::EMPLOYEES) && !$this->getContainer()->getPermissionsService()->currentUserCanDelete(Entities::CUSTOMERS) ) { throw new AccessDeniedException('You are not allowed to read user'); } $result = new CommandResult(); /** @var UserApplicationService $userAS */ $userAS = $this->getContainer()->get('application.user.service'); /** @var AppointmentRepository $appointmentRepo */ $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); $appointmentsCount = $userAS->getAppointmentsCountForUser($command->getArg('id')); /** @var UserRepository $userRepository */ $userRepository = $this->container->get('domain.users.repository'); $userRepository->beginTransaction(); if ($appointmentsCount['futureAppointments']) { $result->setResult(CommandResult::RESULT_CONFLICT); $result->setMessage('Could not delete user.'); $result->setData([]); return $result; } if (!$userRepository->delete($command->getArg('id'))) { $userRepository->rollback(); $result->setResult(CommandResult::RESULT_ERROR); $result->setMessage('Could not delete user.'); $userRepository->rollback(); return $result; } $emptyAppointments = $appointmentRepo->getAppointmentsWithoutBookings(); foreach ($emptyAppointments->keys() as $appointmentKey) { $appointment = $emptyAppointments->getItem($appointmentKey); if (!$appointmentRepo->delete($appointment->getId()->getValue())) { $userRepository->rollback(); $result->setResult(CommandResult::RESULT_ERROR); $result->setMessage('Could not delete user.'); $userRepository->rollback(); return $result; } } $userRepository->commit(); $result->setResult(CommandResult::RESULT_SUCCESS); $result->setMessage('Successfully deleted user.'); $result->setData([]); return $result; } }