From f47af54f1a29bc82c53a476d68ed637050ea65c7 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Mon, 3 Sep 2018 15:21:40 +0300 Subject: [PATCH] Send to CRM activate/deactivate information in marketplace --- src/Model/Config/Backend/ApiVersion.php | 31 +++- src/Model/Service/IntegrationModule.php | 106 ++++++++++++++ src/Setup/Uninstall.php | 25 ++++ .../Model/Service/IntegrationModuleTest.php | 137 ++++++++++++++++++ 4 files changed, 298 insertions(+), 1 deletion(-) create mode 100644 src/Model/Service/IntegrationModule.php create mode 100644 src/Setup/Uninstall.php create mode 100644 src/Test/Unit/Model/Service/IntegrationModuleTest.php diff --git a/src/Model/Config/Backend/ApiVersion.php b/src/Model/Config/Backend/ApiVersion.php index ce7356a..f8fa2f7 100644 --- a/src/Model/Config/Backend/ApiVersion.php +++ b/src/Model/Config/Backend/ApiVersion.php @@ -7,6 +7,9 @@ use Retailcrm\Retailcrm\Helper\Proxy as ApiClient; class ApiVersion extends \Magento\Framework\App\Config\Value { private $api; + private $request; + private $helper; + private $integrationModule; /** * ApiVersion constructor. @@ -16,6 +19,9 @@ 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\Helper\Data $helper + * @param \Retailcrm\Retailcrm\Model\Service\IntegrationModule $integrationModule * @param ApiClient $api * @param array $data */ @@ -24,12 +30,19 @@ 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\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); } @@ -79,6 +92,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 +102,20 @@ class ApiVersion extends \Magento\Framework\App\Config\Value } } + /** + * @param $api + */ + 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); + } + } + /** * @param array $data */ diff --git a/src/Model/Service/IntegrationModule.php b/src/Model/Service/IntegrationModule.php new file mode 100644 index 0000000..1233c0f --- /dev/null +++ b/src/Model/Service/IntegrationModule.php @@ -0,0 +1,106 @@ +resourceConfig = $resourceConfig; + } + + /** + * @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) + { + if ($this->apiVersion == 'v4') { + $this->configuration = [ + 'name' => self::NAME, + 'code' => self::CODE, + '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, + '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() && isset($clientId)) { + $this->resourceConfig->saveConfig(Helper::XML_PATH_RETAILCRM . 'general/client_id_in_crm', $clientId); + + return true; + } + + return false; + } +} diff --git a/src/Setup/Uninstall.php b/src/Setup/Uninstall.php new file mode 100644 index 0000000..7cf13c9 --- /dev/null +++ b/src/Setup/Uninstall.php @@ -0,0 +1,25 @@ +apiClient = $apiClient; + $this->integrationModule = $integrationModule; + } + + public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context) + { + $this->integrationModule->sendConfiguration($this->apiClient, $this->apiClient->getVersion(), 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..1f2cca7 --- /dev/null +++ b/src/Test/Unit/Model/Service/IntegrationModuleTest.php @@ -0,0 +1,137 @@ +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); + } + + /** + * @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->assertEquals( + \Retailcrm\Retailcrm\Model\Service\IntegrationModule::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->assertEquals( + \Retailcrm\Retailcrm\Model\Service\IntegrationModule::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 + ] + ]; + } +}