diff --git a/Model/Console/BlockResetCommand.php b/Model/Console/BlockResetCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..e3c9947bc7db01ae99d0e9225b23c51f78dd003e --- /dev/null +++ b/Model/Console/BlockResetCommand.php @@ -0,0 +1,160 @@ +<?php +/** + * Copyright (c) 2021 TechDivision GmbH <info@techdivision.com> - TechDivision GmbH + * All rights reserved + * + * This product includes proprietary software developed at TechDivision GmbH, Germany + * For more information see http://www.techdivision.com/ + * + * To obtain a valid license for using this software please contact us at + * license@techdivision.com + */ +declare(strict_types=1); + +namespace Firegento\ContentProvisioning\Model\Console; + +use Firegento\ContentProvisioning\Api\Data\BlockEntryInterface; +use Firegento\ContentProvisioning\Model\Command\ApplyBlockEntry; +use Firegento\ContentProvisioning\Model\Command\ApplyMediaFiles; +use Firegento\ContentProvisioning\Model\Query\GetBlockEntryList; +use Magento\Framework\Console\Cli; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * @copyright Copyright (c) 2021 TechDivision GmbH <info@techdivision.com> - TechDivision GmbH + * + * @link https://www.techdivision.com/ + * @author Team Zero <zero@techdivision.com> + */ +class BlockResetCommand extends Command +{ + public const COMMAND = 'content-provisioning:block:reset'; + public const PARAM_KEY = 'key'; + public const PARAM_IDENTIFIER = 'identifier'; + + private GetBlockEntryList $getBlockEntryList; + private ApplyBlockEntry $applyBlockEntry; + private ApplyMediaFiles $applyMediaFiles; + + /** + * @param GetBlockEntryList $getBlockEntryList + * @param ApplyBlockEntry $applyBlockEntry + * @param ApplyMediaFiles $applyMediaFiles + */ + public function __construct( + GetBlockEntryList $getBlockEntryList, + ApplyBlockEntry $applyBlockEntry, + ApplyMediaFiles $applyMediaFiles + ) { + parent::__construct(); + + $this->getBlockEntryList = $getBlockEntryList; + $this->applyBlockEntry = $applyBlockEntry; + $this->applyMediaFiles = $applyMediaFiles; + } + + /** + * Configures the current command. + */ + protected function configure(): void + { + $this->setName(self::COMMAND); + $this->setDescription('Reset CMS content'); + $this->addOption(self::PARAM_KEY, 'k', InputOption::VALUE_OPTIONAL, 'Key'); + $this->addOption(self::PARAM_IDENTIFIER, 'i', InputOption::VALUE_OPTIONAL, 'Identifier'); + + parent::configure(); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + try { + [$search, $type] = $this->parseParams($input); + } catch (\Exception $e) { + $output->writeln('<error>' . $e->getMessage() . '</error>' . "\n"); + return Cli::RETURN_FAILURE; + } + + $blockEntries = $this->getBlockEntries($search, $type); + $updateCount = 0; + + if (empty($blockEntries)) { + $output->writeln('<error>block entry not found for ' . $type . ' "' . $search . '"</error>' . "\n"); + return Cli::RETURN_FAILURE; + } + + try { + foreach ($blockEntries as $blockEntry) { + $this->applyBlockEntry->execute($blockEntry); + $this->applyMediaFiles->execute($blockEntry); + $updateCount++; + } + } catch (\Exception $exception) { + $output->writeln('<error>' . $exception->getMessage() . '</error>' . "\n"); + return Cli::RETURN_FAILURE; + } + + $pluralS = ($updateCount > 1) ? 's' : ''; + $output->writeln('<info>' . $updateCount . ' block' . $pluralS . ' successfully updated</info>'); + + return Cli::RETURN_SUCCESS; + } + + /** + * @param InputInterface $input + * @return string[] + * @throws \Exception + */ + private function parseParams(InputInterface $input): array + { + $key = $input->getOption(self::PARAM_KEY); + $identifier = $input->getOption(self::PARAM_IDENTIFIER); + + if ((!empty($key)) && (!empty($identifier))) { + throw new \Exception('Provide either "' . self::PARAM_KEY . '" or "' . self::PARAM_IDENTIFIER . '", not both!'); + } + + if (!empty($key)) { + $search = $key; + $type = self::PARAM_KEY; + } elseif (!empty($identifier)) { + $search = $identifier; + $type = self::PARAM_IDENTIFIER; + } else { + throw new \Exception('Provide either "' . self::PARAM_KEY . '" or "' . self::PARAM_IDENTIFIER . '"!'); + } + + return [$search, $type]; + } + + /** + * @param string $search + * @param string $type + * @return BlockEntryInterface[] + */ + private function getBlockEntries(string $search, string $type = self::PARAM_KEY): array + { + if (!in_array($type, [self::PARAM_KEY, self::PARAM_IDENTIFIER])) { + return []; + } + + $method = 'get' . ucfirst($type); + $entries = []; + + foreach ($this->getBlockEntryList->get() as $blockEntry) { + if ($blockEntry->$method() === $search) { + $entries[] = $blockEntry; + } + } + + return $entries; + } +} diff --git a/README.md b/README.md index 2ee8dc68f73fc127f7fad3cebd1ee8bf4f04f7ef..374adbfa9fa1bd69463f557c46926acdde767849 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,6 @@ Only the latest patch versions of the following Magento versions are covered by | PHP | Magento 2.3 | Magento 2.4 | |:---: |:---:|:---:| -| 7.3 | [](https://travis-ci.org/magento-hackathon/m2-content-provisioning) | [](https://travis-ci.org/magento-hackathon/m2-content-provisioning) | | 7.4 | - | [](https://travis-ci.org/magento-hackathon/m2-content-provisioning) | ## The idea behind this module @@ -168,6 +167,29 @@ vi dev/tests/integration/etc/install-config-mysql.php php vendor/bin/phpunit -c $(pwd)/vendor/firegento/magento2-content-provisioning/Test/Integration/phpunit.xml ``` +## Console Commands +```shell +# reset a CMS block (all localizations) by its key +bin/magento content-provisioning:block:reset --key "myKey" +bin/magento content-provisioning:block:reset -k "myKey" + +# reset a CMS blocks by its identifier +bin/magento content-provisioning:block:reset --identifier "myIdentifier" +bin/magento content-provisioning:block:reset --i "myIdentifier" + +# add a CMS block by key +bin/magento content-provisioning:block:apply "myKey" + +# add a CMS page by key +bin/magento content-provisioning:page:apply "myKey" + +# list all configured CMS block entries +bin/magento content-provisioning:block:list + +# list all configured CMS page entries +bin/magento content-provisioning:page:list +``` + ## Issues and planned features See issues to see what's planed next: https://github.com/magento-hackathon/m2-content-provisioning/issues diff --git a/etc/di.xml b/etc/di.xml index 7222ea25a86eb149ac0fdc42bc81ba3c124075cb..dccdfbeaf058529d21b8d6908127e32a105f51b4 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -87,6 +87,7 @@ <item name="contentProvisioning.BlockList" xsi:type="object">Firegento\ContentProvisioning\Model\Console\BlockListCommand</item> <item name="contentProvisioning.PageAdd" xsi:type="object">Firegento\ContentProvisioning\Model\Console\AddPageCommand</item> <item name="contentProvisioning.BlockAdd" xsi:type="object">Firegento\ContentProvisioning\Model\Console\AddBlockCommand</item> + <item name="contentProvisioning.BlockReset" xsi:type="object">Firegento\ContentProvisioning\Model\Console\BlockResetCommand</item> </argument> </arguments> </type>