setName("ot:clear-obsoletes-websites") ->setDescription("Delete obsolete websites") ->setHelp(" This CLI command loop over the ot_websites and delete the ones whom organization doesn't exist anymore") ->addOption( 'preview', 'p', InputOption::VALUE_NONE, "Only preview, perform no deletion" ); } /** * -- This method is expected by Typo3, do not rename ou remove -- * * @param InputInterface $input * @param OutputInterface $output * @return int * @throws NoSuchOrganizationException * @throws \Doctrine\DBAL\ConnectionException * @throws \Doctrine\DBAL\DBALException * @throws \Opentalent\OtCore\Exception\InvalidWebsiteConfigurationException * @throws \Opentalent\OtCore\Exception\NoSuchRecordException * @throws \Opentalent\OtCore\Exception\NoSuchWebsiteException * @throws \TYPO3\CMS\Extbase\Object\Exception * @throws \Throwable */ protected function execute(InputInterface $input, OutputInterface $output) { $preview = $input->getOption('preview'); $io = new SymfonyStyle($input, $output); $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites'); $sites = $queryBuilder ->select('organization_id') ->from('ot_websites') ->where($queryBuilder->expr()->eq('deleted', 0)) ->execute() ->fetchAll(); $io->progressStart(count($sites)); $n = 0; foreach ($sites as $site) { try { $this->siteController->fetchOrganization($site['organization_id']); } catch (NoSuchOrganizationException $e) { $n++; if ($preview) { $io->info('Site ' . $site['uid'] . ' to delete : ' . $site['organization_id'] . ' [' . $site['organization_name'] . ']'); continue; } $this->siteController->deleteSiteAction($site['organization_id']); $io->info('Website ' . $site['uid'] . ' deleted : ' . $site['organization_id'] . ' [' . $site['organization_name'] . ']'); } /** @noinspection DisconnectedForeachInstructionInspection */ $io->progressAdvance(); } $io->progressFinish(); if (!$preview) { $io->success(sprintf($n . " websites have been successfully deleted")); } else { $io->success(sprintf("Preview ended -- " . $n . ' websites would be deleted')); } return Command::SUCCESS; } }