Skip to content

Commit

Permalink
Extract destroy action from Setup\ServersController
Browse files Browse the repository at this point in the history
Extracts it into a new controller called Setup\ServerDestroyController.

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed May 10, 2024
1 parent 9cac3a9 commit 2fe67cc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3780,6 +3780,11 @@ parameters:
count: 1
path: src/Controllers/Setup/MainController.php

-
message: "#^Parameter \\#1 \\$config of class PhpMyAdmin\\\\Controllers\\\\Setup\\\\ServerDestroyController constructor expects PhpMyAdmin\\\\Config\\\\ConfigFile, mixed given\\.$#"
count: 1
path: src/Controllers/Setup/MainController.php

-
message: "#^Parameter \\#1 \\$config of class PhpMyAdmin\\\\Controllers\\\\Setup\\\\ServersController constructor expects PhpMyAdmin\\\\Config\\\\ConfigFile, mixed given\\.$#"
count: 1
Expand Down
5 changes: 2 additions & 3 deletions src/Controllers/Setup/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ public function __invoke(ServerRequest $request): Response
}

if ($page === 'servers') {
$controller = new ServersController($GLOBALS['ConfigFile'], $this->template);
if ($request->getQueryParam('mode') === 'remove' && $request->isPost()) {
$controller->destroy($request);
(new ServerDestroyController($GLOBALS['ConfigFile'], $this->template))($request);
$response = $response->withStatus(StatusCodeInterface::STATUS_FOUND);

return $response->withHeader(
Expand All @@ -75,7 +74,7 @@ public function __invoke(ServerRequest $request): Response
);
}

return $response->write($controller->index($request));
return $response->write((new ServersController($GLOBALS['ConfigFile'], $this->template))($request));
}

return $response->write((new HomeController($GLOBALS['ConfigFile'], $this->template))($request));
Expand Down
35 changes: 35 additions & 0 deletions src/Controllers/Setup/ServerDestroyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Controllers\Setup;

use PhpMyAdmin\Http\ServerRequest;

use function is_numeric;

class ServerDestroyController extends AbstractController
{
public function __invoke(ServerRequest $request): void
{
$id = $this->getIdParam($request->getQueryParam('id'));
$hasServer = $id >= 1 && $this->config->get('Servers/' . $id) !== null;
if (! $hasServer) {
return;
}

$this->config->removeServer($id);
}

/** @psalm-return int<0, max> */
private function getIdParam(mixed $idParam): int
{
if (! is_numeric($idParam)) {
return 0;
}

$id = (int) $idParam;

return $id >= 1 ? $id : 0;
}
}
13 changes: 1 addition & 12 deletions src/Controllers/Setup/ServersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class ServersController extends AbstractController
{
public function index(ServerRequest $request): string
public function __invoke(ServerRequest $request): string
{
$id = $this->getIdParam($request->getQueryParam('id'));
$mode = $this->getModeParam($request->getQueryParam('mode'));
Expand Down Expand Up @@ -44,17 +44,6 @@ public function index(ServerRequest $request): string
]);
}

public function destroy(ServerRequest $request): void
{
$id = $this->getIdParam($request->getQueryParam('id'));
$hasServer = $id >= 1 && $this->config->get('Servers/' . $id) !== null;
if (! $hasServer) {
return;
}

$this->config->removeServer($id);
}

private function getFormSetParam(mixed $formSetParam): string
{
return is_string($formSetParam) ? $formSetParam : '';
Expand Down

0 comments on commit 2fe67cc

Please sign in to comment.