added missing named parameters, added strict_types

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-01-26 19:36:56 +01:00
parent 1c1305af89
commit 662737318b
1 changed files with 33 additions and 28 deletions

View File

@ -1,7 +1,8 @@
<?php <?php declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
error_reporting(error_level: E_ALL);
use UnhandledMatchError; use UnhandledMatchError;
/** /**
@ -24,10 +25,10 @@ class RequestController
*/ */
public function __construct(private array $config, private String $requestMethod, private array $uri) public function __construct(private array $config, private String $requestMethod, private array $uri)
{ {
$this->requestMethod = strtoupper($requestMethod); $this->requestMethod = strtoupper(string: $requestMethod);
$this->databaseConnection = new DatabaseConnection($this->config); $this->databaseConnection = new DatabaseConnection(config: $this->config);
$this->panelController = new PanelController($this->databaseConnection); $this->panelController = new PanelController(databaseConnection: $this->databaseConnection);
$this->domainController = new DomainController($this->databaseConnection, $this->panelController); $this->domainController = new DomainController(databaseConnection: $this->databaseConnection, panelController: $this->panelController);
} }
@ -66,14 +67,14 @@ class RequestController
header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header); header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header);
} }
if (!empty($this->result)) { if (!empty($this->result)) {
echo json_encode($this->result); echo json_encode(value: $this->result);
} else { } else {
if ($this->status == 'pong') { if ($this->status == 'pong') {
echo json_encode([ echo json_encode(value: [
'response' => $this->status 'response' => $this->status
]); ]);
} else { } else {
echo json_encode([ echo json_encode(value: [
'status' => $this->status ?? "Error: No status", 'status' => $this->status ?? "Error: No status",
'message' => $this->message ?? "Error: No message." 'message' => $this->message ?? "Error: No message."
]); ]);
@ -87,8 +88,8 @@ class RequestController
*/ */
public function checkPassword(): bool public function checkPassword(): bool
{ {
$headers = array_change_key_case(getallheaders(), CASE_UPPER); $headers = array_change_key_case(array: getallheaders(), case: CASE_UPPER);
$apiKey = $headers['X-API-KEY'] ?? ""; $apiKey = $headers['X-API-KEY'] ?? '';
if (empty($apiKey)) { if (empty($apiKey)) {
$this->header = "401 Unauthorized"; $this->header = "401 Unauthorized";
@ -96,12 +97,12 @@ class RequestController
$this->message = "API key is missing."; $this->message = "API key is missing.";
return false; return false;
} else { } else {
[$prefix,] = explode('.', $apiKey); [$prefix,] = explode(separator: '.', string: $apiKey);
$apiUsers = new ApiKeys($this->databaseConnection); $apiUsers = new ApiKeys(databaseConnection: $this->databaseConnection);
$apiResult = $apiUsers->findByPrefix($prefix); $apiResult = $apiUsers->findByPrefix(prefix: $prefix);
$storedHash = $apiResult['api_token']; $storedHash = $apiResult['api_token'];
if (!password_verify($apiKey, $storedHash)) { if (!password_verify(password: $apiKey, hash: $storedHash)) {
$this->header = "401 Unauthorized"; $this->header = "401 Unauthorized";
$this->status = "401 Unauthorized"; $this->status = "401 Unauthorized";
$this->message = "API key mismatch."; $this->message = "API key mismatch.";
@ -119,7 +120,7 @@ class RequestController
if (empty($this->uri[3])) { if (empty($this->uri[3])) {
$this->result = $this->domainController->findAll(); $this->result = $this->domainController->findAll();
} else { } else {
if ($result = $this->domainController->findByID(intval($this->uri[3]))) { if ($result = $this->domainController->findByName(name: $this->uri[3])) {
$this->result = $result; $this->result = $result;
} else { } else {
$this->header = "404 Not Found "; $this->header = "404 Not Found ";
@ -147,11 +148,11 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "At least one IP address is required."; $this->message = "At least one IP address is required.";
} else { } else {
if ($this->domainController->findByName($name)) { if ($this->domainController->findByName(name: $name)) {
$this->status = "400 Bad request"; $this->status = "400 Bad request";
$this->message = "Domain: $name already exists."; $this->message = "Domain: $name already exists.";
} else { } else {
$result = $this->domainController->insert($name, $panelID, $a, $aaaa); $result = $this->domainController->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa);
$this->status = "201 Created"; $this->status = "201 Created";
$this->message = $result; $this->message = $result;
} }
@ -166,11 +167,11 @@ class RequestController
public function handleDomainPutRequest(): void public function handleDomainPutRequest(): void
{ {
$putData = fopen(filename: 'php://input', mode: 'r'); $putData = fopen(filename: 'php://input', mode: 'r');
$data = fread($putData, 512); $data = fread(stream: $putData, length: 512);
$params = explode('&', $data); $params = explode(separator: '&', string: $data);
foreach ($params as $param) { foreach ($params as $param) {
[$key, $value] = explode('=', $param); [$key, $value] = explode(separator: '=', string: $param);
$put[$key] = $value; $put[$key] = $value;
} }
$id = $put['id'] ?? 0; $id = $put['id'] ?? 0;
@ -183,7 +184,7 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "An ID is required"; $this->message = "An ID is required";
} else { } else {
if (!$this->domainController->findByID($id)) { if (!$this->domainController->findByID(id: $id)) {
$this->status = "404 Not Found"; $this->status = "404 Not Found";
$this->message = "Domain with ID : $id doesn't exist."; $this->message = "Domain with ID : $id doesn't exist.";
} else { } else {
@ -196,9 +197,10 @@ class RequestController
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "At least one IP address is required."; $this->message = "At least one IP address is required.";
} else { } else {
$dcResult = $this->domainController->update($id, $panelID, $name, $a, $aaaa); $dcResult = $this->domainController->update(id: $id, name: $panelID, panelID: $name, a: $a, aaaa: $aaaa);
$this->header = "201 Updated";
$this->status = "201 Updated"; $this->status = "201 Updated";
$this->message = $dcResult; $this->message = "201 Updated";
} }
} }
} }
@ -211,7 +213,7 @@ class RequestController
public function handleDomainDeleteRequest(): void public function handleDomainDeleteRequest(): void
{ {
$deleteData = fopen(filename: 'php://input', mode: 'r'); $deleteData = fopen(filename: 'php://input', mode: 'r');
$data = fread($deleteData, length: 512); $data = fread(stream: $deleteData, length: 512);
$params = explode(separator: '&', string: $data); $params = explode(separator: '&', string: $data);
foreach ($params as $param) { foreach ($params as $param) {
@ -222,14 +224,17 @@ class RequestController
$id = $delete['id'] ?? 0; $id = $delete['id'] ?? 0;
if ($id == 0) { if ($id == 0) {
$this->status = "404 Bad Request"; $this->header = "400 Bad Request";
$this->status = "400 Bad Request";
$this->message = "You need to supply an ID."; $this->message = "You need to supply an ID.";
} else { } else {
if (!$this->domainController->findByID($id)) { if (!$this->domainController->findByID(id: $id)) {
$this->header = "400 Bad Request";
$this->status = "400 Bad Request"; $this->status = "400 Bad Request";
$this->message = "There is no domain with ID $id."; $this->message = "There is no domain with ID $id.";
} else { } else {
$this->domainController->delete($id); $this->domainController->delete(id: $id);
$this->header = "204 No content.";
$this->status = "204 No content."; $this->status = "204 No content.";
$this->message = "The domain $id has been deleted."; $this->message = "The domain $id has been deleted.";
} }