<?php
    class UsersController {
        private $requestMethod;

        public function __construct($requestMethod) {
            $this->requestMethod = $requestMethod;
        }

        public function processRequest($post = [], $id = 0) {
            switch ($this->requestMethod) {
                case 'GET':
                    if ($id == 0)
                        $response = $this->getAllUsers();
                    else 
                        $response = $this->getUser($id);
                    break;
                case 'POST':
                    $response = $this->createUser($post);
                    break;
                case 'PUT':
                    $response = $this->updateUser(json_decode($post), $id);
                    break;
                case 'DELETE':
                    $response = $this->deleteUser($id);
                    break;
                default:
                    $response = $this->notFoundResponse();
                    break;
            }

            header($response['status_code_header']);

            if ($response['body']) {
                print_r($response['body']);
            }
        }

        private function createUser($post) {
            $response = [];
            [$response['status_code_header'], $response['body']] = UserModel::createUser($post);
            return $response;
        }

        private function getUser($id) {
			$response = [];
            [$response['status_code_header'], $response['body']] = UserModel::getUser($id);
            return $response;
        }

        private function getAllUsers() {
			$response = [];
            [$response['status_code_header'], $response['body']] = UserModel::getAllUsers();
            return $response;
        }

        private function updateUser($post, $id) {
			$response = [];
            [$response['status_code_header'], $response['body']] = UserModel::updateUser($post, $id);
            return $response;
        }

        private function deleteUser($id) {
			$response = [];
            [$response['status_code_header'], $response['body']] = UserModel::deleteUser($id);
            return $response;
        }

        private function notFoundResponse() {
            $response['status_code_header'] = 'HTTP/1.1 404 Not Found';
            $response['body'] = null;
            return $response;
        }
    }
?>