|
| 1 | +<?php |
| 2 | +namespace Thai\S3\Console\Command; |
| 3 | + |
| 4 | +use Magento\Config\Model\Config\Factory; |
| 5 | +use Symfony\Component\Console\Input\InputInterface; |
| 6 | +use Symfony\Component\Console\Output\OutputInterface; |
| 7 | + |
| 8 | +class StorageDisableCommand extends \Symfony\Component\Console\Command\Command |
| 9 | +{ |
| 10 | + private $configFactory; |
| 11 | + |
| 12 | + private $state; |
| 13 | + |
| 14 | + private $helper; |
| 15 | + |
| 16 | + private $client; |
| 17 | + |
| 18 | + private $coreFileStorage; |
| 19 | + |
| 20 | + private $storageHelper; |
| 21 | + |
| 22 | + public function __construct( |
| 23 | + \Magento\Framework\App\State $state, |
| 24 | + Factory $configFactory, |
| 25 | + \Magento\MediaStorage\Helper\File\Storage\Database $storageHelper, |
| 26 | + \Magento\MediaStorage\Helper\File\Storage $coreFileStorage, |
| 27 | + \Thai\S3\Helper\Data $helper |
| 28 | + ) { |
| 29 | + $this->state = $state; |
| 30 | + $this->configFactory = $configFactory; |
| 31 | + $this->coreFileStorage = $coreFileStorage; |
| 32 | + $this->helper = $helper; |
| 33 | + $this->storageHelper = $storageHelper; |
| 34 | + parent::__construct(); |
| 35 | + } |
| 36 | + |
| 37 | + protected function configure() |
| 38 | + { |
| 39 | + $this->setName('s3:storage:disable'); |
| 40 | + $this->setDescription('Revert to using the local filesystem as your Magento 2 file storage backend.'); |
| 41 | + } |
| 42 | + |
| 43 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 44 | + { |
| 45 | + $errors = $this->validate($input); |
| 46 | + if ($errors) { |
| 47 | + $output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>'); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + try { |
| 52 | + $this->client = new \Aws\S3\S3Client([ |
| 53 | + 'version' => 'latest', |
| 54 | + 'region' => $this->helper->getRegion(), |
| 55 | + 'credentials' => [ |
| 56 | + 'key' => $this->helper->getAccessKey(), |
| 57 | + 'secret' => $this->helper->getSecretKey() |
| 58 | + ] |
| 59 | + ]); |
| 60 | + } catch (\Exception $e) { |
| 61 | + $output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + if (!$this->client->doesBucketExist($this->helper->getBucket())) { |
| 66 | + $output->writeln('<error>The AWS credentials you provided did not work. Please review your details and try again. You can do so using our config script.</error>'); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if ($this->coreFileStorage->getCurrentStorageCode() != \Thai\S3\Model\MediaStorage\File\Storage::STORAGE_MEDIA_S3) { |
| 71 | + $output->writeln('<error>You are not using S3 as your media file storage backend!</error>'); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + $output->writeln('Updating configuration to use the local filesystem.'); |
| 76 | + |
| 77 | + $this->state->setAreaCode('adminhtml'); |
| 78 | + $config = $this->configFactory->create(); |
| 79 | + $config->setDataByPath('system/media_storage_configuration/media_storage', \Magento\MediaStorage\Model\File\Storage::STORAGE_MEDIA_FILE_SYSTEM); |
| 80 | + $config->save(); |
| 81 | + $output->writeln(sprintf('<info>Magento now uses the local filesystem for its file backend storage.</info>')); |
| 82 | + } |
| 83 | + |
| 84 | + public function validate(InputInterface $input) |
| 85 | + { |
| 86 | + $errors = []; |
| 87 | + |
| 88 | + if (is_null($this->helper->getAccessKey())) { |
| 89 | + $errors[] = 'You have not provided an AWS access key ID. You can do so using our config script.'; |
| 90 | + } |
| 91 | + if (is_null($this->helper->getSecretKey())) { |
| 92 | + $errors[] = 'You have not provided an AWS secret access key. You can do so using our config script.'; |
| 93 | + } |
| 94 | + if (is_null($this->helper->getBucket())) { |
| 95 | + $errors[] = 'You have not provided an S3 bucket. You can do so using our config script.'; |
| 96 | + } |
| 97 | + if (is_null($this->helper->getRegion())) { |
| 98 | + $errors[] = 'You have not provided an S3 region. You can do so using our config script.'; |
| 99 | + } |
| 100 | + |
| 101 | + return $errors; |
| 102 | + } |
| 103 | +} |
0 commit comments