diff --git a/alma/controllers/hook/GetContentHookController.php b/alma/controllers/hook/GetContentHookController.php index 783b4a3d..084cfe15 100644 --- a/alma/controllers/hook/GetContentHookController.php +++ b/alma/controllers/hook/GetContentHookController.php @@ -38,6 +38,7 @@ use Alma\PrestaShop\Builders\Helpers\SettingsHelperBuilder; use Alma\PrestaShop\Builders\Helpers\ShareOfCheckoutHelperBuilder; use Alma\PrestaShop\Builders\Models\MediaHelperBuilder; +use Alma\PrestaShop\Exceptions\AlmaFormConfigurationException; use Alma\PrestaShop\Exceptions\MissingParameterException; use Alma\PrestaShop\Forms\ApiAdminFormBuilder; use Alma\PrestaShop\Forms\CartEligibilityAdminFormBuilder; @@ -62,6 +63,9 @@ use Alma\PrestaShop\Helpers\SettingsHelper; use Alma\PrestaShop\Hooks\AdminHookController; use Alma\PrestaShop\Logger; +use Alma\PrestaShop\Model\AlmaApiKeyModel; +use Alma\Prestashop\Proxy\ConfigurationProxy; +use Alma\Prestashop\Services\AlmaConfigurationService; final class GetContentHookController extends AdminHookController { @@ -162,6 +166,18 @@ final class GetContentHookController extends AdminHookController 'ALMA_STATE_TRIGGER' => 'none', 'ALMA_DESCRIPTION_TRIGGER' => 'none', ]; + /** + * @var \Alma\Prestashop\Services\AlmaConfigurationService + */ + protected $almaConfigurationService; + /** + * @var \Alma\Prestashop\Proxy\ConfigurationProxy + */ + protected $configurationProxy; + /** + * @var \Alma\PrestaShop\Model\AlmaApiKeyModel + */ + protected $almaApiKeyModel; /** * GetContentHook Controller construct. @@ -190,6 +206,10 @@ public function __construct($module) $contextHelperBuilder = new ContextHelperBuilder(); $this->contextHelper = $contextHelperBuilder->getInstance(); + //$this->almaConfigurationService = new AlmaConfigurationService(); + $this->configurationProxy = new ConfigurationProxy(); + $this->almaApiKeyModel = new AlmaApiKeyModel(); + $this->hasKey = false; parent::__construct($module); @@ -849,6 +869,18 @@ public function needsAPIKey() */ public function run($params) { + // Refacto + $liveKey = SettingsHelper::getLiveKey(); + $testKey = SettingsHelper::getTestKey(); + $key = trim(SettingsHelper::getActiveAPIKey()); + + if (\Tools::isSubmit('alma_config_form')) { + $this->almaConfigurationService->saveConfiguration(); + } + + $this->assignSmartyKeys(); + + // Old code to be removed $this->context->smarty->assign([ 'hasPSAccounts' => $params['hasPSAccounts'], 'updated' => true, @@ -1009,4 +1041,54 @@ protected function saveConfigValues() $this->updateSettingsValue($key, $value); } } + + /** + * @return void + */ + private function assignSmartyKeys() + { + $token = \Tools::getAdminTokenLite('AdminModules'); + $href = $this->context->link->getAdminLink('AdminParentModulesSf', $token); + + $this->context->smarty->assign([ + 'hasPSAccounts' => false, // Dynamic content + 'updated' => false, // Dynamic content + 'suggestPSAccounts' => false, // Dynamic content + 'validation_error_classes' => 'alert', //Add error class key + 'tip_classes' => 'conf', //Add tip class key + 'success_classes' => 'conf', //Add success class key + 'breadcrumbs2' => [ + 'container' => [ + 'name' => $this->module->l('Modules', 'GetContentHookController'), + 'href' => $href, + ], + 'tab' => [ + 'name' => $this->module->l('Module Manager', 'GetContentHookController'), + 'href' => $href, + ], + ], + 'quick_access_current_link_name' => $this->module->l('Module Manager - List', 'GetContentHookController'), + 'quick_access_current_link_icon' => 'icon-AdminParentModulesSf', + 'token' => $token, + 'host_mode' => 0, + 'validation_error' => '', //Add error key + 'validation_message' => '', //Add error message + 'n' => 0, //Add installment value + 'deferred_days' => 0, //Add deferred days value + 'deferred_months' => 0, //Add deferred months value + 'min' => 0, //Add min purchase amount value + 'max' => 0, //Add max purchase amount value + 'hasKey' => false, //Return true if api key is set + 'tip' => 'fill_api_keys', + ]); + } + + private function saveConfiguration() + { + try { + $this->almaApiKeyModel->checkActiveApiKey(); + } catch (AlmaFormConfigurationException $e) { + Logger::instance()->error($e->getMessage()); + } + } } diff --git a/alma/exceptions/AlmaFormConfigurationException.php b/alma/exceptions/AlmaFormConfigurationException.php new file mode 100644 index 00000000..49836810 --- /dev/null +++ b/alma/exceptions/AlmaFormConfigurationException.php @@ -0,0 +1,33 @@ + + * @copyright 2018-2024 Alma SAS + * @license https://opensource.org/licenses/MIT The MIT License + */ + +namespace Alma\PrestaShop\Exceptions; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class AlmaFormConfigurationException extends AlmaException +{ +} diff --git a/alma/lib/Services/AlmaConfigurationService.php b/alma/lib/Services/AlmaConfigurationService.php new file mode 100644 index 00000000..83e0c0b5 --- /dev/null +++ b/alma/lib/Services/AlmaConfigurationService.php @@ -0,0 +1,82 @@ + + * @copyright 2018-2024 Alma SAS + * @license https://opensource.org/licenses/MIT The MIT License + */ + +namespace Alma\Prestashop\Services; + +use Alma\PrestaShop\Exceptions\AlmaApiKeyException; +use Alma\PrestaShop\Factories\ClientFactory; +use Alma\PrestaShop\Forms\ApiAdminFormBuilder; +use Alma\PrestaShop\Logger; +use Alma\PrestaShop\Model\AlmaApiKeyModel; +use Alma\PrestaShop\Proxy\ToolsProxy; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class AlmaConfigurationService +{ + /** + * @var \Alma\PrestaShop\Model\AlmaApiKeyModel + */ + private $almaApiKeyModel; + /** + * @var \Alma\PrestaShop\Proxy\ToolsProxy|mixed|null + */ + private $toolsProxy; + + public function __construct( + $almaApiKeyModel = null, + $toolsProxy = null, + $clientFactory = null + ) { + if (!$almaApiKeyModel) { + $almaApiKeyModel = new AlmaApiKeyModel(); + } + $this->almaApiKeyModel = $almaApiKeyModel; + + if (!$toolsProxy) { + $toolsProxy = new ToolsProxy(); + } + $this->toolsProxy = $toolsProxy; + + if (!$clientFactory) { + $clientFactory = new ClientFactory(); + } + } + + public function saveConfiguration() + { + try { + $currentMode = $this->toolsProxy->getValue(ApiAdminFormBuilder::ALMA_API_MODE); + $this->almaApiKeyModel->checkActiveApiKey($currentMode); + } catch (AlmaApiKeyException $e) { + Logger::instance()->error($e->getMessage()); + } + } + + public function getConfiguration() + { + } +} diff --git a/alma/tests/Unit/Services/AlmaConfigurationServiceTest.php b/alma/tests/Unit/Services/AlmaConfigurationServiceTest.php new file mode 100644 index 00000000..fa15fceb --- /dev/null +++ b/alma/tests/Unit/Services/AlmaConfigurationServiceTest.php @@ -0,0 +1,54 @@ + + * @copyright 2018-2024 Alma SAS + * @license https://opensource.org/licenses/MIT The MIT License + */ + +namespace Alma\PrestaShop\Tests\Unit\Services; + +use Alma\PrestaShop\Model\AlmaApiKeyModel; +use Alma\Prestashop\Services\AlmaConfigurationService; +use PHPUnit\Framework\TestCase; + +class AlmaConfigurationServiceTest extends TestCase +{ + /** + * @var AlmaConfigurationService + */ + protected $almaconfigurationService; + + public function setUp() + { + $this->almaApiKeyModel = $this->createMock(AlmaApiKeyModel::class); + $this->almaconfigurationService = new AlmaConfigurationService( + $this->almaApiKeyModel + ); + } + + public function tearDown() + { + $this->almaApiKeyModel = null; + } + + public function testSaveConfiguration() + { + } +}