From 15da35916a7f4bd03478f7db445b5d7f20074312 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 25 Oct 2018 12:35:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BD=D0=B5=D1=81=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA=D0=B8?= =?UTF-8?q?=D1=85=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D0=B5=D0=B9=20=D0=B2=20?= =?UTF-8?q?=D0=BC=D0=B0=D1=80=D0=BA=D0=B5=D1=82=D0=BF=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Model/Config/Backend/ApiVersion.php | 14 ++------- src/Model/Service/IntegrationModule.php | 39 ++++++++++++++++++------- src/Setup/Uninstall.php | 3 +- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/Model/Config/Backend/ApiVersion.php b/src/Model/Config/Backend/ApiVersion.php index f8fa2f7..a55388f 100644 --- a/src/Model/Config/Backend/ApiVersion.php +++ b/src/Model/Config/Backend/ApiVersion.php @@ -8,7 +8,6 @@ class ApiVersion extends \Magento\Framework\App\Config\Value { private $api; private $request; - private $helper; private $integrationModule; /** @@ -20,7 +19,6 @@ class ApiVersion extends \Magento\Framework\App\Config\Value * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection * @param \Magento\Framework\App\Request\Http $request - * @param \Retailcrm\Retailcrm\Helper\Data $helper * @param \Retailcrm\Retailcrm\Model\Service\IntegrationModule $integrationModule * @param ApiClient $api * @param array $data @@ -33,14 +31,12 @@ class ApiVersion extends \Magento\Framework\App\Config\Value \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, \Magento\Framework\App\Request\Http $request, - \Retailcrm\Retailcrm\Helper\Data $helper, \Retailcrm\Retailcrm\Model\Service\IntegrationModule $integrationModule, ApiClient $api, array $data = [] ) { $this->api = $api; $this->request = $request; - $this->helper = $helper; $this->integrationModule = $integrationModule; parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); @@ -107,13 +103,9 @@ class ApiVersion extends \Magento\Framework\App\Config\Value */ private function sendModuleConfiguration($api) { - $clientId = $this->helper->getGeneralSettings('client_id_in_crm'); - - if (!$clientId) { - $this->integrationModule->setApiVersion($api->getVersion()); - $this->integrationModule->setAccountUrl($this->request->getUriString()); - $this->integrationModule->sendConfiguration($api); - } + $this->integrationModule->setApiVersion($api->getVersion()); + $this->integrationModule->setAccountUrl($this->request->getUriString()); + $this->integrationModule->sendConfiguration($api); } /** diff --git a/src/Model/Service/IntegrationModule.php b/src/Model/Service/IntegrationModule.php index 1233c0f..0d877ab 100644 --- a/src/Model/Service/IntegrationModule.php +++ b/src/Model/Service/IntegrationModule.php @@ -7,18 +7,28 @@ use Retailcrm\Retailcrm\Helper\Data as Helper; class IntegrationModule { const LOGO = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b846b1fef57e-magento.svg'; - const CODE = 'magento'; + const INTEGRATION_CODE = 'magento'; const NAME = 'Magento 2'; private $accountUrl = null; - private $resourceConfig; private $apiVersion = 'v5'; private $configuration = []; + private $resourceConfig; + private $clientId; + private $helper; + /** + * IntegrationModule constructor. + * + * @param \Magento\Config\Model\ResourceModel\Config $resourceConfig + * @param \Retailcrm\Retailcrm\Helper\Data $helper + */ public function __construct( - \Magento\Config\Model\ResourceModel\Config $resourceConfig + \Magento\Config\Model\ResourceModel\Config $resourceConfig, + Helper $helper ) { $this->resourceConfig = $resourceConfig; + $this->helper = $helper; } /** @@ -52,21 +62,25 @@ class IntegrationModule */ private function setConfiguration($active) { + $this->clientId = $this->helper->getGeneralSettings('client_id_in_crm'); + + if (!$this->clientId) { + $this->clientId = uniqid(); + } + if ($this->apiVersion == 'v4') { $this->configuration = [ 'name' => self::NAME, - 'code' => self::CODE, + 'code' => self::INTEGRATION_CODE . '-' . $this->clientId, 'logo' => self::LOGO, 'configurationUrl' => $this->accountUrl, 'active' => $active ]; } else { - $clientId = hash('md5', date('Y-m-d H:i:s')); - $this->configuration = [ - 'clientId' => $clientId, - 'code' => self::CODE, - 'integrationCode' => self::CODE, + 'clientId' => $this->clientId, + 'code' => self::INTEGRATION_CODE . '-' . $this->clientId, + 'integrationCode' => self::INTEGRATION_CODE, 'active' => $active, 'name' => self::NAME, 'logo' => self::LOGO, @@ -95,8 +109,11 @@ class IntegrationModule return false; } - if ($response->isSuccessful() && isset($clientId)) { - $this->resourceConfig->saveConfig(Helper::XML_PATH_RETAILCRM . 'general/client_id_in_crm', $clientId); + if ($response->isSuccessful() && $active == true) { + $this->resourceConfig->saveConfig( + Helper::XML_PATH_RETAILCRM . 'general/client_id_in_crm', + $this->clientId + ); return true; } diff --git a/src/Setup/Uninstall.php b/src/Setup/Uninstall.php index 7cf13c9..b444255 100644 --- a/src/Setup/Uninstall.php +++ b/src/Setup/Uninstall.php @@ -20,6 +20,7 @@ class Uninstall implements \Magento\Framework\Setup\UninstallInterface public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context) { - $this->integrationModule->sendConfiguration($this->apiClient, $this->apiClient->getVersion(), false); + $this->integrationModule->setApiVersion($this->apiClient->getVersion()); + $this->integrationModule->sendConfiguration($this->apiClient, false); } }