AccessControl::PERMISSION_MANAGE_HELP, ]; private ScheduledTasksRepository $scheduledTasksRepository; public function __construct( ScheduledTasksRepository $scheduledTasksRepository ) { $this->scheduledTasksRepository = $scheduledTasksRepository; } public function cancelTask($data) { $task = $this->validateAndFindTask($data); if (!($task instanceof ScheduledTaskEntity)) { return $task; } try { $this->scheduledTasksRepository->cancelTask($task); return $this->successResponse(); } catch (\Exception $e) { return $this->handleException($e->getMessage()); } } public function rescheduleTask($data) { $task = $this->validateAndFindTask($data); if (!($task instanceof ScheduledTaskEntity)) { return $task; } try { $this->scheduledTasksRepository->rescheduleTask($task); return $this->successResponse(); } catch (\Exception $e) { return $this->handleException($e->getMessage()); } } private function validateAndFindTask($data) { if (!isset($data['id'])) { return $this->handleException(__('Missing mandatory argument `id`.', 'mailpoet')); } $id = $data['id']; $task = $this->scheduledTasksRepository->findOneById($id); if (!$task) { return $this->handleException(__('Task not found.', 'mailpoet')); } return $task; } private function handleException($message): \MailPoet\API\JSON\ErrorResponse { return $this->badRequest([ ApiError::BAD_REQUEST => $message, ]); } }