Skip to content

Commit 255300c

Browse files
committed
MAGETWO-65337: Add notification command
1 parent 0737156 commit 255300c

File tree

4 files changed

+212
-0
lines changed

4 files changed

+212
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Analytics\Model\Connector;
7+
8+
use Magento\Analytics\Model\AnalyticsToken;
9+
use Magento\Framework\HTTP\ZendClient;
10+
use Magento\Config\Model\Config;
11+
use Psr\Log\LoggerInterface;
12+
use Magento\Store\Model\Store;
13+
14+
/**
15+
* Command executes to notify MBI the reports data was collected
16+
*/
17+
class NotifyDataChangedCommand implements CommandInterface
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private $notifyDataChangedUrlPath = 'analytics/url/notify_data_changed';
23+
24+
/**
25+
* @var AnalyticsToken
26+
*/
27+
private $analyticsToken;
28+
29+
/**
30+
* @var Http\ClientInterface
31+
*/
32+
private $httpClient;
33+
34+
/**
35+
* @var Config
36+
*/
37+
private $config;
38+
39+
/**
40+
* @var LoggerInterface
41+
*/
42+
private $logger;
43+
44+
/**
45+
* NotifyDataChangedCommand constructor.
46+
* @param AnalyticsToken $analyticsToken
47+
* @param Http\ClientInterface $httpClient
48+
* @param Config $config
49+
* @param LoggerInterface $logger
50+
*/
51+
public function __construct(
52+
AnalyticsToken $analyticsToken,
53+
Http\ClientInterface $httpClient,
54+
Config $config,
55+
LoggerInterface $logger
56+
) {
57+
$this->analyticsToken = $analyticsToken;
58+
$this->httpClient = $httpClient;
59+
$this->config = $config;
60+
$this->logger = $logger;
61+
}
62+
63+
/**
64+
* Notify MBI the reports data was collected
65+
* @return bool
66+
*/
67+
public function execute()
68+
{
69+
$result = false;
70+
try {
71+
if ($this->analyticsToken->isTokenExist()) {
72+
$this->httpClient->request(
73+
ZendClient::POST,
74+
$this->config->getConfigDataValue($this->notifyDataChangedUrlPath),
75+
$this->getRequestJson(),
76+
['Content-Type: application/json']
77+
);
78+
$result = true;
79+
}
80+
} catch (\Exception $e) {
81+
$this->logger->critical($e);
82+
}
83+
return $result;
84+
}
85+
86+
/**
87+
* Prepares request data in JSON format.
88+
* @return string
89+
*/
90+
private function getRequestJson()
91+
{
92+
return json_encode(
93+
[
94+
"access-token" => $this->analyticsToken->getToken(),
95+
"url" => $this->config->getConfigDataValue(
96+
Store::XML_PATH_SECURE_BASE_URL
97+
),
98+
]
99+
);
100+
}
101+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Analytics\Test\Unit\Model\Connector;
7+
8+
use Magento\Analytics\Model\AnalyticsToken;
9+
use Magento\Framework\HTTP\ZendClient;
10+
use Magento\Config\Model\Config;
11+
use Psr\Log\LoggerInterface;
12+
use Magento\Analytics\Model\Connector\NotifyDataChangedCommand;
13+
use Magento\Analytics\Model\Connector\Http\ClientInterface;
14+
15+
class NotifyDataChangedCommandTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* @var NotifyDataChangedCommand
19+
*/
20+
private $notifyDataChangedCommand;
21+
22+
/**
23+
* @var AnalyticsToken|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $analyticsTokenMock;
26+
27+
/**
28+
* @var ClientInterface|\PHPUnit_Framework_MockObject_MockObject
29+
*/
30+
private $httpClientMock;
31+
32+
/**
33+
* @var Config|\PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
public $configMock;
36+
37+
/**
38+
* @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
private $loggerMock;
41+
42+
/**
43+
* @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
private $responseMock;
46+
47+
protected function setUp()
48+
{
49+
$this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class)
50+
->disableOriginalConstructor()
51+
->getMock();
52+
53+
$this->httpClientMock = $this->getMockBuilder(ClientInterface::class)
54+
->disableOriginalConstructor()
55+
->getMock();
56+
57+
$this->configMock = $this->getMockBuilder(Config::class)
58+
->disableOriginalConstructor()
59+
->getMock();
60+
61+
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
62+
->disableOriginalConstructor()
63+
->getMock();
64+
65+
$this->responseMock = $this->getMockBuilder(\Zend_Http_Response::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
69+
$this->notifyDataChangedCommand = new NotifyDataChangedCommand(
70+
$this->analyticsTokenMock,
71+
$this->httpClientMock,
72+
$this->configMock,
73+
$this->loggerMock
74+
);
75+
}
76+
77+
public function testExecuteSuccess()
78+
{
79+
$configVal = "Config val";
80+
$token = "Secret token!";
81+
$requestJson = sprintf('{"access-token":"%s","url":"%s"}', $token, $configVal);
82+
$this->analyticsTokenMock->expects($this->once())
83+
->method('isTokenExist')
84+
->willReturn(true);
85+
$this->configMock->expects($this->any())
86+
->method('getConfigDataValue')
87+
->willReturn($configVal);
88+
$this->analyticsTokenMock->expects($this->once())
89+
->method('getToken')
90+
->willReturn($token);
91+
$this->httpClientMock->expects($this->once())
92+
->method('request')
93+
->with(
94+
ZendClient::POST,
95+
$configVal,
96+
$requestJson,
97+
['Content-Type: application/json']
98+
)->willReturn($this->responseMock);
99+
$this->assertTrue($this->notifyDataChangedCommand->execute());
100+
}
101+
102+
public function testExecuteWithoutToken()
103+
{
104+
$this->analyticsTokenMock->expects($this->once())
105+
->method('isTokenExist')
106+
->willReturn(false);
107+
$this->assertFalse($this->notifyDataChangedCommand->execute());
108+
}
109+
}

app/code/Magento/Analytics/etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<basic_tier>https://dashboard.rjmetrics.com/v2/magento/signup</basic_tier>
1515
<otp>https://advancedreporting.rjmetrics.com/otp</otp>
1616
<report>https://advancedreporting.rjmetrics.com/report</report>
17+
<notify_data_changed>http://drakelair1.corp.magento.com/mamock/notify</notify_data_changed>
1718
</url>
1819
<integration_name>Magento Analytics user</integration_name>
1920
<general>

app/code/Magento/Analytics/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<argument name="commands" xsi:type="array">
1818
<item name="signUp" xsi:type="string">Magento\Analytics\Model\Connector\SignUpCommand</item>
1919
<item name="update" xsi:type="string">Magento\Analytics\Model\Connector\UpdateCommand</item>
20+
<item name="notifyDataChanged" xsi:type="string">Magento\Analytics\Model\Connector\NotifyDataChangedCommand</item>
2021
</argument>
2122
</arguments>
2223
</type>

0 commit comments

Comments
 (0)