Skip to content

Commit

Permalink
Add command to configure sequra from command line
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k3lm committed Jul 9, 2024
1 parent e491948 commit 1f7bf22
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
# Exclude specific directories
.github export-ignore
docs export-ignore
docker export-ignore
docker export-ignore
bin export-ignore
208 changes: 208 additions & 0 deletions Console/Configure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php
/**
* Copyright © 2017 SeQura Engineering. All rights reserved.
*/

namespace Sequra\Core\Console;

use Sequra\Core\Model\ConfigFactory;
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 SeQura\Core\BusinessLogic\Domain\Connection\Models\AuthorizationCredentials;
use SeQura\Core\BusinessLogic\Domain\Connection\Models\ConnectionData;
use SeQura\Core\BusinessLogic\Domain\Connection\Services\ConnectionService;
use SeQura\Core\BusinessLogic\Domain\CountryConfiguration\Exceptions\EmptyCountryConfigurationParameterException;
use SeQura\Core\BusinessLogic\Domain\CountryConfiguration\Exceptions\InvalidCountryCodeForConfigurationException;
use SeQura\Core\BusinessLogic\Domain\CountryConfiguration\Models\CountryConfiguration;
use SeQura\Core\BusinessLogic\Domain\CountryConfiguration\Models\SellingCountry;
use SeQura\Core\BusinessLogic\Domain\CountryConfiguration\Services\CountryConfigurationService;
use SeQura\Core\BusinessLogic\SeQuraAPI\BaseProxy;
use SeQura\Core\Infrastructure\ServiceRegister;

/**
* Console command to trigger DR report
*/
class Configure extends Command
{
/**
* Command name
*/
const NAME = 'sequra:configure';
/**
* Names of input arguments or options
*/
const INPUT_KEY_MERCHANT_REF = 'merchant_ref';
/**
* Values of input arguments or options
*/
const INPUT_KEY_USERNAME = 'username';
/**
* Values of input arguments or options
*/
const INPUT_KEY_ASSETS_KEY = 'assets_key';
/**
* Values of input arguments or options
*/
const INPUT_KEY_ENDPOINT = 'endpoint';
/**
* Values of input arguments or options
*/
const INPUT_KEY_PASSWORD = 'password';

/**
* Constructor
*
* @param ConfigFactory $configFactory configFactory
* @param \Sequra\Core\Model\ReporterFactory $reporterFactory reporteFactory
* @param \Magento\Framework\App\State $state
*/
public function __construct(
ConfigFactory $configFactory,
\Sequra\Core\Model\ReporterFactory $reporterFactory,
\Magento\Framework\App\State $state
) {
$this->config = $configFactory->create();
$this->reporter = $reporterFactory->create();
$this->state = $state;
parent::__construct();
}

/**
* Initialize triggerreport command
*
* @return void
*/
protected function configure()
{
$inputOptions = [
new InputOption(
self::INPUT_KEY_MERCHANT_REF,
null,
InputOption::VALUE_NONE,
'Merchant reference'
),
new InputOption(
self::INPUT_KEY_USERNAME,
null,
InputOption::VALUE_NONE,
'Username'
),
new InputOption(
self::INPUT_KEY_ASSETS_KEY,
null,
InputOption::VALUE_NONE,
'Assets key'
),
new InputOption(
self::INPUT_KEY_ENDPOINT,
null,
InputOption::VALUE_NONE,
'Endpoint'
),
new InputOption(
self::INPUT_KEY_PASSWORD,
null,
InputOption::VALUE_NONE,
'Password'
),
];
$this->setName(self::NAME)
->setDescription('Quickly set sequra configuration')
->setDefinition($inputOptions);
parent::configure();
}

/**
* Execute command.
*
* @param InputInterface $input InputInterface
* @param OutputInterface $output OutputInterface
*
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
$this->saveConnectionData(
$input->getOption(self::INPUT_KEY_ENDPOINT),
$input->getOption(self::INPUT_KEY_USERNAME),
$input->getOption(self::INPUT_KEY_PASSWORD)
);
$this->saveCountriesConfig(
'ES',
$input->getOption(self::INPUT_KEY_MERCHANT_REF)
);
$generalSettings = new GeneralSettings(
false,
true,
[],
[],
[]
);
$this->getGeneralSettingsService()->saveGeneralSettings($generalSettings);
$this->saveWidgetSettings($input->getOption(self::INPUT_KEY_ASSETS_KEY));
}

/**
* @param string $endpoint
* @param string $username
* @param string $password
*
* @return void
*
* @throws InvalidEnvironmentException
*/
private function saveConnectionData(string $endpoint, string $username, string $password)
{
$connectionData = new ConnectionData(
$endpoint === 'https://sandbox.sequrapi.com/orders' ? BaseProxy::TEST_MODE : BaseProxy::LIVE_MODE,
'',
new AuthorizationCredentials($username, $password)
);
$this->getConnectionService()->saveConnectionData($connectionData);
}
/**
* @return ConnectionService
*/
private function getConnectionService(): ConnectionService
{
if ($this->connectionService === null) {
$this->connectionService = ServiceRegister::getService(ConnectionService::class);
}

return $this->connectionService;
}

/**
* @param SellingCountry[] $sellingCountries
* @param string $merchantId
*
* @return void
*
* @throws EmptyCountryConfigurationParameterException
* @throws InvalidCountryCodeForConfigurationException
*/
private function saveCountriesConfig(array $sellingCountries, string $merchantId)
{
$countryConfiguration = [];
foreach ($sellingCountries as $country) {
$countryConfiguration[] = new CountryConfiguration($country->getCode(), $merchantId);
}

$this->getCountryConfigService()->saveCountryConfiguration($countryConfiguration);
}
/**
* @return CountryConfigurationService
*/
private function getCountryConfigService(): CountryConfigurationService
{
if ($this->countryConfigService === null) {
$this->countryConfigService = ServiceRegister::getService(CountryConfigurationService::class);
}

return $this->countryConfigService;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"pagos",
"magento2"
],
"version": "2.5.0.4",
"version": "2.5.1",
"license": "MIT",
"authors": [
{
Expand Down
Loading

0 comments on commit 1f7bf22

Please sign in to comment.