|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Core\Service\Sync; |
| 4 | + |
| 5 | +use App\Core\Repository\ServerRepository; |
| 6 | +use Doctrine\ORM\EntityManagerInterface; |
| 7 | +use Psr\Log\LoggerInterface; |
| 8 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 9 | + |
| 10 | +class PterodactylCleanupService |
| 11 | +{ |
| 12 | + public function __construct( |
| 13 | + private readonly EntityManagerInterface $entityManager, |
| 14 | + private readonly ServerRepository $serverRepository, |
| 15 | + private readonly LoggerInterface $logger |
| 16 | + ) { |
| 17 | + } |
| 18 | + |
| 19 | + public function cleanupOrphanedServers(array $existingPterodactylServerIds, ?SymfonyStyle $io = null, bool $dryRun = false): int |
| 20 | + { |
| 21 | + $this->logger->info('Starting cleanup of orphaned servers in PteroCA', ['dry_run' => $dryRun]); |
| 22 | + |
| 23 | + if (empty($existingPterodactylServerIds)) { |
| 24 | + $this->logger->warning('No Pterodactyl server IDs provided for cleanup'); |
| 25 | + return 0; |
| 26 | + } |
| 27 | + |
| 28 | + $orphanedServers = $this->serverRepository->findOrphanedServers($existingPterodactylServerIds); |
| 29 | + |
| 30 | + $deletedCount = 0; |
| 31 | + |
| 32 | + foreach ($orphanedServers as $server) { |
| 33 | + if ($io && !$this->isUserWantDeleteServer($server, $io)) { |
| 34 | + if ($io) { |
| 35 | + $io->info(sprintf( |
| 36 | + 'Skipping server #%s (ID: %d)...', |
| 37 | + $server->getPterodactylServerIdentifier(), |
| 38 | + $server->getPterodactylServerId() |
| 39 | + )); |
| 40 | + } |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + if (!$dryRun) { |
| 45 | + $server->setDeletedAtValue(); |
| 46 | + } |
| 47 | + |
| 48 | + $this->logger->info($dryRun ? 'Would mark server as deleted' : 'Marked server as deleted', [ |
| 49 | + 'server_id' => $server->getId(), |
| 50 | + 'pterodactyl_server_id' => $server->getPterodactylServerId(), |
| 51 | + 'pterodactyl_server_identifier' => $server->getPterodactylServerIdentifier(), |
| 52 | + 'dry_run' => $dryRun |
| 53 | + ]); |
| 54 | + $deletedCount++; |
| 55 | + } |
| 56 | + |
| 57 | + if ($deletedCount > 0 && !$dryRun) { |
| 58 | + $this->entityManager->flush(); |
| 59 | + } elseif ($dryRun && $deletedCount > 0) { |
| 60 | + $this->logger->info('Dry run mode - changes not saved to database'); |
| 61 | + } |
| 62 | + |
| 63 | + $this->logger->info('Cleanup completed', ['deleted_servers_count' => $deletedCount]); |
| 64 | + |
| 65 | + return $deletedCount; |
| 66 | + } |
| 67 | + |
| 68 | + private function isUserWantDeleteServer($server, SymfonyStyle $io): bool |
| 69 | + { |
| 70 | + $questionMessage = sprintf( |
| 71 | + 'Server #%s (ID: %d) was not found in Pterodactyl. Do you want to delete it from PteroCA?', |
| 72 | + $server->getPterodactylServerIdentifier(), |
| 73 | + $server->getPterodactylServerId() |
| 74 | + ); |
| 75 | + |
| 76 | + return strtolower($io->ask($questionMessage, 'yes')) === 'yes'; |
| 77 | + } |
| 78 | +} |
0 commit comments