AddSubdomainCommand.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Commands;
  3. use App\Repository\Organization\OrganizationRepository;
  4. use App\Service\Typo3\SubdomainService;
  5. use Symfony\Component\Console\Attribute\AsCommand;
  6. use Symfony\Component\Console\Command\Command;
  7. use Symfony\Component\Console\Exception\InvalidArgumentException;
  8. use Symfony\Component\Console\Input\InputArgument;
  9. use Symfony\Component\Console\Input\InputInterface;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11. #[AsCommand(
  12. name: 'ot:subdomain:add',
  13. description: 'Add a subdomain to the given organization and make it the active one'
  14. )]
  15. class AddSubdomainCommand extends Command
  16. {
  17. public function __construct(
  18. private readonly OrganizationRepository $organizationRepository,
  19. private readonly SubdomainService $subdomainService
  20. ) {
  21. parent::__construct();
  22. }
  23. /**
  24. * @return void
  25. */
  26. protected function configure(): void
  27. {
  28. $this->addArgument('organization-id', InputArgument::REQUIRED, 'Id of the organization');
  29. $this->addArgument('subdomain', InputArgument::REQUIRED, 'The new active subdomain');
  30. }
  31. protected function execute(InputInterface $input, OutputInterface $output): int
  32. {
  33. $organizationId = $input->getArgument('organization-id');
  34. if (!is_numeric($organizationId)) {
  35. throw new InvalidArgumentException('Invalid organization id : ' . $organizationId);
  36. }
  37. $organization = $this->organizationRepository->find((int)$organizationId);
  38. $subdomainValue = $input->getArgument('subdomain');
  39. $output->writeln("Setting up a new subdomain for organization " . $organizationId . " : " . $subdomainValue);
  40. $this->subdomainService->addNewSubdomain($organization, $subdomainValue, true);
  41. $output->writeln("New subdomain added and activated");
  42. return Command::SUCCESS;
  43. }
  44. }