diff --git a/src/Controller/RequestController.php b/src/Controller/RequestController.php index 0edc607..45ccb62 100644 --- a/src/Controller/RequestController.php +++ b/src/Controller/RequestController.php @@ -1,34 +1,50 @@ 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); + + $containerBuilder = new ContainerBuilder(); + $containerBuilder->addDefinitions([ + DatabaseConnection::class => autowire()->constructorParameter(parameter: 'config', value: $this->config), + ]); + $this->container = $containerBuilder->build(); + + $this->domainRepository = $this->container->get(name: DomainRepository::class); + $this->apikeyRepository = $this->container->get(name: ApikeyRepository::class); + } @@ -54,7 +70,7 @@ class RequestController 'PUT' => $this->handleDomainPutRequest(), 'DELETE' => $this->handleDomainDeleteRequest() }; - } catch(UnhandledMatchError) { + } catch (UnhandledMatchError) { $this->header = '400 Bad Request'; $this->status = '400 Bad Request'; $this->message = "unknown request method: $this->requestMethod"; @@ -62,8 +78,8 @@ class RequestController } } } - - if(!empty($this->header)) { + + if (!empty($this->header)) { header(header: $_SERVER['SERVER_PROTOCOL'] . ' ' . $this->header); } if (!empty($this->result)) { @@ -75,7 +91,7 @@ class RequestController ]); } else { echo json_encode(value: [ - 'status' => $this->status ?? "Error: No status", + 'status' => $this->status ?? "Error: No status", 'message' => $this->message ?? "Error: No message." ]); } @@ -98,9 +114,8 @@ class RequestController return false; } else { [$prefix,] = explode(separator: '.', string: $apiKey); - $apiUsers = new ApiKeys(databaseConnection: $this->databaseConnection); - if ($apiResult = $apiUsers->findByPrefix(prefix: $prefix)) { - $storedHash = $apiResult['api_token']; + if ($apiResult = $this->apikeyRepository->findByPrefix(prefix: $prefix)) { + $storedHash = $apiResult->getApiToken(); if (!password_verify(password: $apiKey, hash: $storedHash)) { $this->header = "401 Unauthorized"; $this->status = "401 Unauthorized"; @@ -110,7 +125,7 @@ class RequestController } else { $this->header = "401 Unauthorized"; $this->status = "401 Unauthorized"; - $this->message = "API key not found."; + $this->message = "Invalid API key."; return false; } } @@ -123,10 +138,29 @@ class RequestController public function handleDomainGetRequest(): void { if (empty($this->uri[3])) { - $this->result = $this->domainController->findAll(); + $domains = $this->domainRepository->findAll(); + $resultDomain = []; + foreach ($domains as $singleDomain) { + $domain = [ + 'id' => $singleDomain->getId(), + 'name' => $singleDomain->getName(), + 'panel_id' => $singleDomain->getPanelId(), + 'a' => $singleDomain->getA(), + 'aaaa' => $singleDomain->getAaaa() + ]; + $resultDomain[] = $domain; + } + $this->result = $resultDomain; } else { - if ($result = $this->domainController->findByName(name: $this->uri[3])) { - $this->result = $result; + if ($result = $this->domainRepository->findByName(name: $this->uri[3])) { + $domain = [ + 'id' => $result->getId(), + 'name' => $result->getName(), + 'panel_id' => $result->getPanelId(), + 'a' => $result->getA(), + 'aaaa' => $result->getAaaa() + ]; + $this->result = $domain; } else { $this->header = "404 Not Found "; $this->status = "404 Not Found "; @@ -155,12 +189,12 @@ class RequestController $this->status = "400 Bad Request"; $this->message = "At least one IP address or panel ID is required."; } else { - if ($this->domainController->findByName(name: $name)) { + if ($this->domainRepository->findByName(name: $name)) { $this->header = "400 Bad request"; $this->status = "400 Bad request"; $this->message = "Domain: $name already exists."; } else { - $result = $this->domainController->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa); + $result = $this->domainRepository->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa); $this->status = "201 Created"; $this->message = $result; } @@ -192,7 +226,7 @@ class RequestController $this->status = "400 Bad Request"; $this->message = "An ID is required"; } else { - if (!$this->domainController->findByID(id: $id)) { + if (!$this->domainRepository->findByID(id: $id)) { $this->status = "404 Not Found"; $this->message = "Domain with ID : $id doesn't exist."; } else { @@ -205,7 +239,7 @@ class RequestController $this->status = "400 Bad Request"; $this->message = "At least one IP address is required."; } else { - $dcResult = $this->domainController->update(id: $id, name: $panelID, panelID: $name, a: $a, aaaa: $aaaa); + $this->domainRepository->update(id: $id, name: $panelID, panelID: $name, a: $a, aaaa: $aaaa); $this->header = "201 Updated"; $this->status = "201 Updated"; $this->message = "201 Updated"; @@ -236,12 +270,12 @@ class RequestController $this->status = "400 Bad Request"; $this->message = "You need to supply an ID."; } else { - if (!$this->domainController->findByID(id: $id)) { + if (!$this->domainRepository->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: $id); + $this->domainRepository->delete(id: $id); $this->header = "204 No content."; $this->status = "204 No content."; $this->message = "The domain $id has been deleted.";