From e8da83f4f3a8a267f78b9ec9ea44f53983dc7b2f Mon Sep 17 00:00:00 2001 From: Sven Reichel Date: Mon, 16 Oct 2023 22:45:38 +0200 Subject: [PATCH] [skip ci] Updated cms:block commands --- .../Cms/Block/AbstractCmsBlockCommand.php | 29 ++++++ .../Magento/Command/Cms/Block/ListCommand.php | 92 +++++++++---------- .../Command/Cms/Block/ToggleCommand.php | 82 +++++++++-------- 3 files changed, 115 insertions(+), 88 deletions(-) create mode 100644 src/N98/Magento/Command/Cms/Block/AbstractCmsBlockCommand.php diff --git a/src/N98/Magento/Command/Cms/Block/AbstractCmsBlockCommand.php b/src/N98/Magento/Command/Cms/Block/AbstractCmsBlockCommand.php new file mode 100644 index 000000000..919ee0c99 --- /dev/null +++ b/src/N98/Magento/Command/Cms/Block/AbstractCmsBlockCommand.php @@ -0,0 +1,29 @@ +_getModel('cms/block'); + if (!$model instanceof Mage_Cms_Model_Block) { + throw new UnexpectedValueException($model, Mage_Cms_Model_Block::class); + } + return $model; + } +} diff --git a/src/N98/Magento/Command/Cms/Block/ListCommand.php b/src/N98/Magento/Command/Cms/Block/ListCommand.php index b0874e621..77fad829f 100644 --- a/src/N98/Magento/Command/Cms/Block/ListCommand.php +++ b/src/N98/Magento/Command/Cms/Block/ListCommand.php @@ -1,80 +1,70 @@ >|null */ - protected function configure() - { - $this - ->setName('cms:block:list') - ->setDescription('List all cms blocks') - ->addOption( - 'format', - null, - InputOption::VALUE_OPTIONAL, - 'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']' - ) - ; - } + private ?array $data = null; /** - * Get an instance of cms/block - * - * @return \Mage_Cms_Model_Block + * @var string + * @deprecated with symfony 6.1 + * @see AsCommand */ - protected function _getBlockModel() - { - return $this->_getModel('cms/block', '\Mage_Cms_Model_Block'); - } + protected static $defaultName = 'cms:block:list'; /** - * Execute the command - * - * @param InputInterface $input + * @var string + * @deprecated with symfony 6.1 + * @see AsCommand + */ + protected static $defaultDescription = 'List all cms blocks'; + + /** + * @param InputInterface $input * @param OutputInterface $output - * - * @return int + * @return array> + * @throws Mage_Core_Exception */ - protected function execute(InputInterface $input, OutputInterface $output): int + public function getData(InputInterface $input, OutputInterface $output): array { - $this->detectMagento($output, true); - if (!$this->initMagento()) { - return 0; - } - - $cmsBlockCollection = $this->_getBlockModel()->getCollection()->addFieldToSelect('*'); + if (is_null($this->data)) { + /** @var Mage_Cms_Model_Block[] $cmsBlockCollection */ + $cmsBlockCollection = $this->_getBlockModel()->getCollection()->addFieldToSelect('*'); - /** @var \Mage_Cms_Model_Resource_Block $resourceModel */ - $resourceModel = $this->_getBlockModel()->getResource(); + $resourceModel = $this->_getBlockModel()->getResource(); - $table = []; - foreach ($cmsBlockCollection as $cmsBlock) { - $storeIds = implode(',', $resourceModel->lookupStoreIds($cmsBlock->getId())); + $this->data = []; + foreach ($cmsBlockCollection as $cmsBlock) { + $storeIds = implode(',', $resourceModel->lookupStoreIds((int)$cmsBlock->getId())); - $table[] = [$cmsBlock->getData('block_id'), $cmsBlock->getData('identifier'), $cmsBlock->getData('title'), $cmsBlock->getData('is_active') ? 'active' : 'inactive', $storeIds]; + $this->data[] = [ + 'block_id' => $cmsBlock->getBlockId(), + 'title' => $cmsBlock->getTitle(), + 'identifier' => $cmsBlock->getIdentifier(), + 'is_active' => $cmsBlock->getIsActive() ? 'active' : 'inactive', + 'store_ids' => $storeIds + ]; + } } - /* @var TableHelper $tableHelper */ - $tableHelper = $this->getHelper('table'); - $tableHelper - ->setHeaders(['block_id', 'identifier', 'title', 'is_active', 'store_ids']) - ->renderByFormat($output, $table, $input->getOption('format')); - return 0; + return $this->data; } } diff --git a/src/N98/Magento/Command/Cms/Block/ToggleCommand.php b/src/N98/Magento/Command/Cms/Block/ToggleCommand.php index 50f9bf8cd..0ea4ba111 100644 --- a/src/N98/Magento/Command/Cms/Block/ToggleCommand.php +++ b/src/N98/Magento/Command/Cms/Block/ToggleCommand.php @@ -1,72 +1,80 @@ setName('cms:block:toggle') - ->addArgument('block_id', InputArgument::REQUIRED, 'Block ID or Identifier') - ->setDescription('Toggle a cms block') - ; - } + protected static $defaultName = 'cms:block:toggle'; + + /** + * @var string + * @deprecated with symfony 6.1 + * @see AsCommand + */ + protected static $defaultDescription = 'Toggle a cms block.'; /** - * Get an instance of cms/block - * - * @return \Mage_Cms_Model_Block + * @return void */ - protected function _getBlockModel() + protected function configure() { - return $this->_getModel('cms/block', '\Mage_Cms_Model_Block'); + $this->addArgument( + self::COMMAND_ARGUMENT_BLOCK_ID, + InputArgument::REQUIRED, + 'Block ID or Identifier' + ); } /** - * Execute the command - * - * @param InputInterface $input + * @param InputInterface $input * @param OutputInterface $output - * * @return int + * @throws Throwable */ protected function execute(InputInterface $input, OutputInterface $output): int { - $this->detectMagento($output, true); - if (!$this->initMagento()) { - return 0; - } - $blockId = $input->getArgument('block_id'); - if (is_numeric($blockId)) { - $block = $this->_getBlockModel()->load($blockId); - } else { - $block = $this->_getBlockModel()->load($blockId, 'identifier'); - } + $this->detectMagento($output); + $this->initMagento(); + + $blockId = $this->getArgumentString($input, self::COMMAND_ARGUMENT_BLOCK_ID); + $block = $this->_getBlockModel()->load($blockId, is_numeric($blockId) ? null : 'identifier'); + if (!$block->getId()) { - return (int) $output->writeln('Block was not found'); + $output->writeln('Block was not found'); + return Command::INVALID; } - $newStatus = !$block->getIsActive(); + $block - ->setIsActive($newStatus) + ->setIsActive(!$block->getIsActive()) ->save(); + $output->writeln(sprintf( - 'Block %s', - $newStatus ? 'enabled' : 'disabled' + 'Block "%s" %s', + $block->getTitle(), + $block->getIsActive() ? 'enabled' : 'disabled' )); - return 0; + + return Command::SUCCESS; } }