Skip to content

Commit 2a96c8d

Browse files
committed
MAGETWO-62556: Subscription Request
1 parent 161d44c commit 2a96c8d

File tree

7 files changed

+119
-269
lines changed

7 files changed

+119
-269
lines changed

app/code/Magento/Analytics/Model/AnalyticsConnector/SignUpCommand.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
use Magento\Analytics\Model\AnalyticsToken;
99
use Magento\Analytics\Model\IntegrationManager;
10-
use Magento\Analytics\Model\TokenProvider;
1110

1211
/**
1312
* Class SignUpCommand
@@ -26,11 +25,6 @@ class SignUpCommand implements AnalyticsCommandInterface
2625
*/
2726
private $integrationManager;
2827

29-
/**
30-
* @var TokenProvider
31-
*/
32-
private $tokenProvider;
33-
3428
/**
3529
* @var SignUpRequest
3630
*/
@@ -42,17 +36,14 @@ class SignUpCommand implements AnalyticsCommandInterface
4236
* @param SignUpRequest $signUpRequest
4337
* @param AnalyticsToken $analyticsToken
4438
* @param IntegrationManager $integrationManager
45-
* @param TokenProvider $tokenProvider
4639
*/
4740
public function __construct(
4841
SignUpRequest $signUpRequest,
4942
AnalyticsToken $analyticsToken,
50-
IntegrationManager $integrationManager,
51-
TokenProvider $tokenProvider
43+
IntegrationManager $integrationManager
5244
) {
5345
$this->analyticsToken = $analyticsToken;
5446
$this->integrationManager = $integrationManager;
55-
$this->tokenProvider = $tokenProvider;
5647
$this->signUpRequest = $signUpRequest;
5748
}
5849

@@ -70,7 +61,7 @@ public function __construct(
7061
*/
7162
public function execute()
7263
{
73-
$integrationToken = $this->tokenProvider->getToken();
64+
$integrationToken = $this->integrationManager->generateToken();
7465
if ($integrationToken) {
7566
$this->integrationManager->activateIntegration();
7667
$responseToken = $this->signUpRequest->call($integrationToken);

app/code/Magento/Analytics/Model/IntegrationManager.php

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Integration\Api\IntegrationServiceInterface;
1111
use Magento\Config\Model\Config;
1212
use Magento\Integration\Model\Integration;
13+
use Magento\Integration\Api\OauthServiceInterface;
1314

1415
/**
1516
* Class IntegrationManager
@@ -30,29 +31,26 @@ class IntegrationManager
3031
*/
3132
private $integrationService;
3233

34+
/**
35+
* @var OauthServiceInterface
36+
*/
37+
private $oauthService;
38+
3339
/**
3440
* IntegrationManager constructor
3541
*
3642
* @param Config $config
3743
* @param IntegrationServiceInterface $integrationService
44+
* @param OauthServiceInterface $oauthService
3845
*/
3946
public function __construct(
4047
Config $config,
41-
IntegrationServiceInterface $integrationService
48+
IntegrationServiceInterface $integrationService,
49+
OauthServiceInterface $oauthService
4250
) {
4351
$this->integrationService = $integrationService;
4452
$this->config = $config;
45-
}
46-
47-
/**
48-
* Creates new integration user for MA
49-
*
50-
* @return bool
51-
*/
52-
public function createIntegration()
53-
{
54-
$this->integrationService->create($this->getIntegrationData());
55-
return true;
53+
$this->oauthService = $oauthService;
5654
}
5755

5856
/**
@@ -75,6 +73,37 @@ public function activateIntegration()
7573
return true;
7674
}
7775

76+
/**
77+
* This method execute Generate Token command and enable integration
78+
*
79+
* @return bool|string
80+
*/
81+
public function generateToken()
82+
{
83+
$consumerId = $this->generateIntegration()->getConsumerId();
84+
$accessToken = $this->oauthService->getAccessToken($consumerId);
85+
if (!$accessToken && $this->oauthService->createAccessToken($consumerId, true)) {
86+
$accessToken = $this->oauthService->getAccessToken($consumerId);
87+
}
88+
return $accessToken;
89+
}
90+
91+
/**
92+
* Returns consumer Id for MA integration user
93+
*
94+
* @return \Magento\Integration\Model\Integration
95+
*/
96+
private function generateIntegration()
97+
{
98+
$integration = $this->integrationService->findByName(
99+
$this->config->getConfigDataValue('analytics/integration_name')
100+
);
101+
if (!$integration->getId()) {
102+
$integration = $this->integrationService->create($this->getIntegrationData());
103+
}
104+
return $integration;
105+
}
106+
78107
/**
79108
* Returns default attributes for MA integration user
80109
*

app/code/Magento/Analytics/Model/TokenProvider.php

Lines changed: 0 additions & 79 deletions
This file was deleted.

app/code/Magento/Analytics/Setup/InstallData.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Framework\Setup\ModuleContextInterface;
1111
use Magento\Framework\Setup\ModuleDataSetupInterface;
1212
use Magento\Analytics\Model\NotificationTime;
13-
use Magento\Analytics\Model\IntegrationManager;
1413

1514
/**
1615
* @codeCoverageIgnore
@@ -22,23 +21,15 @@ class InstallData implements InstallDataInterface
2221
*/
2322
private $notificationTime;
2423

25-
/**
26-
* @var IntegrationManager
27-
*/
28-
private $integrationManager;
29-
3024
/**
3125
* InstallData constructor.
3226
*
3327
* @param NotificationTime $notificationTime
34-
* @param IntegrationManager $integrationManager\
3528
*/
3629
public function __construct(
37-
NotificationTime $notificationTime,
38-
IntegrationManager $integrationManager
30+
NotificationTime $notificationTime
3931
) {
4032
$this->notificationTime = $notificationTime;
41-
$this->integrationManager = $integrationManager;
4233
}
4334

4435
/**
@@ -48,6 +39,5 @@ public function __construct(
4839
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
4940
{
5041
$this->notificationTime->storeLastTimeNotification(1);
51-
$this->integrationManager->createIntegration();
5242
}
5343
}

app/code/Magento/Analytics/Test/Unit/Model/AnalyticsConnector/SignUpCommandTest.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
use Magento\Analytics\Model\AnalyticsConnector\SignUpCommand;
99
use Magento\Analytics\Model\AnalyticsConnector\SignUpRequest;
10-
use Magento\Analytics\Model\TokenProvider;
1110
use Magento\Analytics\Model\AnalyticsToken;
1211
use Magento\Analytics\Model\IntegrationManager;
1312
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
@@ -32,11 +31,6 @@ class SignUpCommandTest extends \PHPUnit_Framework_TestCase
3231
*/
3332
private $integrationManagerMock;
3433

35-
/**
36-
* @var TokenProvider|\PHPUnit_Framework_MockObject_MockObject
37-
*/
38-
private $tokenProviderMock;
39-
4034
/**
4135
* @var SignUpRequest|\PHPUnit_Framework_MockObject_MockObject
4236
*/
@@ -50,9 +44,6 @@ protected function setUp()
5044
$this->integrationManagerMock = $this->getMockBuilder(IntegrationManager::class)
5145
->disableOriginalConstructor()
5246
->getMock();
53-
$this->tokenProviderMock = $this->getMockBuilder(TokenProvider::class)
54-
->disableOriginalConstructor()
55-
->getMock();
5647
$this->signUpRequestMock = $this->getMockBuilder(SignUpRequest::class)
5748
->disableOriginalConstructor()
5849
->getMock();
@@ -62,16 +53,15 @@ protected function setUp()
6253
[
6354
'analyticsToken' => $this->analyticsTokenMock,
6455
'integrationManager' => $this->integrationManagerMock,
65-
'tokenProvider' => $this->tokenProviderMock,
6656
'signUpRequest' => $this->signUpRequestMock
6757
]
6858
);
6959
}
7060

7161
public function testExecuteSuccess()
7262
{
73-
$this->tokenProviderMock->expects($this->once())
74-
->method('getToken')
63+
$this->integrationManagerMock->expects($this->once())
64+
->method('generateToken')
7565
->willReturn('IntegrationToken');
7666
$this->integrationManagerMock->expects($this->once())
7767
->method('activateIntegration')
@@ -89,8 +79,8 @@ public function testExecuteSuccess()
8979

9080
public function testExecuteFailureCannotGenerateToken()
9181
{
92-
$this->tokenProviderMock->expects($this->once())
93-
->method('getToken')
82+
$this->integrationManagerMock->expects($this->once())
83+
->method('generateToken')
9484
->willReturn(false);
9585
$this->integrationManagerMock->expects($this->never())
9686
->method('activateIntegration')
@@ -106,8 +96,8 @@ public function testExecuteFailureCannotGenerateToken()
10696

10797
public function testExecuteFailureResponseIsEmpty()
10898
{
109-
$this->tokenProviderMock->expects($this->once())
110-
->method('getToken')
99+
$this->integrationManagerMock->expects($this->once())
100+
->method('generateToken')
111101
->willReturn('IntegrationToken');
112102
$this->integrationManagerMock->expects($this->once())
113103
->method('activateIntegration')

0 commit comments

Comments
 (0)