setDescription('Rebuild downloadable specification ZIP archives from parsed specification files.');
$this->addOption(
self::OPTION_BRAND_ID,
null,
InputOption::VALUE_REQUIRED,
'Only rebuild the specified specification brand ID.'
);
$this->addOption(
self::OPTION_FORCE,
'f',
InputOption::VALUE_NONE,
'Overwrite an existing valid brand archive.'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$brandIdOption = $input->getOption(self::OPTION_BRAND_ID);
$brandId = $brandIdOption !== null ? filter_var($brandIdOption, FILTER_VALIDATE_INT) : null;
if ($brandIdOption !== null && (!$brandId || $brandId < 1)) {
$output->writeln('The --brand-id option must be a positive integer.');
return Cli::RETURN_FAILURE;
}
$collection = $this->brandCollectionFactory->create()
->addFieldToFilter('status', 1)
->setOrder('sort_order', 'ASC')
->setOrder('brand_name', 'ASC');
if ($brandId !== null) {
$collection->addFieldToFilter('brand_id', $brandId);
}
if (!$collection->getSize()) {
$output->writeln('No enabled specification brands matched the command options.');
return Cli::RETURN_FAILURE;
}
$force = (bool)$input->getOption(self::OPTION_FORCE);
$failed = 0;
foreach ($collection as $brand) {
$brandName = (string)$brand->getData('brand_name');
try {
$result = $this->archiveRebuilder->rebuild($brand, $force);
if ($result['status'] === 'skipped') {
$output->writeln(sprintf('Skipped %s: archive already exists.', $brandName));
continue;
}
$message = sprintf(
'%s %s: %d file(s), %d missing file(s), %s',
ucfirst($result['status']),
$brandName,
$result['file_count'],
$result['missing_count'],
$result['path']
);
$output->writeln('' . $message . '');
} catch (\Throwable $exception) {
$failed++;
$output->writeln(sprintf('Failed %s: %s', $brandName, $exception->getMessage()));
}
}
return $failed === 0 ? Cli::RETURN_SUCCESS : Cli::RETURN_FAILURE;
}
}