TaskController.php 3.15 KiB
<?php
/*
 * The controller is part of the presentation layer in an MVC (Model-View-Controller) architecture.
 * Its primary role is to handle incoming HTTP requests, process user inputs, and determine which business logic (services or models) to invoke.
 * It then decides what response to send back to the client.
 */
class TaskController
  private TaskGateway $gateway;
  public function __construct (TaskGateway $gateway)
    $this->gateway = $gateway;
  protected function parseJsonResult ($result = NULL): void
    if (!empty($result)) {
      echo json_encode($result, JSON_PRETTY_PRINT);
    } else {
      // No result received
      echo json_encode("No results received from the endpoint, maybe nothing to be processed ?");
  /**
   * @param string $method
   * @param string|null $objectType
   * @param $jsonBody
   * @return void
   * @throws Exception
   * NOTE : objectType is actually the task type.
  public function processRequest (string $method, ?string $objectType, $jsonBody = NULL): void
    // Allow result to be nullable.
    $result = NULL;
    // If no specific tasks object specified, return all tasks
    if ($objectType == NULL) {
      if ($method == "GET") {
        echo json_encode($this->gateway->getTask(NULL));
      } else {
        $this->respondMethodAllowed("GET");
      // Otherwise continue the process of the specific task / object type specified
    } else {
      // Define an empty array as returning result.
      switch ($method) {
        // GET methods
        case "GET":
          switch ($objectType) {
            case $objectType:
              if (class_exists($objectType)) {
                $endpoint = new $objectType($this->gateway);
                $result   = $endpoint->processEndPointGet();
              break;
            default:
              $this->respondMethodAllowed("GET, PATCH, DELETE");
          $this->parseJsonResult($result);
          break;
        // PATCH methods
        case "PATCH":
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
switch ($objectType) { case 'removeSubTasks': $result = $this->gateway->removeCompletedTasks(); break; case 'activateCyclicTasks': $result = $this->gateway->activateCyclicTasks(); break; case $objectType: if (class_exists($objectType)) { $endpoint = new $objectType($this->gateway); $result = $endpoint->processEndPointPatch($jsonBody); } break; } $this->parseJsonResult($result); break; case "DELETE": break; default: $this->respondMethodAllowed("GET, PATCH, DELETE"); } } } private function respondMethodAllowed (string $allowed_methods): void { http_response_code(405); header("Allow: $allowed_methods"); } public static function respondNotFound (string $objectType): void { http_response_code(404); // Task ID is easier to be used - requires unique ID attributes during task creation (FD-Interface) echo json_encode(["message" => "Task object type : $objectType not found"]); } }