1
0
mirror of synced 2024-11-21 20:46:06 +03:00

Send to CRM activate/deactivate information in marketplace

This commit is contained in:
Akolzin Dmitry 2018-09-03 15:21:40 +03:00
parent 520ad44dce
commit f47af54f1a
4 changed files with 298 additions and 1 deletions

View File

@ -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
*/

View File

@ -0,0 +1,106 @@
<?php
namespace Retailcrm\Retailcrm\Model\Service;
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 NAME = 'Magento 2';
private $accountUrl = null;
private $resourceConfig;
private $apiVersion = 'v5';
private $configuration = [];
public function __construct(
\Magento\Config\Model\ResourceModel\Config $resourceConfig
) {
$this->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;
}
}

25
src/Setup/Uninstall.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace Retailcrm\Retailcrm\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class Uninstall implements \Magento\Framework\Setup\UninstallInterface
{
private $apiClient;
private $integrationModule;
public function __construct(
\Retailcrm\Retailcrm\Helper\Proxy $apiClient,
\Retailcrm\Retailcrm\Model\Service\IntegrationModule $integrationModule
) {
$this->apiClient = $apiClient;
$this->integrationModule = $integrationModule;
}
public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$this->integrationModule->sendConfiguration($this->apiClient, $this->apiClient->getVersion(), false);
}
}

View File

@ -0,0 +1,137 @@
<?php
namespace Retailcrm\Retailcrm\Test\Unit\Model\Service;
class IntegrationModuleTest extends \PHPUnit\Framework\TestCase
{
private $mockResourceConfig;
private $mockApiClient;
private $unit;
const ACCOUNT_URL = 'test';
public function setUp()
{
$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);
}
/**
* @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
]
];
}
}