Skip to content

Commit c7e2e11

Browse files
author
Volodymyr Klymenko
authored
Merge pull request #909 from magento-firedrakes/bugfixes
[Firedrakes] Bugfixes and Analytics critical changes
2 parents f1dc91b + 1e21672 commit c7e2e11

File tree

61 files changed

+1181
-334
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1181
-334
lines changed

app/code/Magento/Analytics/Cron/CollectData.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
namespace Magento\Analytics\Cron;
77

8-
use Magento\Analytics\Model\ExportDataHandler;
8+
use Magento\Analytics\Model\ExportDataHandlerInterface;
99
use Magento\Analytics\Model\SubscriptionStatusProvider;
1010

1111
/**
@@ -16,7 +16,7 @@ class CollectData
1616
/**
1717
* Resource for the handling of a new data collection.
1818
*
19-
* @var ExportDataHandler
19+
* @var ExportDataHandlerInterface
2020
*/
2121
private $exportDataHandler;
2222

@@ -28,11 +28,11 @@ class CollectData
2828
private $subscriptionStatus;
2929

3030
/**
31-
* @param ExportDataHandler $exportDataHandler
31+
* @param ExportDataHandlerInterface $exportDataHandler
3232
* @param SubscriptionStatusProvider $subscriptionStatus
3333
*/
3434
public function __construct(
35-
ExportDataHandler $exportDataHandler,
35+
ExportDataHandlerInterface $exportDataHandler,
3636
SubscriptionStatusProvider $subscriptionStatus
3737
) {
3838
$this->exportDataHandler = $exportDataHandler;
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 notifies MBI about that data collection was finished.
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 about that data collection was finished
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+
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Class for the handling of a new data collection for MBI.
1717
*/
18-
class ExportDataHandler
18+
class ExportDataHandler implements ExportDataHandlerInterface
1919
{
2020
/**
2121
* Subdirectory path for all temporary files.
@@ -84,9 +84,7 @@ public function __construct(
8484
}
8585

8686
/**
87-
* Execute collecting new data for MBI.
88-
*
89-
* @return bool
87+
* @inheritdoc
9088
*/
9189
public function prepareExportData()
9290
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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;
7+
8+
/**
9+
* The interface represents the type of classes that handling of a new data collection for MBI.
10+
*/
11+
interface ExportDataHandlerInterface
12+
{
13+
/**
14+
* Execute collecting new data for MBI.
15+
*
16+
* @return bool
17+
*/
18+
public function prepareExportData();
19+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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;
7+
8+
use Magento\Analytics\Model\ExportDataHandler;
9+
10+
/**
11+
* Class which add notification behaviour to classes that handling of a new data collection for MBI.
12+
*/
13+
class ExportDataHandlerNotification implements ExportDataHandlerInterface
14+
{
15+
/**
16+
* @var ExportDataHandler
17+
*/
18+
private $exportDataHandler;
19+
20+
/**
21+
* @var Connector
22+
*/
23+
private $analyticsConnector;
24+
25+
/**
26+
* ExportDataHandlerNotification constructor.
27+
*
28+
* @param ExportDataHandlerInterface $exportDataHandler
29+
* @param Connector $connector
30+
*/
31+
public function __construct(ExportDataHandler $exportDataHandler, Connector $connector)
32+
{
33+
$this->exportDataHandler = $exportDataHandler;
34+
$this->analyticsConnector = $connector;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
* Execute notification command.
40+
*
41+
* @return bool
42+
*/
43+
public function prepareExportData()
44+
{
45+
$result = $this->exportDataHandler->prepareExportData();
46+
$this->analyticsConnector->execute('notifyDataChanged');
47+
return $result;
48+
}
49+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct(
5252
public function get()
5353
{
5454
$fileInfo = $this->fileInfoManager->load();
55-
if ($fileInfo->getPath() === null || $fileInfo->getInitializationVector() === null) {
55+
if (!$fileInfo->getPath() || !$fileInfo->getInitializationVector()) {
5656
throw new Exception(__('File is not ready yet.'), 0, Exception::HTTP_NOT_FOUND);
5757
}
5858
$link = $this->linkInterfaceFactory->create();

app/code/Magento/Analytics/ReportXml/DB/ColumnsResolver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Analytics\ReportXml\DB;
88

9+
use Magento\Framework\DB\Sql\ColumnValueExpression;
10+
911
/**
1012
* Class ColumnsResolver
1113
*
@@ -51,7 +53,7 @@ public function getColumns(SelectBuilder $selectBuilder, $entityConfig)
5153
if (isset($attributeData['distinct']) && $attributeData['distinct'] == true) {
5254
$prefix = ' DISTINCT ';
5355
}
54-
$expression = new \Zend_Db_Expr(
56+
$expression = new ColumnValueExpression(
5557
strtoupper($attributeData['function']) . '(' . $prefix . $tableAlias . '.' . $columnName . ')'
5658
);
5759
} else {

app/code/Magento/Analytics/ReportXml/DB/ConditionResolver.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Analytics\ReportXml\DB;
88

99
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\DB\Sql\Expression;
1011

1112
/**
1213
* Class ConditionResolver
@@ -87,7 +88,7 @@ private function getValue($condition, $referencedEntity)
8788
$value = $this->getConnection()->quote($argument);
8889
break;
8990
case "variable":
90-
$value = new \Zend_Db_Expr($argument);
91+
$value = new Expression($argument);
9192
break;
9293
case "identifier":
9394
$value = $this->getConnection()->quoteIdentifier(
@@ -110,7 +111,9 @@ private function getValue($condition, $referencedEntity)
110111
private function getCondition(SelectBuilder $selectBuilder, $tableName, $condition, $referencedEntity = null)
111112
{
112113
$columns = $selectBuilder->getColumns();
113-
if (isset($columns[$condition['attribute']]) && $columns[$condition['attribute']] instanceof \Zend_Db_Expr) {
114+
if (isset($columns[$condition['attribute']])
115+
&& $columns[$condition['attribute']] instanceof Expression
116+
) {
114117
$expression = $columns[$condition['attribute']];
115118
} else {
116119
$expression = $tableName . '.' . $condition['attribute'];

app/code/Magento/Analytics/ReportXml/SelectHydrator.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\DB\Select;
1010
use Magento\Framework\App\ResourceConnection;
11+
use Magento\Framework\ObjectManagerInterface;
1112

1213
/**
1314
* Class SelectHydrator
@@ -45,15 +46,22 @@ class SelectHydrator
4546
private $resourceConnection;
4647

4748
/**
48-
* SelectHydrator constructor.
49+
* @var ObjectManagerInterface
50+
*/
51+
private $objectManager;
52+
53+
/**
4954
* @param ResourceConnection $resourceConnection
55+
* @param ObjectManagerInterface $objectManager
5056
* @param array $selectParts
5157
*/
5258
public function __construct(
5359
ResourceConnection $resourceConnection,
60+
ObjectManagerInterface $objectManager,
5461
$selectParts = []
5562
) {
5663
$this->resourceConnection = $resourceConnection;
64+
$this->objectManager = $objectManager;
5765
$this->selectParts = $selectParts;
5866
}
5967

@@ -88,9 +96,48 @@ public function extract(Select $select)
8896
public function recreate(array $selectParts)
8997
{
9098
$select = $this->resourceConnection->getConnection()->select();
99+
100+
$select = $this->processColumns($select, $selectParts);
101+
91102
foreach ($selectParts as $partName => $partValue) {
92103
$select->setPart($partName, $partValue);
93104
}
105+
106+
return $select;
107+
}
108+
109+
/**
110+
* Process COLUMNS part values and add this part into select.
111+
*
112+
* If each column contains information about select expression
113+
* an object with the type of this expression going to be created and assigned to this column.
114+
*
115+
* @param Select $select
116+
* @param array $selectParts
117+
* @return Select
118+
*/
119+
private function processColumns(Select $select, array &$selectParts)
120+
{
121+
if (!empty($selectParts[Select::COLUMNS]) && is_array($selectParts[Select::COLUMNS])) {
122+
$part = [];
123+
124+
foreach ($selectParts[Select::COLUMNS] as $columnEntry) {
125+
list($correlationName, $column, $alias) = $columnEntry;
126+
if (is_array($column) && !empty($column['class'])) {
127+
$expression = $this->objectManager->create(
128+
$column['class'],
129+
isset($column['arguments']) ? $column['arguments'] : []
130+
);
131+
$part[] = [$correlationName, $expression, $alias];
132+
} else {
133+
$part[] = $columnEntry;
134+
}
135+
}
136+
137+
$select->setPart(Select::COLUMNS, $part);
138+
unset($selectParts[Select::COLUMNS]);
139+
}
140+
94141
return $select;
95142
}
96143
}

app/code/Magento/Analytics/Test/Unit/Cron/CollectDataTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\Analytics\Test\Unit\Cron;
77

88
use Magento\Analytics\Cron\CollectData;
9-
use Magento\Analytics\Model\ExportDataHandler;
9+
use Magento\Analytics\Model\ExportDataHandlerInterface;
1010
use Magento\Analytics\Model\SubscriptionStatusProvider;
1111
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1212

@@ -16,7 +16,7 @@
1616
class CollectDataTest extends \PHPUnit_Framework_TestCase
1717
{
1818
/**
19-
* @var ExportDataHandler|\PHPUnit_Framework_MockObject_MockObject
19+
* @var ExportDataHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
2020
*/
2121
private $exportDataHandlerMock;
2222

@@ -40,9 +40,8 @@ class CollectDataTest extends \PHPUnit_Framework_TestCase
4040
*/
4141
protected function setUp()
4242
{
43-
$this->exportDataHandlerMock = $this->getMockBuilder(ExportDataHandler::class)
44-
->disableOriginalConstructor()
45-
->getMock();
43+
$this->exportDataHandlerMock = $this->getMockBuilder(ExportDataHandlerInterface::class)
44+
->getMockForAbstractClass();
4645

4746
$this->subscriptionStatusMock = $this->getMockBuilder(SubscriptionStatusProvider::class)
4847
->disableOriginalConstructor()

0 commit comments

Comments
 (0)