From 4fb5cdcffdc29440993c98c99dc2379a0934cb84 Mon Sep 17 00:00:00 2001 From: tracer Date: Sun, 6 Feb 2022 17:58:52 +0100 Subject: [PATCH] refactored handleAllDomainsGetRequest Signed-off-by: tracer --- src/Controller/RequestController.php | 194 ++++++++++++++++----------- 1 file changed, 112 insertions(+), 82 deletions(-) diff --git a/src/Controller/RequestController.php b/src/Controller/RequestController.php index 789cf98..78ba460 100644 --- a/src/Controller/RequestController.php +++ b/src/Controller/RequestController.php @@ -4,12 +4,11 @@ namespace App\Controller; error_reporting(error_level: E_ALL); +use App\Entity\Domain; use App\Repository\ApikeyRepository; use App\Repository\DomainRepository; use DI\Container; use DI\ContainerBuilder; -use OpenApi\Annotations\ServerVariable; -use OpenApi\Annotations\Tag; use OpenApi\Generator; use UnhandledMatchError; use function DI\autowire; @@ -18,7 +17,32 @@ use OpenApi\Attributes as OAT; /** * */ -#[OAT\Info(version: '0.0.1', title: 'bindAPI' )] +#[OAT\Info(version: '0.0.1', title: 'bindAPI')] +#[OAT\Server( + url: "{schema}://{hostname}/api", + description: "The bindAPI URL.", + variables: [ + new OAT\ServerVariable( + serverVariable: "schema", + default: "https", + enum: ["https", "http"] + ), + new OAT\ServerVariable( + serverVariable: "hostname", + default: "ns2.24unix.net", + ) + ] +)] +#[OAT\Tag( + name: "Server" +)] +#[OAT\SecurityScheme( + securityScheme: "Authorization", + type: "apiKey", + description: "description", + name: "X-API-Key", + in: "header" +)] class RequestController { //private DatabaseConnection $databaseConnection; @@ -51,12 +75,56 @@ class RequestController $this->apikeyRepository = $this->container->get(name: ApikeyRepository::class); } - /** - * @OA\Server( - * url = "https://ns2.24unix.net/api" - * ) + * @return void + */ + #[OAT\Get( + path: '/domains', + operationId: 'getAllDomains', + description: 'Returns a list of all domains on this server.', + summary: 'Listing all domains.', +// security: [ +// 'Authorization' => [ +// +// "read:api" +// ] +// ], + servers: [], + tags: ['Domains'], + responses: [ + new OAT\Response( + response: 200, + description: 'OK' + ), + new OAT\Response( + response: 401, + description: 'API key is missing or invalid.' + ), + new OAT\Response( + response: 404, + description: 'Domain not found.' + )] + )] + public function handleAllDomainsGetRequest(): void + { + $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; + } + + + /** * @OA\Tag(name = "Server") * @OA\Get( * path = "/ping", @@ -70,61 +138,7 @@ class RequestController * } * ) * - * @OA\SecurityScheme (name="bindAPISecurity", - * type="apiKey", - * description="description", - * name="X-API-Key", - * in="header", - * securityScheme="Authorization" - * - * ) - * @SwaggerDefinition( - * securityDefinition = @SecurityDefinition( - * apiKeyAuthDefinitions = { - * @ApiKeyAuthDefinition( - * key = "X-API-Key", in = ApiKeyAuthDefinition.ApiKeyLocation.HEADER, name = "X-API-KEY" - * ) - * } - * ) - * ) * @OA\Tag(name = "Domains") - * @OA\Get( - * path="/domains", - * summary="Listing all domains.", - * description="desc", - * tags={"Domains"}, - * @OA\Response(response="200", description="OK"), - * @OA\Response(response = "401", description = "API key is missing or invalid."), - * @OA\Response(response="404", description="Domain not found."), - * security={ - * {"Authorization":{"read":"write"}} - * } - * ) - * @OA\Post( - * path="/domains", - * summary="Create a domain.", - * description="Creates a new domain.", - * tags={"Domains"}, - * @OA\Response(response="201", description="Created"), - * @OA\Response(response = "400", description = "Invalid request body."), - * @OA\Response(response = "401", description = "API key is missing or invalid."), - * @OA\Response(response="404", description="Domain not found."), - * security={ - * {"Authorization":{"read":"write"}} - * } - * ) - * @OA\Get( - * path="/domains/{name}", - * summary="Returns a single domain.", - * description="Returns information of a single domain specified by its domain name.", - * tags={"Domains"}, - * @OA\Response(response="200", description="OK"), - * @OA\Response(response = "401", description = "API key is missing or invalid."), - * @OA\Response(response="404", description="Domain not found."), - * security={ - * {"Authorization":{"read":"write"}} - * } - * ) * @OA\Put( * path="/domains/{name}", * summary="Updates a domain.", @@ -149,9 +163,35 @@ class RequestController * {"Authorization":{"read":"write"}} * } * ) - * * @return void */ + + #[OAT\Get( + path: '/domains/{name}', + operationId: 'getSingleDomain', + description: 'Returns information of a single domain specified by its domain name.', + summary: 'Returns a single domain.', + security: [ + ], + tags: ['Domains'], + parameters: [ + new OAT\Parameter(name: 'name', in: 'path', required: true, schema: new OAT\Schema(type: 'string')), + ], + responses: [ + new OAT\Response( + response: 200, + description: 'OK' + ), + new OAT\Response( + response: 401, + description: 'API key is missing or invalid.' + ), + new OAT\Response( + response: 404, + description: 'Domain not found.' + )] + + )] public function processRequest() { $command = $this->uri[2]; @@ -253,19 +293,7 @@ class RequestController public function handleDomainGetRequest(): void { if (empty($this->uri[3])) { - $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; + $this->handleAllDomainsGetRequest(); } else { if ($result = $this->domainRepository->findByName(name: $this->uri[3])) { $domain = [ @@ -288,8 +316,7 @@ class RequestController /** * @return void */ - public - function handleDomainPostRequest(): void + public function handleDomainPostRequest(): void { $name = $_POST['name'] ?? ''; $panelID = intval(value: $_POST['panel_id'] ?? 0); @@ -310,7 +337,8 @@ class RequestController $this->status = "400 Bad request"; $this->message = "Domain: $name already exists."; } else { - $result = $this->domainRepository->insert(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa); + $domain = new Domain(name: $name, panelID: $panelID, a: $a, aaaa: $aaaa); + $result = $this->domainRepository->insert(domain: $domain); $this->status = "201 Created"; $this->message = $result; } @@ -334,7 +362,7 @@ class RequestController $put[$key] = $value; } $id = $put['id'] ?? 0; - $name = $put['name'] ?? ""; + $name = $put['name'] ?? ''; $panelID = $put['panel_id'] ?? ""; $a = $put['a'] ?? ""; $aaaa = $put['aaaa'] ?? ""; @@ -356,7 +384,8 @@ class RequestController $this->status = "400 Bad Request"; $this->message = "At least one IP address is required."; } else { - $this->domainRepository->update(id: $id, name: $panelID, panelID: $name, a: $a, aaaa: $aaaa); + $domain = new Domain(name: $panelID, id: $id, panelID: $name, a: $a, aaaa: $aaaa); + $this->domainRepository->update(domain: $domain); $this->header = "201 Updated"; $this->status = "201 Updated"; $this->message = "201 Updated"; @@ -389,12 +418,13 @@ class RequestController $this->status = "400 Bad Request"; $this->message = "You need to supply an ID."; } else { - if (!$this->domainRepository->findByID(id: $id)) { + + if (!$domain = $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->domainRepository->delete(id: $id); + $this->domainRepository->delete(domain: $domain); $this->header = "204 No content."; $this->status = "204 No content."; $this->message = "The domain $id has been deleted.";