getContainer()->getPermissionsService()->currentUserCanDelete(Entities::APPOINTMENTS)) { throw new AccessDeniedException('You are not allowed to delete appointment'); } $result = new CommandResult(); /** @var AppointmentRepository $appointmentRepo */ $appointmentRepo = $this->container->get('domain.booking.appointment.repository'); /** @var Appointment $appointment */ $appointment = $appointmentRepo->getById($command->getArg('id')); if (!$appointment instanceof Appointment) { $result->setResult(CommandResult::RESULT_ERROR); $result->setMessage('Could not delete appointment'); return $result; } // Set status to rejected, to send the notification that appointment is rejected $appointment->setStatus(new BookingStatus(BookingStatus::REJECTED)); $bookings = $appointment->getBookings(); foreach ($bookings->toArray() as $booking) { $booking = $bookings->getItem($booking['id']); $booking->setStatus(new BookingStatus(BookingStatus::REJECTED)); } $appointmentRepo->delete($command->getArg('id')); $result->setResult(CommandResult::RESULT_SUCCESS); $result->setMessage('Successfully deleted appointment'); $result->setData([ Entities::APPOINTMENT => $appointment->toArray() ]); return $result; } }