- Created a new phpstan.neon file in the Symfony4 tests directory. - Configured paths and excluded Symfony3 directory. - Added bootstrap files for autoloading. - Set dynamic constant names and adjusted reporting settings. - Established PHPStan level to 6 for stricter analysis.
204 lines
7.0 KiB
PHP
204 lines
7.0 KiB
PHP
<?php
|
|
|
|
|
|
use Empik\Marketplace\Adapter\ConfigurationAdapter;
|
|
use Empik\Marketplace\Adapter\LinkAdapter;
|
|
use Empik\Marketplace\Factory\EmpikClientFactory;
|
|
use Empik\Marketplace\Validator\Validator;
|
|
|
|
class AdminEmpikConnectionController extends ModuleAdminController
|
|
{
|
|
const CONN_SUCCESS = 1;
|
|
const CONN_ERROR_API = 2;
|
|
|
|
public $_conf = [];
|
|
public $_error = [];
|
|
|
|
/** @var EmpikMarketplace */
|
|
public $module;
|
|
|
|
/** @var EmpikClientFactory */
|
|
protected $empikClientFactory;
|
|
|
|
/** @var LinkAdapter */
|
|
protected $link;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
$this->bootstrap = true;
|
|
|
|
$this->_conf[self::CONN_SUCCESS] = $this->l('Connection to API works fine.');
|
|
$this->_error[self::CONN_ERROR_API] = $this->l('Unable to connect, check connection details and try again.');
|
|
|
|
$this->empikClientFactory = $this->module->getService('empik.marketplace.factory.empikClientFactory');
|
|
$this->link = $this->module->getService('empik.marketplace.adapter.link');
|
|
}
|
|
|
|
public function initProcess()
|
|
{
|
|
$this->display = 'edit';
|
|
|
|
parent::initProcess();
|
|
}
|
|
|
|
public function postProcess()
|
|
{
|
|
if (Tools::getIsset('submit'.$this->controller_name)) {
|
|
$this->handleForm();
|
|
}
|
|
|
|
parent::postProcess();
|
|
}
|
|
|
|
public function handleForm()
|
|
{
|
|
$environment = Tools::getValue('environment');
|
|
$apiKey = Tools::getValue('api_key');
|
|
|
|
if (!Validator::isValidEnvironment($environment)) {
|
|
$this->errors[] = $this->l('Invalid environment field');
|
|
}
|
|
|
|
if (!Validator::isValidApiKey($apiKey)) {
|
|
$this->errors[] = $this->l('Invalid API key field');
|
|
}
|
|
|
|
if (empty($this->errors)) {
|
|
|
|
$result = true;
|
|
|
|
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_ENVIRONMENT, $environment);
|
|
$result &= Configuration::updateValue(ConfigurationAdapter::CONF_API_KEY, $apiKey);
|
|
|
|
if ($result) {
|
|
Tools::redirectAdmin(
|
|
$this->link->getAdminLink($this->controller_name, ['conf' => 4])
|
|
);
|
|
}
|
|
}
|
|
|
|
$this->errors[] = $this->l('Error saving form.');
|
|
}
|
|
|
|
public function processTestConnection()
|
|
{
|
|
$status = $this->getConnectionStatus();
|
|
if ($status === self::CONN_SUCCESS) {
|
|
$this->module->setAuthStatus();
|
|
Tools::redirectAdmin(
|
|
$this->link->getAdminLink($this->controller_name, ['conf' => self::CONN_SUCCESS])
|
|
);
|
|
}
|
|
|
|
$this->errors[] = $this->_error[self::CONN_ERROR_API];
|
|
}
|
|
|
|
/**
|
|
* @return int
|
|
*/
|
|
public function getConnectionStatus()
|
|
{
|
|
try {
|
|
$client = $this->empikClientFactory->createClient();
|
|
$client->getAccount();
|
|
} catch (Exception $e) {
|
|
return self::CONN_ERROR_API;
|
|
}
|
|
|
|
return self::CONN_SUCCESS;
|
|
}
|
|
|
|
public function renderForm()
|
|
{
|
|
$controller = $this->controller_name;
|
|
|
|
$this->context->smarty->assign([
|
|
'url_test_connection' => $this->link->getAdminLink($controller, ['action' => 'testConnection'])
|
|
]);
|
|
|
|
$fields = [
|
|
'form' => [
|
|
'legend' => [
|
|
'title' => $this->l('Configuration'),
|
|
'icon' => 'icon-cogs',
|
|
],
|
|
'input' => [
|
|
[
|
|
'type' => 'select',
|
|
'name' => 'environment',
|
|
'label' => $this->l('Environment'),
|
|
'hint' => $this->l('Select environment'),
|
|
'desc' => $this->l('Use "testing" for development, switch to "production" when ready for real orders.'),
|
|
'options' => [
|
|
'query' => [
|
|
[
|
|
'url' => ConfigurationAdapter::ENV_TEST,
|
|
'name' => $this->l('testing'),
|
|
],
|
|
[
|
|
'url' => ConfigurationAdapter::ENV_PROD,
|
|
'name' => $this->l('production'),
|
|
],
|
|
],
|
|
'id' => 'url',
|
|
'name' => 'name',
|
|
],
|
|
'class' => 'fixed-width-xxl',
|
|
],
|
|
[
|
|
'type' => 'text',
|
|
'label' => $this->l('API key'),
|
|
'hint' => $this->l('API key'),
|
|
'desc' => $this->l('Generate API key in EmpikPlace panel (Marketplace > API settings). After entering key and testing connection, CRON URLs will be available in Help tab.'),
|
|
'name' => 'api_key',
|
|
],
|
|
[
|
|
'label' => '',
|
|
'type' => 'html',
|
|
'name' => 'html_data',
|
|
'html_content' => $this->context->smarty->fetch(
|
|
$this->module->getLocalPath().'/views/templates/admin/_partials/test_connection_button.tpl'
|
|
),
|
|
],
|
|
[
|
|
'label' => '',
|
|
'type' => 'html',
|
|
'name' => 'custom_html',
|
|
'html_content' => $this->context->smarty->fetch(
|
|
$this->module->getLocalPath().'/views/templates/admin/_partials/test_connection_footer.tpl'
|
|
),
|
|
],
|
|
],
|
|
],
|
|
];
|
|
|
|
$helper = new HelperForm();
|
|
|
|
$helper->show_toolbar = false;
|
|
$helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT');
|
|
$helper->module = $this->module;
|
|
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
|
|
$helper->identifier = $this->identifier;
|
|
$helper->submit_action = 'submit'.$controller;
|
|
$helper->currentIndex = $this->link->getAdminLink($controller);
|
|
$helper->token = Tools::getAdminTokenLite($controller);
|
|
$helper->tpl_vars = [
|
|
'languages' => $this->context->controller->getLanguages(),
|
|
'id_language' => $this->context->language->id,
|
|
'fields_value' => [
|
|
'environment' => Tools::getValue('environment', Configuration::get(ConfigurationAdapter::CONF_ENVIRONMENT)),
|
|
'api_key' => Tools::getValue('api_key', Configuration::get(ConfigurationAdapter::CONF_API_KEY))
|
|
]
|
|
];
|
|
|
|
return $helper->generateForm([$fields]);
|
|
}
|
|
|
|
protected function l($string, $class = null, $addslashes = false, $htmlentities = true)
|
|
{
|
|
return Translate::getModuleTranslation($this->module, $string, get_class($this), null, $addslashes);
|
|
}
|
|
}
|