|
| 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\Handler\Backup; |
| 12 | + |
| 13 | +use AllInData\ContentFuzzyfyr\Console\Command\ExportCommand; |
| 14 | +use AllInData\ContentFuzzyfyr\Handler\BackupHandler; |
| 15 | +use Magento\Framework\Backup\Db\BackupFactory; |
| 16 | +use Magento\Framework\EntityManager\EventManager; |
| 17 | +use AllInData\ContentFuzzyfyr\Model\Configuration; |
| 18 | +use AllInData\ContentFuzzyfyr\Model\ConfigurationFactory; |
| 19 | + |
| 20 | +/** |
| 21 | + * Class Db |
| 22 | + * @package AllInData\ContentFuzzyfyr\Handler\Backup |
| 23 | + */ |
| 24 | +class DatabaseHandler extends \Magento\Framework\Backup\Db |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var EventManager |
| 28 | + */ |
| 29 | + private $eventManager; |
| 30 | + /** |
| 31 | + * @var ConfigurationFactory |
| 32 | + */ |
| 33 | + private $configurationFactory; |
| 34 | + /** |
| 35 | + * @var BackupHandler |
| 36 | + */ |
| 37 | + private $backupHandler; |
| 38 | + |
| 39 | + /** |
| 40 | + * DatabaseHandler constructor. |
| 41 | + * @param BackupFactory $backupFactory |
| 42 | + * @param EventManager $eventManager |
| 43 | + * @param ConfigurationFactory $configurationFactory |
| 44 | + * @param BackupHandler $backupHandler |
| 45 | + */ |
| 46 | + public function __construct( |
| 47 | + BackupFactory $backupFactory, |
| 48 | + EventManager $eventManager, |
| 49 | + ConfigurationFactory $configurationFactory, |
| 50 | + BackupHandler $backupHandler |
| 51 | + ) { |
| 52 | + parent::__construct($backupFactory); |
| 53 | + |
| 54 | + $this->eventManager = $eventManager; |
| 55 | + $this->configurationFactory = $configurationFactory; |
| 56 | + $this->backupHandler = $backupHandler; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * {@inheritdoc} |
| 61 | + */ |
| 62 | + public function create() |
| 63 | + { |
| 64 | + /* |
| 65 | + * Processing |
| 66 | + */ |
| 67 | + $this->backupHandler->beginTransaction(); |
| 68 | + |
| 69 | + try { |
| 70 | + $this->eventManager->dispatch(ExportCommand::EVENT_NAME, [ |
| 71 | + 'configuration' => $this->loadConfiguration() |
| 72 | + ]); |
| 73 | + |
| 74 | + if (!parent::create()) { |
| 75 | + throw new \RuntimeException('Failed to create database backup'); |
| 76 | + } |
| 77 | + } catch (\Exception $e) { |
| 78 | + $this->backupHandler->endTransaction(); |
| 79 | + return ExportCommand::ERROR_EXPORT_FAILED; |
| 80 | + } |
| 81 | + |
| 82 | + $this->backupHandler->endTransaction(); |
| 83 | + |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Add path that should be ignoring when creating or rolling back backup |
| 89 | + * |
| 90 | + * @param string|array $paths |
| 91 | + * @return $this |
| 92 | + */ |
| 93 | + public function addIgnorePaths($paths) |
| 94 | + { |
| 95 | + return $this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @return Configuration |
| 100 | + */ |
| 101 | + protected function loadConfiguration() |
| 102 | + { |
| 103 | + $configuration = $this->configurationFactory->create(); |
| 104 | + // --- Flags |
| 105 | + $configuration->setApplyToUsers(true); |
| 106 | + $configuration->setApplyToCustomers(true); |
| 107 | + // --- Options |
| 108 | + $configuration->setDummyContentText(ExportCommand::DEFAULT_DUMMY_CONTENT_TEXT); |
| 109 | + $configuration->setDummyContentEmail(ExportCommand::DEFAULT_DUMMY_CONTENT_EMAIL); |
| 110 | + $configuration->setDummyContentUrl(ExportCommand::DEFAULT_DUMMY_CONTENT_URL); |
| 111 | + $configuration->setDummyPhoneNumber(ExportCommand::DEFAULT_DUMMY_CONTENT_PHONE); |
| 112 | + |
| 113 | + return $configuration; |
| 114 | + } |
| 115 | +} |
0 commit comments