Skip to content
Snippets Groups Projects
Commit a534038b authored by Nigel  Sahl's avatar Nigel Sahl
Browse files

Update ControllerWrapper.php

parent 27a3b72e
No related merge requests found
<?php <?php
require_once __DIR__ . "/BaseController.php"; require_once __DIR__ . "/BaseController.php";
require_once __DIR__ . "/../middleware/ApiKeyHandler.php";
class ControllerWrapper class ControllerWrapper
{ {
private BaseController $controller; private BaseController $controller;
private string $apiKey;
public function __construct(BaseController $controller) public function __construct(BaseController $controller)
{ {
$headers = getallheaders();
$this->apiKey = $headers['X-API-KEY'] ?? '';
$this->controller = $controller; $this->controller = $controller;
} }
...@@ -24,25 +28,32 @@ class ControllerWrapper ...@@ -24,25 +28,32 @@ class ControllerWrapper
} }
public function respond() public function respond()
{ {
$requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); $apiKeyHandler = ApiKeyHandler::getInstance();
$res = $apiKeyHandler->checkApiKey($this->apiKey);
$strErrorDesc = ''; $strErrorDesc = '';
$strErrorHeader = ''; $strErrorHeader = '';
$responseData = []; if (!$res) {
$strErrorDesc = 'Invalid API Key';
try {
$this->controller->respond($requestMethod, $responseData, $strErrorDesc, $strErrorHeader);
} catch (Error $e) {
$strErrorDesc = $e->getMessage() . ' . Something went wrong!';
$strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
} catch (ServiceException $e) {
$strErrorDesc = (string) "Service Exception Encountered";
$strErrorHeader = 'HTTP/1.1 400 Bad Request';
} catch (Exception $e) {
$strErrorDesc = $e->getMessage();
$strErrorHeader = 'HTTP/1.1 400 Bad Request'; $strErrorHeader = 'HTTP/1.1 400 Bad Request';
} else {
$requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]);
$responseData = [];
try {
$this->controller->respond($requestMethod, $responseData, $strErrorDesc, $strErrorHeader);
} catch (Error $e) {
$strErrorDesc = $e->getMessage() . ' . Something went wrong!';
$strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
} catch (ServiceException $e) {
$strErrorDesc = (string) "Service Exception Encountered";
$strErrorHeader = 'HTTP/1.1 400 Bad Request';
} catch (Exception $e) {
$strErrorDesc = $e->getMessage();
$strErrorHeader = 'HTTP/1.1 400 Bad Request';
}
} }
// send output // send output
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment