diff --git a/CHACNGELOG.md b/CHACNGELOG.md index 01d13c8..b19a513 100644 --- a/CHACNGELOG.md +++ b/CHACNGELOG.md @@ -1,3 +1,6 @@ +## 2018-10-25 v.2.3.1 +* Добавлена активация модуля в маркетплейсе retailCRM + ## 2018-08-21 v.2.3.0 * Добавлены консольные команды для выгрузки архива клиентов и заказов * Обработка данных для формирования структуры перед отправкой вынесена в сервисы diff --git a/VERSION b/VERSION index cc6612c..a625450 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.3.0 \ No newline at end of file +2.3.1 \ No newline at end of file diff --git a/src/Model/Config/Backend/ApiVersion.php b/src/Model/Config/Backend/ApiVersion.php index ce7356a..a55388f 100644 --- a/src/Model/Config/Backend/ApiVersion.php +++ b/src/Model/Config/Backend/ApiVersion.php @@ -7,6 +7,8 @@ use Retailcrm\Retailcrm\Helper\Proxy as ApiClient; class ApiVersion extends \Magento\Framework\App\Config\Value { private $api; + private $request; + private $integrationModule; /** * ApiVersion constructor. @@ -16,6 +18,8 @@ class ApiVersion extends \Magento\Framework\App\Config\Value * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList * @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\Model\Service\IntegrationModule $integrationModule * @param ApiClient $api * @param array $data */ @@ -24,12 +28,17 @@ class ApiVersion extends \Magento\Framework\App\Config\Value \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, - ApiClient $api, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, + \Magento\Framework\App\Request\Http $request, + \Retailcrm\Retailcrm\Model\Service\IntegrationModule $integrationModule, + ApiClient $api, array $data = [] ) { $this->api = $api; + $this->request = $request; + $this->integrationModule = $integrationModule; + parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); } @@ -79,6 +88,8 @@ class ApiVersion extends \Magento\Framework\App\Config\Value if (isset($availableVersions)) { if (in_array($apiVersions[$apiVersion], $availableVersions)) { $this->setValue($this->getValue()); + + $this->sendModuleConfiguration($api); } else { throw new \Magento\Framework\Exception\ValidatorException( __('The selected API version is unavailable') @@ -87,6 +98,16 @@ class ApiVersion extends \Magento\Framework\App\Config\Value } } + /** + * @param $api + */ + private function sendModuleConfiguration($api) + { + $this->integrationModule->setApiVersion($api->getVersion()); + $this->integrationModule->setAccountUrl($this->request->getUriString()); + $this->integrationModule->sendConfiguration($api); + } + /** * @param array $data */ diff --git a/src/Model/Service/IntegrationModule.php b/src/Model/Service/IntegrationModule.php new file mode 100644 index 0000000..0d877ab --- /dev/null +++ b/src/Model/Service/IntegrationModule.php @@ -0,0 +1,123 @@ +resourceConfig = $resourceConfig; + $this->helper = $helper; + } + + /** + * @param $accountUrl + */ + public function setAccountUrl($accountUrl) + { + $this->accountUrl = $accountUrl; + } + + /** + * @param $apiVersion + */ + public function setApiVersion($apiVersion) + { + $this->apiVersion = $apiVersion; + } + + /** + * @return array + */ + public function getConfiguration() + { + return $this->configuration; + } + + /** + * @param $active + * + * @return void + */ + 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::INTEGRATION_CODE . '-' . $this->clientId, + 'logo' => self::LOGO, + 'configurationUrl' => $this->accountUrl, + 'active' => $active + ]; + } else { + $this->configuration = [ + 'clientId' => $this->clientId, + 'code' => self::INTEGRATION_CODE . '-' . $this->clientId, + 'integrationCode' => self::INTEGRATION_CODE, + 'active' => $active, + 'name' => self::NAME, + 'logo' => self::LOGO, + 'accountUrl' => $this->accountUrl + ]; + } + } + + /** + * @param \Retailcrm\Retailcrm\Helper\Proxy $apiClient + * @param boolean $active + * + * @return boolean + */ + public function sendConfiguration($apiClient, $active = true) + { + $this->setConfiguration($active); + + if ($this->apiVersion == 'v4') { + $response = $apiClient->marketplaceSettingsEdit(Helper::filterRecursive($this->configuration)); + } else { + $response = $apiClient->integrationModulesEdit(Helper::filterRecursive($this->configuration)); + } + + if (!$response) { + return false; + } + + if ($response->isSuccessful() && $active == true) { + $this->resourceConfig->saveConfig( + Helper::XML_PATH_RETAILCRM . 'general/client_id_in_crm', + $this->clientId + ); + + return true; + } + + return false; + } +} diff --git a/src/Setup/Uninstall.php b/src/Setup/Uninstall.php new file mode 100644 index 0000000..b444255 --- /dev/null +++ b/src/Setup/Uninstall.php @@ -0,0 +1,26 @@ +apiClient = $apiClient; + $this->integrationModule = $integrationModule; + } + + public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context) + { + $this->integrationModule->setApiVersion($this->apiClient->getVersion()); + $this->integrationModule->sendConfiguration($this->apiClient, false); + } +} diff --git a/src/Test/Unit/Model/Service/IntegrationModuleTest.php b/src/Test/Unit/Model/Service/IntegrationModuleTest.php new file mode 100644 index 0000000..7679e05 --- /dev/null +++ b/src/Test/Unit/Model/Service/IntegrationModuleTest.php @@ -0,0 +1,145 @@ +mockData = $this->getMockBuilder(\Retailcrm\Retailcrm\Helper\Data::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->mockResourceConfig = $this->getMockBuilder(\Magento\Config\Model\ResourceModel\Config::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->mockApiClient = $this->getMockBuilder(\Retailcrm\Retailcrm\Helper\Proxy::class) + ->disableOriginalConstructor() + ->setMethods([ + 'marketplaceSettingsEdit', + 'integrationModulesEdit' + ]) + ->getMock(); + + $this->unit = new \Retailcrm\Retailcrm\Model\Service\IntegrationModule( + $this->mockResourceConfig, + $this->mockData + ); + } + + /** + * @param $active + * @param $apiVersion + * @param $isSuccessful + * + * @dataProvider dataProvider + */ + public function testSendConfiguration($active, $apiVersion, $isSuccessful) + { + $response = $this->getMockBuilder(\RetailCrm\Response\ApiResponse::class) + ->disableOriginalConstructor() + ->getMock(); + $response->expects($this->any())->method('isSuccessful')->willReturn($isSuccessful); + + if ($apiVersion == 'v4') { + $this->mockApiClient->expects($this->any())->method('marketplaceSettingsEdit') + ->willReturn($response); + } else { + $this->mockApiClient->expects($this->any())->method('integrationModulesEdit') + ->willReturn($response); + } + + $this->unit->setAccountUrl(self::ACCOUNT_URL); + $this->unit->setApiVersion($apiVersion); + $this->unit->sendConfiguration($this->mockApiClient, $active); + $configuration = $this->unit->getConfiguration(); + + $this->assertNotEmpty($configuration); + $this->assertArrayHasKey('name', $configuration); + $this->assertEquals( + \Retailcrm\Retailcrm\Model\Service\IntegrationModule::NAME, + $configuration['name'] + ); + $this->assertArrayHasKey('logo', $configuration); + $this->assertEquals( + \Retailcrm\Retailcrm\Model\Service\IntegrationModule::LOGO, + $configuration['logo'] + ); + $this->assertArrayHasKey('code', $configuration); + $this->assertContains( + \Retailcrm\Retailcrm\Model\Service\IntegrationModule::INTEGRATION_CODE, + $configuration['code'] + ); + $this->assertArrayHasKey('active', $configuration); + $this->assertEquals($active, $configuration['active']); + + if ($apiVersion == 'v4') { + $this->assertArrayHasKey('configurationUrl', $configuration); + $this->assertEquals(self::ACCOUNT_URL, $configuration['configurationUrl']); + } else { + $this->assertArrayHasKey('accountUrl', $configuration); + $this->assertEquals(self::ACCOUNT_URL, $configuration['accountUrl']); + $this->assertArrayHasKey('integrationCode', $configuration); + $this->assertContains( + \Retailcrm\Retailcrm\Model\Service\IntegrationModule::INTEGRATION_CODE, + $configuration['integrationCode'] + ); + $this->assertArrayHasKey('clientId', $configuration); + $this->assertNotEmpty($configuration['clientId']); + } + } + + public function dataProvider() + { + return [ + [ + 'active' => true, + 'api_version' => 'v4', + 'is_successful' => true + ], + [ + 'active' => false, + 'api_version' => 'v4', + 'is_successful' => true + ], + [ + 'active' => true, + 'api_version' => 'v4', + 'is_successful' => false + ], + [ + 'active' => false, + 'api_version' => 'v4', + 'is_successful' => false + ], + [ + 'active' => true, + 'api_version' => 'v5', + 'is_successful' => true + ], + [ + 'active' => false, + 'api_version' => 'v5', + 'is_successful' => true + ], + [ + 'active' => true, + 'api_version' => 'v5', + 'is_successful' => false + ], + [ + 'active' => false, + 'api_version' => 'v5', + 'is_successful' => false + ] + ]; + } +} diff --git a/src/composer.json b/src/composer.json index 718bfa7..87a3451 100644 --- a/src/composer.json +++ b/src/composer.json @@ -5,7 +5,7 @@ "retailcrm/api-client-php": "~5.0" }, "type": "magento2-module", - "version": "2.3.0", + "version": "2.3.1", "license": [ "OSL-3.0", "AFL-3.0" @@ -13,7 +13,7 @@ "authors": [ { "name": "RetailDriver LLC", - "email": "gorokh@retailcrm.ru", + "email": "integration@retailcrm.ru", "homepage": "https://www.retailcrm.ru", "role": "Developer" }