Skip to content
107 changes: 30 additions & 77 deletions Command/ExportCommand.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Sherlockode\AdvancedContentBundle\Command;

use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -15,70 +17,29 @@

class ExportCommand extends Command
{
const AVAILABLE_ENTITIES = ['Page', 'Content'];

/**
* @var EntityManagerInterface
*/
private $em;

/**
* @var ConfigurationManager
*/
private $configurationManager;

/**
* @var TranslatorInterface
*/
private $translator;

/**
* @var string
*/
private $rootDir;

/**
* @var SymfonyStyle
*/
private $symfonyStyle;
public const AVAILABLE_ENTITIES = ['Page', 'Content'];

/**
* @var string
*/
private $sourceDirectory;
private ?SymfonyStyle $symfonyStyle = null;

/**
* @var ExportManager
*/
private $exportManager;
private ?string $sourceDirectory = null;

/**
* @var array
*/
private $exportTypes = [];

/**
* @param EntityManagerInterface $em
* @param ConfigurationManager $configurationManager
* @param TranslatorInterface $translator
* @param ExportManager $exportManager
* @param string $rootDir
* @param null|string $name
* @param string $rootDir
*/
public function __construct(
EntityManagerInterface $em,
ConfigurationManager $configurationManager,
TranslatorInterface $translator,
ExportManager $exportManager,
$rootDir,
$name = null
private readonly EntityManagerInterface $em,
private readonly ConfigurationManager $configurationManager,
private readonly TranslatorInterface $translator,
private readonly ExportManager $exportManager,
private $rootDir,
?string $name = null,
) {
parent::__construct($name);
$this->em = $em;
$this->configurationManager = $configurationManager;
$this->exportManager = $exportManager;
$this->translator = $translator;
$this->rootDir = $rootDir;
}

protected function configure()
Expand All @@ -103,12 +64,9 @@ protected function configure()
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return void
*/
protected function execute(InputInterface $input, OutputInterface $output)
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->symfonyStyle = new SymfonyStyle($input, $output);
try {
Expand All @@ -118,67 +76,62 @@ protected function execute(InputInterface $input, OutputInterface $output)
$pages = $this->em->getRepository($this->configurationManager->getEntityClass('page'))->findAll();
$this->exportManager->generatePagesData($pages);
}

if (in_array('Content', $this->exportTypes)) {
$contents = $this->em->getRepository($this->configurationManager->getEntityClass('content'))->findAll();
$contentsToExport = [];
foreach ($contents as $content) {
if ($content->getPage() instanceof PageInterface) {
continue;
}

$contentsToExport[] = $content;
}

$this->exportManager->generateContentsData($contentsToExport);
}

$this->exportManager->generateFiles($this->sourceDirectory);

$this->symfonyStyle->success($this->translator->trans('init.export_success', ['%dir%' => $this->sourceDirectory], 'AdvancedContentBundle'));
} catch (\Exception $e) {
$this->symfonyStyle->error($e->getMessage());
} catch (\Exception $exception) {
$this->symfonyStyle->error($exception->getMessage());

if (defined(sprintf('%s::FAILURE', get_class($this)))) {
return self::FAILURE;
}

return;
return self::FAILURE;
}

if (defined(sprintf('%s::SUCCESS', get_class($this)))) {
return self::SUCCESS;
}
return self::SUCCESS;
}

/**
* @param InputInterface $input
*
* @throws \Exception
*/
private function init(InputInterface $input)
private function init(InputInterface $input): void
{
$initDir = $input->getOption('dir');
if ($initDir === null) {
if (null === $initDir) {
$initDir = $this->configurationManager->getInitDirectory();
}
if (strpos($initDir, '/') !== 0) {
$initDir = $this->rootDir . '/' . $initDir;

if (!str_starts_with((string) $initDir, '/')) {
$initDir = $this->rootDir.'/'.$initDir;
}

$initDir .= '/';

if (!file_exists($initDir)) {
throw new \Exception(
$this->translator->trans('init.errors.init_dir', ['%dir%' => $initDir], 'AdvancedContentBundle')
);
throw new \Exception($this->translator->trans('init.errors.init_dir', ['%dir%' => $initDir], 'AdvancedContentBundle'));
}

$this->sourceDirectory = $initDir;

$exportTypes = $input->getOption('type');
foreach ($exportTypes as $exportType) {
if (!in_array($exportType, self::AVAILABLE_ENTITIES)) {
throw new \Exception(
$this->translator->trans('init.errors.unknown_entity_type', ['%type%' => $exportType, '%list%' => join(', ', self::AVAILABLE_ENTITIES)], 'AdvancedContentBundle')
);
throw new \Exception($this->translator->trans('init.errors.unknown_entity_type', ['%type%' => $exportType, '%list%' => implode(', ', self::AVAILABLE_ENTITIES)], 'AdvancedContentBundle'));
}
}

$this->exportTypes = $exportTypes;
}
}
Loading