templateCollection = $templateCollection;
$this->compatibilityChecker = $compatibilityChecker;
}
/**
* @inheritdoc
*/
protected function configure()
{
$this->setName('dev:email:override-compatibility-check')
->setDescription('Scans email template overrides for potential variable usage compatibility issues');
}
/**
* @inheritDoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$collection = $this->templateCollection->create();
$collection->load();
$this->hasErrors = false;
foreach ($collection as $template) {
$this->checkTemplate($template, $output);
}
if (!$this->hasErrors) {
$output->writeln('No errors detected');
}
return $this->hasErrors ? Cli::RETURN_FAILURE : Cli::RETURN_SUCCESS;
}
/**
* Check the given template for compatibility issues
*
* @param AbstractTemplate $template
* @param OutputInterface $output
*/
protected function checkTemplate(AbstractTemplate $template, OutputInterface $output): void
{
$errors = $this->compatibilityChecker->getCompatibilityIssues($template->getTemplateText());
$templateName = $template->getTemplateCode();
if (!empty($errors)) {
$this->hasErrors = true;
$output->writeln(
'Template "' . $templateName . '" has the following compatibility issues:'
);
$this->renderErrors($output, $errors);
}
$errors = $this->compatibilityChecker->getCompatibilityIssues($template->getTemplateSubject());
if (!empty($errors)) {
$this->hasErrors = true;
$output->writeln(
'Template "' . $templateName . '" subject has the following compatibility issues:'
);
$this->renderErrors($output, $errors);
}
}
/**
* Render given errors
*
* @param OutputInterface $output
* @param array $errors
*/
private function renderErrors(OutputInterface $output, array $errors): void
{
foreach ($errors as $error) {
$error = str_replace(PHP_EOL, PHP_EOL . ' ', $error ?? '');
$output->writeln(
' - ' . $error . ''
);
}
$output->writeln('');
}
}