|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of the Content Fuzzyfyr module for Magento2. |
| 4 | + * |
| 5 | + * (c) All.In Data GmbH |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace AllInData\ContentFuzzyfyr\Console\Command; |
| 12 | + |
| 13 | +use AllInData\ContentFuzzyfyr\Handler\BackupHandler; |
| 14 | +use Magento\Framework\App\State; |
| 15 | +use Magento\Framework\EntityManager\EventManager; |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Input\InputOption; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | +use AllInData\ContentFuzzyfyr\Model\Configuration; |
| 21 | +use AllInData\ContentFuzzyfyr\Model\ConfigurationFactory; |
| 22 | + |
| 23 | +/** |
| 24 | + * Class ExportCommand |
| 25 | + * @package AllInData\ContentFuzzyfyr\Console\Command |
| 26 | + */ |
| 27 | +class ExportCommand extends Command |
| 28 | +{ |
| 29 | + /** |
| 30 | + * Codes |
| 31 | + */ |
| 32 | + const SUCCESS = 0; |
| 33 | + const ERROR_BACKUP_PATH_NOT_RESOLVABLE = 127; |
| 34 | + const ERROR_EXPORT_FAILED = 156; |
| 35 | + |
| 36 | + /** |
| 37 | + * Flags |
| 38 | + */ |
| 39 | + const FLAG_ONLY_EMPTY = 'only-empty'; |
| 40 | + const FLAG_CATEGORIES = 'categories'; |
| 41 | + const FLAG_CMS_BLOCKS = 'cms-blocks'; |
| 42 | + const FLAG_CMS_PAGES = 'cms-pages'; |
| 43 | + const FLAG_CUSTOMERS = 'customers'; |
| 44 | + const FLAG_PRODUCTS = 'products'; |
| 45 | + const FLAG_USERS = 'users'; |
| 46 | + |
| 47 | + /** |
| 48 | + * Options |
| 49 | + */ |
| 50 | + const OPTION_DUMMY_CONTENT_TEXT = 'dummy-content-text'; |
| 51 | + const OPTION_DUMMY_CONTENT_EMAIL = 'dummy-content-email'; |
| 52 | + const OPTION_DUMMY_CONTENT_URL = 'dummy-content-url'; |
| 53 | + const OPTION_DUMMY_CONTENT_PHONE = 'dummy-content-phone'; |
| 54 | + const OPTION_DUMP_OUTPUT = 'backup-output'; |
| 55 | + |
| 56 | + /** |
| 57 | + * Defaults |
| 58 | + */ |
| 59 | + const DEFAULT_DUMMY_CONTENT_TEXT = 'Lorem ipsum.'; |
| 60 | + const DEFAULT_DUMMY_CONTENT_EMAIL = 'lorem.ipsum.%1$s@test.localhost'; |
| 61 | + const DEFAULT_DUMMY_CONTENT_URL = 'https://lor.emips.um/foo/bar/'; |
| 62 | + const DEFAULT_DUMMY_CONTENT_PHONE = '+49 (0) 600 987 654 32'; |
| 63 | + const DEFAULT_DUMP_OUTPUT = './var/backup/'; |
| 64 | + |
| 65 | + /** |
| 66 | + * @var State |
| 67 | + */ |
| 68 | + private $state; |
| 69 | + /** |
| 70 | + * @var EventManager |
| 71 | + */ |
| 72 | + private $eventManager; |
| 73 | + /** |
| 74 | + * @var ConfigurationFactory |
| 75 | + */ |
| 76 | + private $configurationFactory; |
| 77 | + /** |
| 78 | + * @var BackupHandler |
| 79 | + */ |
| 80 | + private $backupHandler; |
| 81 | + |
| 82 | + /** |
| 83 | + * ExportCommand constructor. |
| 84 | + * @param State $state |
| 85 | + * @param EventManager $eventManager |
| 86 | + * @param ConfigurationFactory $configurationFactory |
| 87 | + * @param BackupHandler $backupHandler |
| 88 | + */ |
| 89 | + public function __construct( |
| 90 | + State $state, |
| 91 | + EventManager $eventManager, |
| 92 | + ConfigurationFactory $configurationFactory, |
| 93 | + BackupHandler $backupHandler |
| 94 | + ) { |
| 95 | + $this->state = $state; |
| 96 | + $this->eventManager = $eventManager; |
| 97 | + $this->configurationFactory = $configurationFactory; |
| 98 | + $this->backupHandler = $backupHandler; |
| 99 | + parent::__construct(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * {@inheritdoc} |
| 104 | + */ |
| 105 | + protected function configure() |
| 106 | + { |
| 107 | + $this->setName('aid:content:export') |
| 108 | + ->setDescription('Fuzzyfied Content export command by All.In Data GmbH') |
| 109 | + ->setDefinition([ |
| 110 | + new InputOption( |
| 111 | + self::FLAG_ONLY_EMPTY, |
| 112 | + null, |
| 113 | + InputOption::VALUE_NONE, |
| 114 | + 'Use dummy content only if the original data is equal to empty' |
| 115 | + ), |
| 116 | + new InputOption( |
| 117 | + self::FLAG_CATEGORIES, |
| 118 | + null, |
| 119 | + InputOption::VALUE_NONE, |
| 120 | + 'Apply dummy content to categories (content, meta description)' |
| 121 | + ), |
| 122 | + new InputOption( |
| 123 | + self::FLAG_CMS_BLOCKS, |
| 124 | + null, |
| 125 | + InputOption::VALUE_NONE, |
| 126 | + 'Apply dummy content to CMS Blocks (content)' |
| 127 | + ), |
| 128 | + new InputOption( |
| 129 | + self::FLAG_CMS_PAGES, |
| 130 | + null, |
| 131 | + InputOption::VALUE_NONE, |
| 132 | + 'Apply dummy content to CMS Pages (content, meta description)' |
| 133 | + ), |
| 134 | + new InputOption( |
| 135 | + self::FLAG_CUSTOMERS, |
| 136 | + null, |
| 137 | + InputOption::VALUE_NONE, |
| 138 | + 'Apply dummy content to customers (Last name, address, email)' |
| 139 | + ), |
| 140 | + new InputOption( |
| 141 | + self::FLAG_PRODUCTS, |
| 142 | + null, |
| 143 | + InputOption::VALUE_NONE, |
| 144 | + 'Apply dummy content to products (description)' |
| 145 | + ), |
| 146 | + new InputOption( |
| 147 | + self::FLAG_USERS, |
| 148 | + null, |
| 149 | + InputOption::VALUE_NONE, |
| 150 | + 'Apply dummy content to users (Last name, email)' |
| 151 | + ), |
| 152 | + new InputOption( |
| 153 | + self::OPTION_DUMMY_CONTENT_TEXT, |
| 154 | + null, |
| 155 | + InputOption::VALUE_OPTIONAL, |
| 156 | + sprintf('Used as dummy text content. Defaults to \'%s\'', self::DEFAULT_DUMMY_CONTENT_TEXT), |
| 157 | + self::DEFAULT_DUMMY_CONTENT_TEXT |
| 158 | + ), |
| 159 | + new InputOption( |
| 160 | + self::OPTION_DUMMY_CONTENT_EMAIL, |
| 161 | + null, |
| 162 | + InputOption::VALUE_OPTIONAL, |
| 163 | + sprintf('Used as dummy email content. Defaults to \'%s\'', self::DEFAULT_DUMMY_CONTENT_EMAIL), |
| 164 | + self::DEFAULT_DUMMY_CONTENT_EMAIL |
| 165 | + ), |
| 166 | + new InputOption( |
| 167 | + self::OPTION_DUMMY_CONTENT_URL, |
| 168 | + null, |
| 169 | + InputOption::VALUE_OPTIONAL, |
| 170 | + sprintf('Used as dummy URL. Defaults to \'%s\'', self::DEFAULT_DUMMY_CONTENT_URL), |
| 171 | + self::DEFAULT_DUMMY_CONTENT_URL |
| 172 | + ), |
| 173 | + new InputOption( |
| 174 | + self::OPTION_DUMMY_CONTENT_PHONE, |
| 175 | + null, |
| 176 | + InputOption::VALUE_OPTIONAL, |
| 177 | + sprintf('Used as dummy phone number. Defaults to \'%s\'', self::DEFAULT_DUMMY_CONTENT_PHONE), |
| 178 | + self::DEFAULT_DUMMY_CONTENT_PHONE |
| 179 | + ), |
| 180 | + new InputOption( |
| 181 | + self::OPTION_DUMP_OUTPUT, |
| 182 | + null, |
| 183 | + InputOption::VALUE_OPTIONAL, |
| 184 | + sprintf('Path where the database dump is placed. Defaults to \'%s\'', self::DEFAULT_DUMP_OUTPUT), |
| 185 | + self::DEFAULT_DUMP_OUTPUT |
| 186 | + ) |
| 187 | + ]); |
| 188 | + |
| 189 | + parent::configure(); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * {@inheritdoc} |
| 194 | + */ |
| 195 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 196 | + { |
| 197 | + // Set area code |
| 198 | + $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML); |
| 199 | + |
| 200 | + /* |
| 201 | + * Configuration |
| 202 | + */ |
| 203 | + $configuration = $this->loadConfiguration( |
| 204 | + $this->configurationFactory->create(), |
| 205 | + $input |
| 206 | + ); |
| 207 | + |
| 208 | + /* |
| 209 | + * Processing |
| 210 | + */ |
| 211 | + $output->writeln('Start content export, this may take a moment...'); |
| 212 | + $this->backupHandler->beginTransaction(); |
| 213 | + |
| 214 | + try { |
| 215 | + $this->eventManager->dispatch('aid_content_export_event', [ |
| 216 | + 'configuration' => $configuration |
| 217 | + ]); |
| 218 | + |
| 219 | + $this->backupHandler->run( |
| 220 | + $input->getOption(self::OPTION_DUMP_OUTPUT) |
| 221 | + ); |
| 222 | + } catch (\Exception $e) { |
| 223 | + $this->backupHandler->endTransaction(); |
| 224 | + $output->writeln('Failed to export fuzzyfied content. Check your logs for more information.'); |
| 225 | + return self::ERROR_EXPORT_FAILED; |
| 226 | + } |
| 227 | + |
| 228 | + $this->backupHandler->endTransaction(); |
| 229 | + $output->writeln('Finished content export'); |
| 230 | + |
| 231 | + return self::SUCCESS; |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * @param Configuration $configuration |
| 236 | + * @param InputInterface $input |
| 237 | + * @return Configuration |
| 238 | + */ |
| 239 | + protected function loadConfiguration(Configuration $configuration, InputInterface $input) |
| 240 | + { |
| 241 | + // --- Flags |
| 242 | + $configuration->setUseOnlyEmpty($input->getOption(self::FLAG_ONLY_EMPTY)); |
| 243 | + $configuration->setApplyToCategories($input->getOption(self::FLAG_CATEGORIES)); |
| 244 | + $configuration->setApplyToCmsBlocks($input->getOption(self::FLAG_CMS_BLOCKS)); |
| 245 | + $configuration->setApplyToCmsPages($input->getOption(self::FLAG_CMS_PAGES)); |
| 246 | + $configuration->setApplyToCustomers($input->getOption(self::FLAG_CUSTOMERS)); |
| 247 | + $configuration->setApplyToProducts($input->getOption(self::FLAG_PRODUCTS)); |
| 248 | + $configuration->setApplyToUsers($input->getOption(self::FLAG_USERS)); |
| 249 | + |
| 250 | + // --- Options |
| 251 | + $configuration->setDummyContentText($input->getOption(self::OPTION_DUMMY_CONTENT_TEXT)); |
| 252 | + $configuration->setDummyContentEmail($input->getOption(self::OPTION_DUMMY_CONTENT_EMAIL)); |
| 253 | + $configuration->setDummyContentUrl($input->getOption(self::OPTION_DUMMY_CONTENT_URL)); |
| 254 | + $configuration->setDummyPhoneNumber($input->getOption(self::OPTION_DUMMY_CONTENT_PHONE)); |
| 255 | + |
| 256 | + return $configuration; |
| 257 | + } |
| 258 | +} |
0 commit comments