Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement PHP client endpoints into Prestashop #587

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion alma/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"require": {
"php": "^5.6 || ~7.0 || ~7.1 || ~7.2 || ~7.3 || ~7.4 || ~8.0 || ~8.1",
"alma/alma-php-client": "^2.2.0",
"alma/alma-php-client": "^2.3.1",
"ext-json": "*",
"ext-openssl": "*",
"prestashop/prestashop-accounts-installer": "^v1.0.4",
Expand Down
134 changes: 134 additions & 0 deletions alma/controllers/front/cmsdataexport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?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
*/

use Alma\API\Entities\MerchantData\CmsFeatures;
use Alma\API\Entities\MerchantData\CmsInfo;
use Alma\API\Lib\PayloadFormatter;
use Alma\PrestaShop\Builders\Helpers\SettingsHelperBuilder;
use Alma\PrestaShop\Exceptions\CmsDataException;
use Alma\PrestaShop\Exceptions\ValidateException;
use Alma\PrestaShop\Helpers\CmsDataHelper;
use Alma\PrestaShop\Helpers\SettingsHelper;
use Alma\PrestaShop\Helpers\ValidateHelper;
use Alma\PrestaShop\Traits\AjaxTrait;

if (!defined('_PS_VERSION_')) {
exit;
}

/**
* AlmaCmsDataExportModuleFrontController
*/
class AlmaCmsDataExportModuleFrontController extends ModuleFrontController
{
use AjaxTrait;

/**
* @var ValidateHelper
*/
protected $validateHelper;
/**
* @var \Alma\PrestaShop\Helpers\SettingsHelper
*/
protected $settingsHelper;
/**
* @var \Alma\API\Lib\PayloadFormatter
*/
protected $payloadFormatter;
/**
* @var CmsDataHelper
*/
protected $cmsDataHelper;

public function __construct()
{
parent::__construct();
$this->validateHelper = new ValidateHelper();
$this->cmsDataHelper = new CmsDataHelper();
$this->settingsHelper = (new SettingsHelperBuilder())->getInstance();
$this->payloadFormatter = new PayloadFormatter();
}

/**
* @throws \Alma\PrestaShop\Exceptions\CmsDataException
* @throws \PrestaShopException
*/
public function postProcess()
{
parent::postProcess();
try {
$this->validateHelper->checkSignature($this->settingsHelper->getIdMerchant(), SettingsHelper::getActiveAPIKey(), $_SERVER['HTTP_X_ALMA_SIGNATURE']);
} catch (ValidateException $e) {
throw new CmsDataException('[Alma] checkSignature - ' . $e->getMessage());
} catch (\Exception $e) {
throw new CmsDataException('[Alma] Get Merchant Id - ' . $e->getMessage());
}

$cmsInfo = new CmsInfo($this->cmsDataHelper->getCmsInfoArray());
$cmsFeature = new CmsFeatures($this->cmsDataHelper->getCmsFeatureArray());

$payload = $this->payloadFormatter->formatConfigurationPayload($cmsInfo, $cmsFeature);
$this->ajaxRenderAndExit(json_encode(['success' => $payload]));
}

/**
* @param $validateHelper
*
* @return void
*/
public function setValidateHelper($validateHelper)
{
$this->validateHelper = $validateHelper;
}

/**
* @param $settingsHelper
*
* @return void
*/
public function setSettingsHelper($settingsHelper)
{
$this->settingsHelper = $settingsHelper;
}

/**
* @param $payloadFormatter
*
* @return void
*/
public function setPayloadFormatter($payloadFormatter)
{
$this->payloadFormatter = $payloadFormatter;
}

/**
* @param $cmsDataHelper
*
* @return void
*/
public function setCmsDataHelper($cmsDataHelper)
{
$this->cmsDataHelper = $cmsDataHelper;
}
}
27 changes: 22 additions & 5 deletions alma/controllers/hook/GetContentHookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
}

use Alma\API\Entities\Merchant;
use Alma\API\Lib\IntegrationsConfigurationsUtils;
use Alma\API\RequestError;
use Alma\PrestaShop\Builders\Helpers\ApiHelperBuilder;
use Alma\PrestaShop\Builders\Helpers\ContextHelperBuilder;
use Alma\PrestaShop\Builders\Helpers\CustomFieldHelperBuilder;
use Alma\PrestaShop\Builders\Helpers\PriceHelperBuilder;
use Alma\PrestaShop\Builders\Helpers\SettingsHelperBuilder;
Expand All @@ -51,7 +53,9 @@
use Alma\PrestaShop\Helpers\ApiHelper;
use Alma\PrestaShop\Helpers\ApiKeyHelper;
use Alma\PrestaShop\Helpers\ClientHelper;
use Alma\PrestaShop\Helpers\CmsDataHelper;
use Alma\PrestaShop\Helpers\ConstantsHelper;
use Alma\PrestaShop\Helpers\ContextHelper;
use Alma\PrestaShop\Helpers\CustomFieldsHelper;
use Alma\PrestaShop\Helpers\MediaHelper;
use Alma\PrestaShop\Helpers\PriceHelper;
Expand All @@ -69,7 +73,7 @@ final class GetContentHookController extends AdminHookController
/** @var ApiKeyHelper */
private $apiKeyHelper;

/** @var Alma */
/** @var \Module */
protected $module;

/**
Expand Down Expand Up @@ -97,6 +101,11 @@ final class GetContentHookController extends AdminHookController
*/
protected $hasKey;

/**
* @var ContextHelper
*/
protected $contextHelper;

/**
* @var array
*/
Expand Down Expand Up @@ -178,6 +187,9 @@ public function __construct($module)
$mediaHelperBuilder = new MediaHelperBuilder();
$this->mediaHelper = $mediaHelperBuilder->getInstance();

$contextHelperBuilder = new ContextHelperBuilder();
$this->contextHelper = $contextHelperBuilder->getInstance();

$this->hasKey = false;

parent::__construct($module);
Expand Down Expand Up @@ -250,8 +262,8 @@ public function processConfiguration()
}

// Down here, we know the provided API keys are correct (at least the one for the chosen API mode)
$this->setKeyIfValueIsNotObscur($liveKey, ALMA_MODE_LIVE);
$this->setKeyIfValueIsNotObscur($testKey, ALMA_MODE_TEST);
$this->setKeyIfValueIsNotObscure($liveKey, ALMA_MODE_LIVE);
$this->setKeyIfValueIsNotObscure($testKey, ALMA_MODE_TEST);

// Try to get merchant from configured API key/mode
try {
Expand Down Expand Up @@ -421,20 +433,25 @@ public function processConfiguration()
return $credentialsError['message'];
}

if (IntegrationsConfigurationsUtils::isUrlRefreshRequired($this->settingsHelper->getKey(CmsDataHelper::ALMA_CMSDATA_DATE))) {
$this->apiHelper->sendUrlForGatherCmsData($this->contextHelper->getModuleLink('cmsdataexport', [], true));
$this->settingsHelper->updateKey(CmsDataHelper::ALMA_CMSDATA_DATE, time());
}

$this->context->smarty->clearAssign('validation_error');

return $this->module->display($this->module->file, 'getContent.tpl');
}

/**
* Check if Api key are obscur.
* Check if Api key are obscure.
*
* @param string $apiKey
* @param string $mode
*
* @return void
*/
private function setKeyIfValueIsNotObscur($apiKey, $mode)
private function setKeyIfValueIsNotObscure($apiKey, $mode)
{
if (ConstantsHelper::OBSCURE_VALUE === $apiKey) {
return;
Expand Down
33 changes: 33 additions & 0 deletions alma/exceptions/CmsDataException.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 CmsDataException extends AlmaException
{
}
33 changes: 33 additions & 0 deletions alma/exceptions/ValidateException.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 ValidateException extends AlmaException
{
}
5 changes: 4 additions & 1 deletion alma/lib/Builders/Helpers/SettingsHelperBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ public function getInstance()
{
return new SettingsHelper(
$this->getShopHelper(),
$this->getConfigurationHelper()
$this->getConfigurationHelper(),
$this->getCategoryFactory(),
$this->getContextFactory(),
$this->getValidateHelper()
);
}
}
45 changes: 45 additions & 0 deletions alma/lib/Factories/CategoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\Factories;

if (!defined('_PS_VERSION_')) {
exit;
}

/**
* Class CategoryFactory.
*/
class CategoryFactory
{
/**
* @param int $id
*
* @return \Category
*/
public function create($id = null, $idLang = null)
{
return new \Category($id, $idLang);
}
}
22 changes: 22 additions & 0 deletions alma/lib/Factories/ModuleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public function getModule()
return \Module::getInstanceByName(ConstantsHelper::ALMA_MODULE_NAME);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Module name is not a Param - it a getAlmaModule methode and it's an AlmaModuleFactory

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Francois-Gomis I'm not sure to understand your request. Can we speak on that in synchroniously

}

/**
* @return array
*/
public function getModulesInstalled()
{
return \Module::getModulesInstalled();
}

/**
* @return string
*/
Expand All @@ -69,6 +77,20 @@ public function getModuleName()
return '';
}

/**
* @return string
*/
public function getModuleVersion()
{
$module = $this->getModule();

if ($module) {
return $module->version;
}

return '';
}

/**
* @return string|null
*/
Expand Down
Loading