addArgument('domain', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Domain(s) served by this tenant') ->addOption('label', 'l', InputOption::VALUE_REQUIRED, 'Display label for the tenant') ->addOption('description', 'd', InputOption::VALUE_REQUIRED, 'Tenant description') ->addOption('identifier', null, InputOption::VALUE_REQUIRED, 'Explicit tenant identifier (defaults to a generated UUID)') ->addOption('disabled', null, InputOption::VALUE_NONE, 'Create the tenant in a disabled state') ->setHelp('This command creates a new tenant with one or more domains. The tenant identifier is generated automatically unless --identifier is provided.') ; } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); $identifier = $input->getOption('identifier') ?? UUID::v4(); $domains = array_values(array_unique(array_filter(array_map('trim', $input->getArgument('domain'))))); $io->title('Create Tenant'); try { if (empty($domains)) { $io->error('At least one non-empty domain is required.'); return Command::FAILURE; } // Ensure identifier is unique if ($this->tenantService->fetchById($identifier)) { $io->error("Tenant '{$identifier}' already exists."); return Command::FAILURE; } // Ensure domains are not claimed by another tenant foreach ($domains as $domain) { $existing = $this->tenantService->fetchByDomain($domain); if ($existing) { $io->error("Domain '{$domain}' is already assigned to tenant '{$existing->getIdentifier()}'."); return Command::FAILURE; } } $tenant = new TenantObject(); $tenant->setIdentifier($identifier); $tenant->setEnabled(!$input->getOption('disabled')); $tenant->setLabel($input->getOption('label') ?? $domains[0]); $tenant->setDescription($input->getOption('description') ?? ''); $tenant->setDomains(new DomainCollection($domains)); $tenant->setConfiguration(new TenantConfiguration()); $tenant = $this->tenantService->deposit($tenant); if (!$tenant) { $io->error('Failed to create tenant.'); return Command::FAILURE; } $this->logger->info('Tenant created via console', [ 'identifier' => $identifier, 'command' => $this->getName(), ]); $io->success("Tenant '{$identifier}' created successfully!"); $io->definitionList( ['Id' => $tenant->getId()], ['Identifier' => $tenant->getIdentifier()], ['Label' => $tenant->getLabel()], ['Enabled' => $tenant->getEnabled() ? 'yes' : 'no'], ['Domains' => implode(', ', $domains)], ); return Command::SUCCESS; } catch (\Throwable $e) { $io->error('Failed to create tenant: ' . $e->getMessage()); $this->logger->error('Tenant create failed', [ 'identifier' => $identifier, 'error' => $e->getMessage(), ]); return Command::FAILURE; } } }