From 16aeeef26a8594a85dbc8194b354c552d45c0f0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucian=20V=C4=83c=C4=83roiu?= Date: Sat, 11 Oct 2025 00:37:38 +0300 Subject: [PATCH 1/4] refactor(server): extract server selection logic to ServerHelpersTrait - Add selectServer() method to handle server selection with empty inventory handling - Rename displayServerInfo() to displayServerDeets() for consistency - Update tests to reflect method name changes - Simplify server display formatting --- app/Traits/ServerHelpersTrait.php | 72 +++++++++++++++++--- tests/Fixtures/TestConsoleCommand.php | 2 +- tests/Unit/Traits/ServerHelpersTraitTest.php | 8 +-- 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/app/Traits/ServerHelpersTrait.php b/app/Traits/ServerHelpersTrait.php index 11cd7488..71d4c0ba 100644 --- a/app/Traits/ServerHelpersTrait.php +++ b/app/Traits/ServerHelpersTrait.php @@ -7,6 +7,7 @@ use Bigpixelrocket\DeployerPHP\DTOs\ServerDTO; use Bigpixelrocket\DeployerPHP\Repositories\ServerRepository; use Bigpixelrocket\DeployerPHP\Services\SSHService; +use Symfony\Component\Console\Command\Command; /** * Reusable server-related helpers for commands. @@ -18,18 +19,73 @@ trait ServerHelpersTrait { /** - * Display server information. + * Display server details. */ - protected function displayServerInfo(ServerDTO $server, string $color = 'black'): void + protected function displayServerDeets(ServerDTO $server): void { $this->writeln([ - " Name: {$server->name}", - " Host: {$server->host}", - " Port: {$server->port}", - " User: {$server->username}", - " Key: ".($server->privateKeyPath ?? 'default (~/.ssh/id_ed25519 or ~/.ssh/id_rsa)')."", - " " + " Name: {$server->name}", + " Host: {$server->host}", + " Port: {$server->port}", + " User: {$server->username}", + ' Key: '.($server->privateKeyPath ?? 'default (~/.ssh/id_ed25519 or ~/.ssh/id_rsa)').'', + ' ' ]); } + /** + * Select a server from inventory by name option or interactive prompt. + * + * @return array{server: ServerDTO|null, exit_code: int} Server DTO and exit code (SUCCESS if empty inventory, FAILURE if not found) + */ + protected function selectServer(string $optionName = 'name', string $promptLabel = 'Select server:'): array + { + // + // Get all servers + + $allServers = $this->servers->all(); + if (count($allServers) === 0) { + $this->warning('No servers found in inventory'); + $this->writeln([ + '', + 'Use server:add to add a server', + '', + ]); + + return ['server' => null, 'exit_code' => Command::SUCCESS]; + } + + // + // Extract server names and prompt for selection + + $serverNames = array_map(fn (ServerDTO $server) => $server->name, $allServers); + + $name = (string) $this->getOptionOrPrompt( + $optionName, + fn () => $this->promptSelect( + label: $promptLabel, + options: $serverNames, + ) + ); + + // + // Find server by name + + $server = null; + foreach ($allServers as $s) { + if ($s->name === $name) { + $server = $s; + break; + } + } + + if ($server === null) { + $this->error("Server '{$name}' not found in inventory"); + + return ['server' => null, 'exit_code' => Command::FAILURE]; + } + + return ['server' => $server, 'exit_code' => Command::SUCCESS]; + } + } diff --git a/tests/Fixtures/TestConsoleCommand.php b/tests/Fixtures/TestConsoleCommand.php index 8f2db2bd..f1a49381 100644 --- a/tests/Fixtures/TestConsoleCommand.php +++ b/tests/Fixtures/TestConsoleCommand.php @@ -70,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'hr' => $this->hr(), 'writeln' => $this->writeln(...$this->testArgs), 'showCommandHint' => $this->showCommandHint(...$this->testArgs), - 'displayServerInfo' => $this->displayServerInfo(...$this->testArgs), + 'displayServerDeets' => $this->displayServerDeets(...$this->testArgs), 'getOptionOrPrompt' => $this->testGetOptionOrPrompt(), 'getOptionOrPromptEmpty' => $this->testGetOptionOrPromptEmpty(), 'getOptionOrPromptBoolean' => $this->testGetOptionOrPromptBoolean(), diff --git a/tests/Unit/Traits/ServerHelpersTraitTest.php b/tests/Unit/Traits/ServerHelpersTraitTest.php index c54cafe7..f45bc724 100644 --- a/tests/Unit/Traits/ServerHelpersTraitTest.php +++ b/tests/Unit/Traits/ServerHelpersTraitTest.php @@ -17,12 +17,12 @@ }); // - // displayServerInfo + // displayServerDeets // ------------------------------------------------------------------------------- it('displays server information with all fields', function () { // ARRANGE - $this->command->setTestMethod('displayServerInfo', [ + $this->command->setTestMethod('displayServerDeets', [ new ServerDTO( name: 'production-web', host: '192.168.1.100', @@ -51,7 +51,7 @@ it('displays default SSH key message when privateKeyPath is null', function () { // ARRANGE - $this->command->setTestMethod('displayServerInfo', [ + $this->command->setTestMethod('displayServerDeets', [ new ServerDTO(name: 'test-server', host: '127.0.0.1'), ]); @@ -68,7 +68,7 @@ it('displays server info with default values', function () { // ARRANGE - $this->command->setTestMethod('displayServerInfo', [ + $this->command->setTestMethod('displayServerDeets', [ new ServerDTO(name: 'minimal', host: 'example.com'), ]); From 505af980aacb9fd6d26fa9f2311e7e5ac1903734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucian=20V=C4=83c=C4=83roiu?= Date: Sat, 11 Oct 2025 00:37:41 +0300 Subject: [PATCH 2/4] refactor(server): simplify ServerDeleteCommand using extracted helpers - Replace inline server selection logic with selectServer() method - Use displayServerDeets() for consistent server information display - Remove unused ServerDTO import - Reduce code complexity by 56 lines --- app/Console/Server/ServerDeleteCommand.php | 56 ++++------------------ 1 file changed, 10 insertions(+), 46 deletions(-) diff --git a/app/Console/Server/ServerDeleteCommand.php b/app/Console/Server/ServerDeleteCommand.php index f82fa929..7283e6d0 100644 --- a/app/Console/Server/ServerDeleteCommand.php +++ b/app/Console/Server/ServerDeleteCommand.php @@ -5,7 +5,6 @@ namespace Bigpixelrocket\DeployerPHP\Console\Server; use Bigpixelrocket\DeployerPHP\Contracts\BaseCommand; -use Bigpixelrocket\DeployerPHP\DTOs\ServerDTO; use Bigpixelrocket\DeployerPHP\Traits\ServerHelpersTrait; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; @@ -44,54 +43,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->hr(); - // - // Get all servers - - $allServers = $this->servers->all(); - if (count($allServers) === 0) { - $this->warning('No servers found in inventory'); - $this->writeln([ - '', - 'Use server:add to add a server', - '', - ]); - - return Command::SUCCESS; - } - - // Extract server names from DTOs for promptSelect - $serverNames = array_map(fn (ServerDTO $server) => $server->name, $allServers); - - // - // Select server to delete - $this->h1('Delete Server'); - $name = (string) $this->getOptionOrPrompt( - 'name', - fn () => $this->promptSelect( - label: 'Select server:', - options: $serverNames, - ) - ); - // - // Find server and display info - - $server = null; - foreach ($allServers as $s) { - if ($s->name === $name) { - $server = $s; - break; - } - } + // Select server + + $selection = $this->selectServer(); - if ($server === null) { - $this->error("Server '{$name}' not found in inventory"); - return Command::FAILURE; + if ($selection['server'] === null) { + return $selection['exit_code']; } - $this->displayServerInfo($server); + $server = $selection['server']; + $this->displayServerDeets($server); // // Confirm deletion @@ -115,16 +79,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int // // Delete server - $this->servers->delete($name); + $this->servers->delete($server->name); - $this->success("Server '{$name}' deleted successfully"); + $this->success("Server '{$server->name}' deleted successfully"); $this->writeln(''); // // Show command hint $this->showCommandHint('server:delete', [ - 'name' => $name, + 'name' => $server->name, 'yes' => $confirmed, ]); From 7aa6b988efb7d99db0b324e210e2b9ab76a88319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucian=20V=C4=83c=C4=83roiu?= Date: Sat, 11 Oct 2025 00:37:44 +0300 Subject: [PATCH 3/4] refactor(server): update commands to use displayServerDeets method - Replace displayServerInfo() calls with displayServerDeets() in ServerAddCommand - Replace displayServerInfo() calls with displayServerDeets() in ServerListCommand - Maintain consistent method naming across all server commands --- app/Console/Server/ServerAddCommand.php | 2 +- app/Console/Server/ServerListCommand.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Console/Server/ServerAddCommand.php b/app/Console/Server/ServerAddCommand.php index 44009700..db936e64 100644 --- a/app/Console/Server/ServerAddCommand.php +++ b/app/Console/Server/ServerAddCommand.php @@ -129,7 +129,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->hr(); - $this->displayServerInfo($server); + $this->displayServerDeets($server); // // Verify connectivity diff --git a/app/Console/Server/ServerListCommand.php b/app/Console/Server/ServerListCommand.php index d9ab5933..80923063 100644 --- a/app/Console/Server/ServerListCommand.php +++ b/app/Console/Server/ServerListCommand.php @@ -47,7 +47,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->h1('All Servers'); foreach ($allServers as $server) { - $this->displayServerInfo($server); + $this->displayServerDeets($server); } return Command::SUCCESS; From 2ead14d45e2d010444ff763712759482e7121c34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucian=20V=C4=83c=C4=83roiu?= Date: Sat, 11 Oct 2025 00:37:46 +0300 Subject: [PATCH 4/4] style(console): enhance banner and UI styling with bold formatting - Add bold formatting to banner lines in SymfonyApp - Enhance h1() and hr() methods with bold styling - Improve visual hierarchy and readability of console output --- app/SymfonyApp.php | 12 ++++++------ app/Traits/ConsoleOutputTrait.php | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/SymfonyApp.php b/app/SymfonyApp.php index 61d0d148..35f76279 100644 --- a/app/SymfonyApp.php +++ b/app/SymfonyApp.php @@ -94,13 +94,13 @@ private function displayBanner(): void // Simple, compact banner $banner = [ '', - '╭──────────────────────────────────────────', - ' ┌┬┐┌─┐┌─┐┬ ┌─┐┬ ┬┌─┐┬─┐', - ' ││├┤ ├─┘│ │ │└┬┘├┤ ├┬┘', - ' ─┴┘└─┘┴ ┴─┘└─┘ ┴ └─┘┴└─PHP '.$version.'', + '╭───────────────────────────────────────────────', + ' ┌┬┐┌─┐┌─┐┬ ┌─┐┬ ┬┌─┐┬─┐', + ' ││├┤ ├─┘│ │ │└┬┘├┤ ├┬┘', + ' ─┴┘└─┘┴ ┴─┘└─┘ ┴ └─┘┴└─PHP '.$version.'', '', - ' The Server & Site Deployment Tool for PHP', - '╰──────────────────────────────────────────', + ' The Server & Site Deployment Tool for PHP', + '╰───────────────────────────────────────────────', '' ]; diff --git a/app/Traits/ConsoleOutputTrait.php b/app/Traits/ConsoleOutputTrait.php index 403b1257..ef675d67 100644 --- a/app/Traits/ConsoleOutputTrait.php +++ b/app/Traits/ConsoleOutputTrait.php @@ -74,7 +74,7 @@ protected function error(string $message): void protected function h1(string $text): void { $this->writeln([ - ''.$text.'', + ''.$text.'', '', ]); } @@ -85,7 +85,7 @@ protected function h1(string $text): void protected function hr(): void { $this->writeln([ - '╭──────────────────────────────────────────', + '╭───────────────────────────────────────────────', '', ]); }