-
Notifications
You must be signed in to change notification settings - Fork 0
feat: server repository crud #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| review everything thoroughly with a focus on where these fall short of our development, architecture and testing rules | ||
| review everything thoroughly with a focus on where these fall short of our development, architecture and testing rules as well as finding potential bugs and regressions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| Meticulously catalog and analyze all the changes in this branch, compared to the branch it's based on. | ||
|
|
||
| Review everything thoroughly with a focus on where these fall short of our development, architecture and testing rules. | ||
| Review everything thoroughly with a focus on where these fall short of our development, architecture and testing rules as well as finding potential bugs and regressions | ||
|
|
||
| Provide a detailed report but don't make any changes yet. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| Meticulously catalog and analyze all the changes in this Git working tree, staged or unstaged. | ||
|
|
||
| Review everything thoroughly with a focus on where these fall short of our development, architecture and testing rules. | ||
| Review everything thoroughly with a focus on where these fall short of our development, architecture and testing rules as well as finding potential bugs and regressions. | ||
|
|
||
| Provide a detailed report but don't make any changes yet. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bigpixelrocket\DeployerPHP\DTOs; | ||
|
|
||
| readonly class ServerDTO | ||
| { | ||
| public function __construct( | ||
| public string $name, | ||
| public string $host, | ||
| public int $port = 22, | ||
| public string $username = 'root', | ||
| public ?string $privateKeyPath = null, | ||
| ) { | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Bigpixelrocket\DeployerPHP\Repositories; | ||
|
|
||
| use Bigpixelrocket\DeployerPHP\DTOs\ServerDTO; | ||
| use Bigpixelrocket\DeployerPHP\Services\InventoryService; | ||
|
|
||
| /** | ||
| * Repository for server CRUD operations using inventory storage. | ||
| * | ||
| * Stores servers as an array of objects to handle any special characters in server names. | ||
| */ | ||
| final class ServerRepository | ||
| { | ||
| private const PREFIX = 'servers'; | ||
|
|
||
| private ?InventoryService $inventory = null; | ||
|
|
||
| /** @var array<int, array<string, mixed>> */ | ||
| private array $servers = []; | ||
|
|
||
| // | ||
| // Public | ||
| // ------------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Set the inventory service instance to use for storage operations. | ||
| */ | ||
| public function loadInventory(InventoryService $inventory): void | ||
| { | ||
| $this->inventory = $inventory; | ||
|
|
||
| $servers = $inventory->get(self::PREFIX); | ||
| if (!is_array($servers)) { | ||
| $servers = []; | ||
| $inventory->set(self::PREFIX, $servers); | ||
| } | ||
|
|
||
| /** @var array<int, array<string, mixed>> $servers */ | ||
| $this->servers = $servers; | ||
| } | ||
|
|
||
| /** | ||
| * Create a new server in the inventory. | ||
| */ | ||
| public function create(ServerDTO $server): void | ||
| { | ||
| $this->assertInventoryLoaded(); | ||
|
|
||
| $existing = $this->findByName($server->name); | ||
| if (null !== $existing) { | ||
| throw new \RuntimeException("Server '{$server->name}' already exists"); | ||
| } | ||
|
|
||
| $this->servers[] = $this->dehydrateServerDTO($server); | ||
|
|
||
| $this->inventory->set(self::PREFIX, $this->servers); | ||
| } | ||
|
|
||
| /** | ||
| * Find a server by name. | ||
| */ | ||
| public function findByName(string $name): ?ServerDTO | ||
| { | ||
| $this->assertInventoryLoaded(); | ||
|
|
||
| foreach ($this->servers as $server) { | ||
| if (isset($server['name']) && $server['name'] === $name) { | ||
| return $this->hydrateServerDTO($server); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Get all servers from the inventory. | ||
| * | ||
| * @return array<int, ServerDTO> | ||
| */ | ||
| public function all(): array | ||
| { | ||
| $this->assertInventoryLoaded(); | ||
|
|
||
| $result = []; | ||
| foreach ($this->servers as $server) { | ||
| $result[] = $this->hydrateServerDTO($server); | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
| * Delete a server from the inventory. | ||
| */ | ||
| public function delete(string $name): void | ||
| { | ||
| $this->assertInventoryLoaded(); | ||
|
|
||
| $filtered = []; | ||
| foreach ($this->servers as $server) { | ||
| if (isset($server['name']) && $server['name'] !== $name) { | ||
| $filtered[] = $server; | ||
| } | ||
| } | ||
|
|
||
| $this->servers = $filtered; | ||
|
|
||
| $this->inventory->set(self::PREFIX, $this->servers); | ||
| } | ||
|
|
||
| // | ||
| // Private | ||
| // ------------------------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Ensure inventory service is loaded before operations. | ||
| * | ||
| * @throws \RuntimeException If inventory is not set | ||
| * @phpstan-assert !null $this->inventory | ||
| */ | ||
| private function assertInventoryLoaded(): void | ||
| { | ||
| if ($this->inventory === null) { | ||
| throw new \RuntimeException('Inventory not set. Call loadInventory() first.'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Convert ServerDTO to array for storage. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| private function dehydrateServerDTO(ServerDTO $server): array | ||
| { | ||
| return [ | ||
| 'name' => $server->name, | ||
| 'host' => $server->host, | ||
| 'port' => $server->port, | ||
| 'username' => $server->username, | ||
| 'privateKeyPath' => $server->privateKeyPath, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Hydrate a ServerDTO from inventory data. | ||
| * | ||
| * @param array<string, mixed> $data | ||
| */ | ||
| private function hydrateServerDTO(array $data): ServerDTO | ||
| { | ||
| $name = $data['name'] ?? ''; | ||
| $host = $data['host'] ?? ''; | ||
| $port = $data['port'] ?? 22; | ||
| $username = $data['username'] ?? 'root'; | ||
| $privateKeyPath = $data['privateKeyPath'] ?? null; | ||
|
|
||
| return new ServerDTO( | ||
| name: is_string($name) ? $name : '', | ||
| host: is_string($host) ? $host : '', | ||
| port: is_int($port) ? $port : 22, | ||
| username: is_string($username) ? $username : 'root', | ||
| privateKeyPath: is_string($privateKeyPath) ? $privateKeyPath : null, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Add class-level DocBlock.
The ServerDTO class is missing a DocBlock comment. As per coding guidelines, all classes should have DocBlock comments with minimalist descriptions.
Apply this diff to add a DocBlock:
📝 Committable suggestion
🤖 Prompt for AI Agents