From 386430d84020b2f607d4098083df9bcf46f98584 Mon Sep 17 00:00:00 2001 From: BentiGorlich Date: Tue, 10 Dec 2024 16:53:44 +0100 Subject: [PATCH] Add a magazine create command (#1273) --- src/Command/MagazineCreateCommand.php | 102 ++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/Command/MagazineCreateCommand.php diff --git a/src/Command/MagazineCreateCommand.php b/src/Command/MagazineCreateCommand.php new file mode 100644 index 000000000..2675a71a5 --- /dev/null +++ b/src/Command/MagazineCreateCommand.php @@ -0,0 +1,102 @@ +addArgument('name', InputArgument::REQUIRED) + ->addOption('owner', 'o', InputOption::VALUE_REQUIRED, 'the owner of the magazine') + ->addOption('remove', 'r', InputOption::VALUE_NONE, 'Remove the magazine') + ->addOption('purge', null, InputOption::VALUE_NONE, 'Purge the magazine') + ->addOption('restricted', null, InputOption::VALUE_NONE, 'Restrict the creation of threads to moderators') + ->addOption('title', 't', InputOption::VALUE_REQUIRED, 'the title of the magazine') + ->addOption('description', 'd', InputOption::VALUE_REQUIRED, 'the description of the magazine') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + $remove = $input->getOption('remove'); + $purge = $input->getOption('purge'); + $restricted = $input->getOption('restricted'); + $ownerInput = $input->getOption('owner'); + if ($ownerInput) { + $user = $this->userRepository->findOneByUsername($ownerInput); + if (null === $user) { + $io->error(\sprintf('There is no user named: "%s"', $input->getArgument('owner'))); + + return Command::FAILURE; + } + } else { + $user = $this->userRepository->findAdmin(); + } + $magazineName = $input->getArgument('name'); + $existing = $this->magazineRepository->findOneBy(['name' => $magazineName, 'apId' => null]); + if ($remove || $purge) { + if (null !== $existing) { + if ($remove) { + $this->magazineManager->delete($existing); + $io->success(\sprintf('The magazine "%s" has been removed.', $magazineName)); + + return Command::SUCCESS; + } else { + $this->magazineManager->purge($existing); + $io->success(\sprintf('The magazine "%s" has been purged.', $magazineName)); + + return Command::SUCCESS; + } + } else { + $io->error(\sprintf('There is no magazine named: "%s"', $magazineName)); + + return Command::FAILURE; + } + } + + if (null !== $existing) { + $io->error(\sprintf('There already is a magazine called "%s"', $magazineName)); + + return Command::FAILURE; + } + + $dto = new MagazineDto(); + $dto->name = $magazineName; + $dto->title = $input->getOption('title') ?? $magazineName; + $dto->description = $input->getOption('description'); + $dto->isPostingRestrictedToMods = $restricted; + + $magazine = $this->magazineManager->create($dto, $user, rateLimit: false); + $io->success(\sprintf('The magazine "%s" was created successfully', $magazine->name)); + + return Command::SUCCESS; + } +}