Skip to content

Commit

Permalink
MAGECLOUD-2447: Fix code style to fit Magento's (#1)
Browse files Browse the repository at this point in the history
* MAGECLOUD-2447: Fix code style to fit Magento's

* MAGECLOUD-2447: Fix code style to fit Magento's

- Added logging
- Added plugin sources

* MAGECLOUD-2447: Fix code style to fit Magento's

- Review comments fixes
  • Loading branch information
shiftedreality authored and bbatsche committed Jul 23, 2018
1 parent 89e61ba commit 785c982
Show file tree
Hide file tree
Showing 26 changed files with 1,002 additions and 350 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1 +1,3 @@
/.idea/
composer.lock
/vendor/
16 changes: 12 additions & 4 deletions Block/Adminhtml/Form/Field/Headers.php
@@ -1,7 +1,12 @@
<?php
namespace Thai\S3\Block\Adminhtml\Form\Field;

class Headers extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;

/**
* @inheritdoc
*/
class Headers extends AbstractFieldArray
{
/**
* @var \Magento\Framework\Data\Form\Element\Factory
Expand All @@ -17,18 +22,21 @@ public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Data\Form\Element\Factory $elementFactory,
array $data = []
)
{
) {
$this->_elementFactory = $elementFactory;
parent::__construct($context, $data);
}

/**
* @inheritdoc
*/
protected function _construct()
{
$this->addColumn('header', ['label' => __('Header')]);
$this->addColumn('value', ['label' => __('Value')]);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add Custom Header');

parent::_construct();
}
}
}
63 changes: 48 additions & 15 deletions Console/Command/ConfigListCommand.php
Expand Up @@ -2,44 +2,77 @@
namespace Thai\S3\Console\Command;

use Magento\Config\Model\Config\Factory;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ConfigListCommand extends \Symfony\Component\Console\Command\Command
/**
* @inheritdoc
*/
class ConfigListCommand extends Command
{
private $configFactory;

/**
* @var State
*/
private $state;

/**
* @param State $state
* @param Factory $configFactory
*/
public function __construct(
\Magento\Framework\App\State $state,
State $state,
Factory $configFactory
) {
$this->state = $state;
$this->configFactory = $configFactory;

parent::__construct();
}

/**
* @inheritdoc
*/
protected function configure()
{
$this->setName('s3:config:list');
$this->setDescription('Lists whatever credentials for S3 you have provided for Magento.');
}

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$this->state->setAreaCode('adminhtml');
} catch (\Magento\Framework\Exception\LocalizedException $e) {
// intentionally left empty
}

$config = $this->configFactory->create();
$output->writeln('Here are your AWS credentials.');
$output->writeln('');
$output->writeln(sprintf('Access Key ID: %s', $config->getConfigDataValue('thai_s3/general/access_key')));
$output->writeln(sprintf('Secret Access Key: %s', $config->getConfigDataValue('thai_s3/general/secret_key')));
$output->writeln(sprintf('Bucket: %s', $config->getConfigDataValue('thai_s3/general/bucket')));
$output->writeln(sprintf('Region: %s', $config->getConfigDataValue('thai_s3/general/region')));
return $this->state->emulateAreaCode(Area::AREA_ADMINHTML, function () use ($output) {
$config = $this->configFactory->create();

$output->writeln('Here are your AWS credentials.');
$output->writeln('');
$output->writeln(sprintf(
'Access Key ID: %s',
$config->getConfigDataValue('thai_s3/general/access_key')
));
$output->writeln(sprintf(
'Secret Access Key: %s',
$config->getConfigDataValue('thai_s3/general/secret_key')
));
$output->writeln(sprintf(
'Bucket: %s',
$config->getConfigDataValue('thai_s3/general/bucket')
));
$output->writeln(sprintf(
'Region: %s',
$config->getConfigDataValue('thai_s3/general/region')
));

return 0;
});
}
}
102 changes: 71 additions & 31 deletions Console/Command/ConfigSetCommand.php
Expand Up @@ -2,77 +2,113 @@
namespace Thai\S3\Console\Command;

use Magento\Config\Model\Config\Factory;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Thai\S3\Helper\S3 as S3Helper;

class ConfigSetCommand extends \Symfony\Component\Console\Command\Command
/**
* @inheritdoc
*/
class ConfigSetCommand extends Command
{
/**
* @var Factory
*/
private $configFactory;

/**
* @var State
*/
private $state;

/**
* @var S3Helper
*/
private $helper;

/**
* @param State $state
* @param Factory $configFactory
* @param S3Helper $helper
*/
public function __construct(
\Magento\Framework\App\State $state,
State $state,
Factory $configFactory,
\Thai\S3\Helper\S3 $helper
S3Helper $helper
) {
$this->state = $state;
$this->helper = $helper;
$this->configFactory = $configFactory;

parent::__construct();
}

/**
* @inheritdoc
*/
protected function configure()
{
$this->setName('s3:config:set');
$this->setDescription('Allows you to set your S3 configuration via the CLI.');
$this->setDefinition($this->getOptionsList());
}

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$this->state->setAreaCode('adminhtml');
} catch (\Magento\Framework\Exception\LocalizedException $e) {
// intentionally left empty
}
return $this->state->emulateAreaCode(Area::AREA_ADMINHTML, function () use ($input, $output) {
if (!$input->getOption('region') && !$input->getOption('bucket') && !$input->getOption('secret-key') && !$input->getOption('access-key-id')) {
$output->writeln($this->getSynopsis());

if (!$input->getOption('region') && !$input->getOption('bucket') && !$input->getOption('secret-key') && !$input->getOption('access-key-id')) {
$output->writeln($this->getSynopsis());
return;
}
return 1;
}

$errors = $this->validate($input);
if ($errors) {
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
return;
}
$errors = $this->validate($input);
if ($errors) {
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');

return 1;
}

$config = $this->configFactory->create();
$config = $this->configFactory->create();

foreach ($this->getOptions() as $option => $pathValue) {
if ( ! empty($input->getOption($option))) {
$config->setDataByPath('thai_s3/general/'.$pathValue, $input->getOption($option));
$config->save();
foreach ($this->getOptions() as $option => $pathValue) {
if (!empty($input->getOption($option))) {
$config->setDataByPath('thai_s3/general/' . $pathValue, $input->getOption($option));
$config->save();
}
}
}

$output->writeln('<info>You have successfully updated your S3 credentials.</info>');
$output->writeln('<info>You have successfully updated your S3 credentials.</info>');

return 0;
});
}

/**
* @return array
*/
public function getOptions()
{
return [
'access-key-id' => 'access_key',
'secret-key' => 'secret_key',
'bucket' => 'bucket',
'region' => 'region',
'secret-key' => 'secret_key',
'bucket' => 'bucket',
'region' => 'region',
];
}

/**
* @return array
*/
public function getOptionsList()
{
return [
Expand All @@ -83,14 +119,18 @@ public function getOptionsList()
];
}

/**
* @param InputInterface $input
* @return array
*/
public function validate(InputInterface $input)
{
$errors = [];
if ($input->getOption('region')) {
if (!$this->helper->isValidRegion($input->getOption('region'))) {
$errors[] = sprintf('The region "%s" is invalid.', $input->getOption('region'));
}

if ($input->getOption('region') && !$this->helper->isValidRegion($input->getOption('region'))) {
$errors[] = sprintf('The region "%s" is invalid.', $input->getOption('region'));
}

return $errors;
}
}
52 changes: 38 additions & 14 deletions Console/Command/CustomEndpointDisableCommand.php
Expand Up @@ -2,42 +2,66 @@
namespace Thai\S3\Console\Command;

use Magento\Config\Model\Config\Factory;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CustomEndpointDisableCommand extends \Symfony\Component\Console\Command\Command
/**
* @inheritdoc
*/
class CustomEndpointDisableCommand extends Command
{
/**
* @var Factory
*/
private $configFactory;

/**
* @var State
*/
private $state;

/**
* @param State $state
* @param Factory $configFactory
*/
public function __construct(
\Magento\Framework\App\State $state,
State $state,
Factory $configFactory
) {
$this->state = $state;
$this->configFactory = $configFactory;

parent::__construct();
}

/**
* @inheritdoc
*/
protected function configure()
{
$this->setName('s3:custom-endpoint:disable');
$this->setDescription('Revert to using Amazon S3 as the default endpoint.');
}

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$this->state->setAreaCode('adminhtml');
} catch (\Magento\Framework\Exception\LocalizedException $e) {
// intentionally left empty
}

$output->writeln('Updating configuration to use Amazon S3 as the default endpoint.');

$config = $this->configFactory->create();
$config->setDataByPath('thai_s3/custom_endpoint/enabled', 0);
$config->save();
$output->writeln(sprintf('<info>Magento now uses Amazon S3 as the default endpoint.</info>'));
return $this->state->emulateAreaCode(Area::AREA_ADMINHTML, function () use ($output) {
$output->writeln('Updating configuration to use Amazon S3 as the default endpoint.');

$config = $this->configFactory->create();
$config->setDataByPath('thai_s3/custom_endpoint/enabled', 0);
$config->save();
$output->writeln(sprintf('<info>Magento now uses Amazon S3 as the default endpoint.</info>'));

return 0;
});
}
}

0 comments on commit 785c982

Please sign in to comment.