Files
cabinet/src/Command/UserCleanupCommand.php
T
2026-05-28 12:09:28 +03:00

67 lines
2.6 KiB
PHP

<?php
namespace App\Command;
use App\Service\UserCleanupService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class UserCleanupCommand extends Command
{
private $userCleanupService;
public function __construct(UserCleanupService $userCleanupService)
{
parent::__construct();
$this->userCleanupService = $userCleanupService;
}
protected static $defaultName = 'app:user:cleanup';
protected static $defaultDescription = 'Удаляет пользователей с только ROLE_USER после истечения времени жизни кукисов';
protected function configure(): void
{
$this
->setDescription(self::$defaultDescription)
->addOption('dry-run', null, InputOption::VALUE_NONE, 'Показать, какие пользователи будут удалены, без фактического удаления')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$dryRun = $input->getOption('dry-run');
if ($dryRun) {
$io->note('Режим проверки (dry-run). Пользователи не будут удалены.');
}
$io->title('Очистка пользователей');
try {
if ($dryRun) {
$io->note('Режим проверки (dry-run). Пользователи не будут удалены.');
// В dry-run режиме просто показываем сколько будет удалено
// Но для этого нужно добавить метод в сервис или просто запустить и показать результат
}
$deletedCount = $this->userCleanupService->cleanupExpiredUsers();
if ($dryRun) {
$io->success(sprintf('Найдено пользователей для удаления: %d', $deletedCount));
} else {
$io->success(sprintf('Успешно удалено пользователей: %d', $deletedCount));
}
return Command::SUCCESS;
} catch (\Exception $e) {
$io->error(sprintf('Ошибка при выполнении очистки: %s', $e->getMessage()));
$io->error($e->getTraceAsString());
return Command::FAILURE;
}
}
}