controllerName = $controllerName; $this->controllerMethod = $controllerMethod; $this->postMethod = $postMethod; $this->controllerNamespace = $controllerNamespace; } /** * @inheritDoc */ public function init(Task $task) { } /** * @inheritDoc * @throws TasksException */ public function primaryHandler(Task $task): bool { // Set the arguments $args = $task->getArguments(); array_unshift($args, $task); // First we fetch the controller $controller = $this->getController(); // Check if method exists if (!method_exists($controller, $this->controllerMethod)) throw new TasksException("Could not handle task. Method '$this->controllerMethod' not found on controller."); if (!method_exists($controller, 'getTaskStatus')) throw new TasksException("Could not handle task. Method 'getTaskStatus()' not found on controller, which is required."); if ($this->parentInput !== null && method_exists($controller, 'setInput')) $controller->setInput($this->parentInput); // Call method and collect output $this->output = call_user_func_array([$controller, $this->controllerMethod], $args); $success = $controller->getTaskStatus(); if (!is_bool($success)) throw new TasksException("Could not determine whether task has succeeded. getTaskStatus() returned non-bool."); return $success; } /** * @inheritDoc */ public function getOutput(): string { return $this->output; } /** * @inheritDoc * @throws TasksException */ public function postHandler(Task $task) { // Abort if no postMethod exists if (is_null($this->postMethod)) throw new TasksException("Could not handle task. No post method provided."); // First we fetch the controller $controller = $this->getController(); // Check if method exists if (!method_exists($controller, $this->postMethod)) throw new TasksException("Could not handle task. Post method '$this->postMethod' not found on controller."); if (!method_exists($controller, 'getTaskStatus')) throw new TasksException("Could not handle task. Method 'getTaskStatus()' not found on controller, which is required."); if ($this->parentInput !== null && method_exists($controller, 'setInput')) $controller->setInput($this->parentInput); // Call method and collect output $this->postOutput = call_user_func_array([$controller, $this->postMethod], [$task]); $success = $controller->getTaskStatus(); if (!is_bool($success)) throw new TasksException("Could not determine whether task has succeeded. getTaskStatus() returned non-bool."); return $success; } /** * @inheritDoc */ public function getPostOutput(): string { return $this->postOutput; } /** * @return Controller * @throws TasksException */ private function getController(): Controller { // First load the controllers component try { /** @var Controllers $controllers */ $controllers = Factory::getInstance('controllers'); // Load the requested controller return $controllers->get($this->controllerName, [], $this->controllerNamespace); } catch (FactoryException $e) { throw new TasksException("Could not get controller. FuzeWorks\MVCR is not installed!"); } catch (ControllerException $e) { throw new TasksException("Could not get controller. Controller threw exception: '" . $e->getMessage() . "'"); } catch (NotFoundException $e) { throw new TasksException("Could not get controller. Controller was not found."); } } /** * @var Handler */ private $parentHandler; /** * @inheritDoc */ public function getParentHandler(): ?Handler { return $this->parentHandler; } /** * @inheritDoc */ public function setParentHandler(Handler $parentHandler): void { $this->parentHandler = $parentHandler; } /** * @inheritDoc */ public function setParentInput(string $input): void { $this->parentInput = $input; } }