diff --git a/Console/ExportItem.php b/Console/ExportItem.php new file mode 100644 index 0000000..51f4432 --- /dev/null +++ b/Console/ExportItem.php @@ -0,0 +1,150 @@ +getBlockByIdentifier = $getBlockByIdentifier; + $this->getPageByIdentifier = $getPageByIdentifier; + parent::__construct($name); + } + + protected function configure() + { + $this->setName(self::COMMAND_NAME); + $this->setDescription('Export CMS object data as a PHP array for use with simple data scripts'); + $this->setDefinition( + [ + new InputArgument( + self::TYPE_ARGUMENT, + InputArgument::REQUIRED + ), + new InputArgument( + self::IDENTIFIER_ARGUMENT, + InputArgument::REQUIRED + ), + new InputArgument( + self::STORE_ARGUMENT, + InputArgument::OPTIONAL, + 'Store Id', + Store::DEFAULT_STORE_ID + ) + ] + ); + + parent::configure(); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $entityType = $input->getArgument(self::TYPE_ARGUMENT); + $identifier = $input->getArgument(self::IDENTIFIER_ARGUMENT); + $storeId = $input->getArgument(self::STORE_ARGUMENT); + $errors = $this->validate($entityType); + if ($errors) { + foreach ($errors as $error) { + $output->writeln(''.$error.''); + } + return Cli::RETURN_FAILURE; + } + try { + if ($entityType == self::TYPE_BLOCK) { + $output->writeln($this->generateBlock($identifier, $storeId)); + } + if ($entityType == self::TYPE_PAGE) { + $output->writeln($this->generatePage($identifier, $storeId)); + } + } catch (NoSuchEntityException $nse) { + $output->writeln("No {$entityType} with the identifier {$identifier} was found"); + return Cli::RETURN_FAILURE; + } + return Cli::RETURN_SUCCESS; + } + + private function validate(string $entityType) + { + $errors = []; + if ($entityType != self::TYPE_BLOCK && $entityType != self::TYPE_PAGE) { + $errors[] = "Invalid value for type must be one of: page, block"; + } + return $errors; + } + + /** + * @param string $identifier + * @return string + * @throws NoSuchEntityException + */ + private function generatePage(string $identifier, int $storeId) + { + $page = $this->getPageByIdentifier->execute($identifier, $storeId); + return sprintf( + "[ + 'identifier' => '%s', + 'title' => '%s', + 'content' => <<getTitle(), + $page->getContent() + ); + } + + /** + * @param string $identifier + * @return string + * @throws NoSuchEntityException + */ + private function generateBlock(string $identifier, int $storeId) + { + $block = $this->getBlockByIdentifier->execute($identifier, $storeId); + return sprintf( + "[ + 'identifier' => '%s', + 'title' => '%s', + 'content' => <<getTitle(), + $block->getContent() + ); + } + } diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..189b365 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,13 @@ + + + + + + + MarkShust\SimpleData\Console\ExportItem + + + + +