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

View File

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