|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright (c) 2021 TechDivision GmbH <[email protected]> - TechDivision GmbH |
| 4 | + * All rights reserved |
| 5 | + * |
| 6 | + * This product includes proprietary software developed at TechDivision GmbH, Germany |
| 7 | + * For more information see http://www.techdivision.com/ |
| 8 | + * |
| 9 | + * To obtain a valid license for using this software please contact us at |
| 10 | + |
| 11 | + */ |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Firegento\ContentProvisioning\Model\Console; |
| 15 | + |
| 16 | +use Firegento\ContentProvisioning\Api\Data\BlockEntryInterface; |
| 17 | +use Firegento\ContentProvisioning\Model\Command\ApplyBlockEntry; |
| 18 | +use Firegento\ContentProvisioning\Model\Command\ApplyMediaFiles; |
| 19 | +use Firegento\ContentProvisioning\Model\Query\GetBlockEntryList; |
| 20 | +use Magento\Framework\Console\Cli; |
| 21 | +use Symfony\Component\Console\Command\Command; |
| 22 | +use Symfony\Component\Console\Input\InputInterface; |
| 23 | +use Symfony\Component\Console\Input\InputOption; |
| 24 | +use Symfony\Component\Console\Output\OutputInterface; |
| 25 | + |
| 26 | +/** |
| 27 | + * @copyright Copyright (c) 2021 TechDivision GmbH <[email protected]> - TechDivision GmbH |
| 28 | + * |
| 29 | + * @link https://www.techdivision.com/ |
| 30 | + * @author Team Zero <[email protected]> |
| 31 | + */ |
| 32 | +class BlockResetCommand extends Command |
| 33 | +{ |
| 34 | + public const COMMAND = 'content-provisioning:block:reset'; |
| 35 | + public const PARAM_KEY = 'key'; |
| 36 | + public const PARAM_IDENTIFIER = 'identifier'; |
| 37 | + |
| 38 | + private GetBlockEntryList $getBlockEntryList; |
| 39 | + private ApplyBlockEntry $applyBlockEntry; |
| 40 | + private ApplyMediaFiles $applyMediaFiles; |
| 41 | + |
| 42 | + /** |
| 43 | + * @param GetBlockEntryList $getBlockEntryList |
| 44 | + * @param ApplyBlockEntry $applyBlockEntry |
| 45 | + * @param ApplyMediaFiles $applyMediaFiles |
| 46 | + */ |
| 47 | + public function __construct( |
| 48 | + GetBlockEntryList $getBlockEntryList, |
| 49 | + ApplyBlockEntry $applyBlockEntry, |
| 50 | + ApplyMediaFiles $applyMediaFiles |
| 51 | + ) { |
| 52 | + parent::__construct(); |
| 53 | + |
| 54 | + $this->getBlockEntryList = $getBlockEntryList; |
| 55 | + $this->applyBlockEntry = $applyBlockEntry; |
| 56 | + $this->applyMediaFiles = $applyMediaFiles; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Configures the current command. |
| 61 | + */ |
| 62 | + protected function configure(): void |
| 63 | + { |
| 64 | + $this->setName(self::COMMAND); |
| 65 | + $this->setDescription('Reset CMS content'); |
| 66 | + $this->addOption(self::PARAM_KEY, 'k', InputOption::VALUE_OPTIONAL, 'Key'); |
| 67 | + $this->addOption(self::PARAM_IDENTIFIER, 'i', InputOption::VALUE_OPTIONAL, 'Identifier'); |
| 68 | + |
| 69 | + parent::configure(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @param InputInterface $input |
| 74 | + * @param OutputInterface $output |
| 75 | + * @return int |
| 76 | + */ |
| 77 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 78 | + { |
| 79 | + try { |
| 80 | + [$search, $type] = $this->parseParams($input); |
| 81 | + } catch (\Exception $e) { |
| 82 | + $output->writeln('<error>' . $e->getMessage() . '</error>' . "\n"); |
| 83 | + return Cli::RETURN_FAILURE; |
| 84 | + } |
| 85 | + |
| 86 | + $blockEntries = $this->getBlockEntries($search, $type); |
| 87 | + $updateCount = 0; |
| 88 | + |
| 89 | + if (empty($blockEntries)) { |
| 90 | + $output->writeln('<error>block entry not found for ' . $type . ' "' . $search . '"</error>' . "\n"); |
| 91 | + return Cli::RETURN_FAILURE; |
| 92 | + } |
| 93 | + |
| 94 | + try { |
| 95 | + foreach ($blockEntries as $blockEntry) { |
| 96 | + $this->applyBlockEntry->execute($blockEntry); |
| 97 | + $this->applyMediaFiles->execute($blockEntry); |
| 98 | + $updateCount++; |
| 99 | + } |
| 100 | + } catch (\Exception $exception) { |
| 101 | + $output->writeln('<error>' . $exception->getMessage() . '</error>' . "\n"); |
| 102 | + return Cli::RETURN_FAILURE; |
| 103 | + } |
| 104 | + |
| 105 | + $pluralS = ($updateCount > 1) ? 's' : ''; |
| 106 | + $output->writeln('<info>' . $updateCount . ' block' . $pluralS . ' successfully updated</info>'); |
| 107 | + |
| 108 | + return Cli::RETURN_SUCCESS; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param InputInterface $input |
| 113 | + * @return string[] |
| 114 | + * @throws \Exception |
| 115 | + */ |
| 116 | + private function parseParams(InputInterface $input): array |
| 117 | + { |
| 118 | + $key = $input->getOption(self::PARAM_KEY); |
| 119 | + $identifier = $input->getOption(self::PARAM_IDENTIFIER); |
| 120 | + |
| 121 | + if ((!empty($key)) && (!empty($identifier))) { |
| 122 | + throw new \Exception('Provide either "' . self::PARAM_KEY . '" or "' . self::PARAM_IDENTIFIER . '", not both!'); |
| 123 | + } |
| 124 | + |
| 125 | + if (!empty($key)) { |
| 126 | + $search = $key; |
| 127 | + $type = self::PARAM_KEY; |
| 128 | + } elseif (!empty($identifier)) { |
| 129 | + $search = $identifier; |
| 130 | + $type = self::PARAM_IDENTIFIER; |
| 131 | + } else { |
| 132 | + throw new \Exception('Provide either "' . self::PARAM_KEY . '" or "' . self::PARAM_IDENTIFIER . '"!'); |
| 133 | + } |
| 134 | + |
| 135 | + return [$search, $type]; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @param string $search |
| 140 | + * @param string $type |
| 141 | + * @return BlockEntryInterface[] |
| 142 | + */ |
| 143 | + private function getBlockEntries(string $search, string $type = self::PARAM_KEY): array |
| 144 | + { |
| 145 | + if (!in_array($type, [self::PARAM_KEY, self::PARAM_IDENTIFIER])) { |
| 146 | + return []; |
| 147 | + } |
| 148 | + |
| 149 | + $method = 'get' . ucfirst($type); |
| 150 | + $entries = []; |
| 151 | + |
| 152 | + foreach ($this->getBlockEntryList->get() as $blockEntry) { |
| 153 | + if ($blockEntry->$method() === $search) { |
| 154 | + $entries[] = $blockEntry; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return $entries; |
| 159 | + } |
| 160 | +} |
0 commit comments