Skip to content

Commit

Permalink
feat: WIP - Handle configuration module to save and display the form
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Freoua-Alma committed Nov 22, 2024
1 parent 7228240 commit d646c61
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 0 deletions.
82 changes: 82 additions & 0 deletions alma/controllers/hook/GetContentHookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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());
}
}
}
33 changes: 33 additions & 0 deletions alma/exceptions/AlmaFormConfigurationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* 2018-2024 Alma SAS.
*
* THE MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @author Alma SAS <[email protected]>
* @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
{
}
82 changes: 82 additions & 0 deletions alma/lib/Services/AlmaConfigurationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* 2018-2024 Alma SAS.
*
* THE MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @author Alma SAS <[email protected]>
* @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()
{
}
}
54 changes: 54 additions & 0 deletions alma/tests/Unit/Services/AlmaConfigurationServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* 2018-2024 Alma SAS.
*
* THE MIT LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @author Alma SAS <[email protected]>
* @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()
{
}
}

0 comments on commit d646c61

Please sign in to comment.