Skip to content

Commit

Permalink
Merge pull request #909 from magento-firedrakes/bugfixes
Browse files Browse the repository at this point in the history
[Firedrakes] Bugfixes and Analytics critical changes
  • Loading branch information
Volodymyr Klymenko authored Mar 10, 2017
2 parents f1dc91b + 1e21672 commit c7e2e11
Show file tree
Hide file tree
Showing 61 changed files with 1,181 additions and 334 deletions.
8 changes: 4 additions & 4 deletions app/code/Magento/Analytics/Cron/CollectData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
namespace Magento\Analytics\Cron;

use Magento\Analytics\Model\ExportDataHandler;
use Magento\Analytics\Model\ExportDataHandlerInterface;
use Magento\Analytics\Model\SubscriptionStatusProvider;

/**
Expand All @@ -16,7 +16,7 @@ class CollectData
/**
* Resource for the handling of a new data collection.
*
* @var ExportDataHandler
* @var ExportDataHandlerInterface
*/
private $exportDataHandler;

Expand All @@ -28,11 +28,11 @@ class CollectData
private $subscriptionStatus;

/**
* @param ExportDataHandler $exportDataHandler
* @param ExportDataHandlerInterface $exportDataHandler
* @param SubscriptionStatusProvider $subscriptionStatus
*/
public function __construct(
ExportDataHandler $exportDataHandler,
ExportDataHandlerInterface $exportDataHandler,
SubscriptionStatusProvider $subscriptionStatus
) {
$this->exportDataHandler = $exportDataHandler;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Analytics\Model\Connector;

use Magento\Analytics\Model\AnalyticsToken;
use Magento\Framework\HTTP\ZendClient;
use Magento\Config\Model\Config;
use Psr\Log\LoggerInterface;
use Magento\Store\Model\Store;

/**
* Command notifies MBI about that data collection was finished.
*/
class NotifyDataChangedCommand implements CommandInterface
{
/**
* @var string
*/
private $notifyDataChangedUrlPath = 'analytics/url/notify_data_changed';

/**
* @var AnalyticsToken
*/
private $analyticsToken;

/**
* @var Http\ClientInterface
*/
private $httpClient;

/**
* @var Config
*/
private $config;

/**
* @var LoggerInterface
*/
private $logger;

/**
* NotifyDataChangedCommand constructor.
* @param AnalyticsToken $analyticsToken
* @param Http\ClientInterface $httpClient
* @param Config $config
* @param LoggerInterface $logger
*/
public function __construct(
AnalyticsToken $analyticsToken,
Http\ClientInterface $httpClient,
Config $config,
LoggerInterface $logger
) {
$this->analyticsToken = $analyticsToken;
$this->httpClient = $httpClient;
$this->config = $config;
$this->logger = $logger;
}

/**
* Notify MBI about that data collection was finished
* @return bool
*/
public function execute()
{
$result = false;
try {
if ($this->analyticsToken->isTokenExist()) {
$this->httpClient->request(
ZendClient::POST,
$this->config->getConfigDataValue($this->notifyDataChangedUrlPath),
$this->getRequestJson(),
['Content-Type: application/json']
);
$result = true;
}
} catch (\Exception $e) {
$this->logger->critical($e);
}
return $result;
}

/**
* Prepares request data in JSON format.
* @return string
*/
private function getRequestJson()
{
return json_encode(
[
"access-token" => $this->analyticsToken->getToken(),
"url" => $this->config->getConfigDataValue(
Store::XML_PATH_SECURE_BASE_URL
),
]
);
}
}
6 changes: 2 additions & 4 deletions app/code/Magento/Analytics/Model/ExportDataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Class for the handling of a new data collection for MBI.
*/
class ExportDataHandler
class ExportDataHandler implements ExportDataHandlerInterface
{
/**
* Subdirectory path for all temporary files.
Expand Down Expand Up @@ -84,9 +84,7 @@ public function __construct(
}

/**
* Execute collecting new data for MBI.
*
* @return bool
* @inheritdoc
*/
public function prepareExportData()
{
Expand Down
19 changes: 19 additions & 0 deletions app/code/Magento/Analytics/Model/ExportDataHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Analytics\Model;

/**
* The interface represents the type of classes that handling of a new data collection for MBI.
*/
interface ExportDataHandlerInterface
{
/**
* Execute collecting new data for MBI.
*
* @return bool
*/
public function prepareExportData();
}
49 changes: 49 additions & 0 deletions app/code/Magento/Analytics/Model/ExportDataHandlerNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Analytics\Model;

use Magento\Analytics\Model\ExportDataHandler;

/**
* Class which add notification behaviour to classes that handling of a new data collection for MBI.
*/
class ExportDataHandlerNotification implements ExportDataHandlerInterface
{
/**
* @var ExportDataHandler
*/
private $exportDataHandler;

/**
* @var Connector
*/
private $analyticsConnector;

/**
* ExportDataHandlerNotification constructor.
*
* @param ExportDataHandlerInterface $exportDataHandler
* @param Connector $connector
*/
public function __construct(ExportDataHandler $exportDataHandler, Connector $connector)
{
$this->exportDataHandler = $exportDataHandler;
$this->analyticsConnector = $connector;
}

/**
* {@inheritdoc}
* Execute notification command.
*
* @return bool
*/
public function prepareExportData()
{
$result = $this->exportDataHandler->prepareExportData();
$this->analyticsConnector->execute('notifyDataChanged');
return $result;
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/Analytics/Model/LinkProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(
public function get()
{
$fileInfo = $this->fileInfoManager->load();
if ($fileInfo->getPath() === null || $fileInfo->getInitializationVector() === null) {
if (!$fileInfo->getPath() || !$fileInfo->getInitializationVector()) {
throw new Exception(__('File is not ready yet.'), 0, Exception::HTTP_NOT_FOUND);
}
$link = $this->linkInterfaceFactory->create();
Expand Down
4 changes: 3 additions & 1 deletion app/code/Magento/Analytics/ReportXml/DB/ColumnsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Magento\Analytics\ReportXml\DB;

use Magento\Framework\DB\Sql\ColumnValueExpression;

/**
* Class ColumnsResolver
*
Expand Down Expand Up @@ -51,7 +53,7 @@ public function getColumns(SelectBuilder $selectBuilder, $entityConfig)
if (isset($attributeData['distinct']) && $attributeData['distinct'] == true) {
$prefix = ' DISTINCT ';
}
$expression = new \Zend_Db_Expr(
$expression = new ColumnValueExpression(
strtoupper($attributeData['function']) . '(' . $prefix . $tableAlias . '.' . $columnName . ')'
);
} else {
Expand Down
7 changes: 5 additions & 2 deletions app/code/Magento/Analytics/ReportXml/DB/ConditionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\Analytics\ReportXml\DB;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Sql\Expression;

/**
* Class ConditionResolver
Expand Down Expand Up @@ -87,7 +88,7 @@ private function getValue($condition, $referencedEntity)
$value = $this->getConnection()->quote($argument);
break;
case "variable":
$value = new \Zend_Db_Expr($argument);
$value = new Expression($argument);
break;
case "identifier":
$value = $this->getConnection()->quoteIdentifier(
Expand All @@ -110,7 +111,9 @@ private function getValue($condition, $referencedEntity)
private function getCondition(SelectBuilder $selectBuilder, $tableName, $condition, $referencedEntity = null)
{
$columns = $selectBuilder->getColumns();
if (isset($columns[$condition['attribute']]) && $columns[$condition['attribute']] instanceof \Zend_Db_Expr) {
if (isset($columns[$condition['attribute']])
&& $columns[$condition['attribute']] instanceof Expression
) {
$expression = $columns[$condition['attribute']];
} else {
$expression = $tableName . '.' . $condition['attribute'];
Expand Down
49 changes: 48 additions & 1 deletion app/code/Magento/Analytics/ReportXml/SelectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Magento\Framework\DB\Select;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\ObjectManagerInterface;

/**
* Class SelectHydrator
Expand Down Expand Up @@ -45,15 +46,22 @@ class SelectHydrator
private $resourceConnection;

/**
* SelectHydrator constructor.
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @param ResourceConnection $resourceConnection
* @param ObjectManagerInterface $objectManager
* @param array $selectParts
*/
public function __construct(
ResourceConnection $resourceConnection,
ObjectManagerInterface $objectManager,
$selectParts = []
) {
$this->resourceConnection = $resourceConnection;
$this->objectManager = $objectManager;
$this->selectParts = $selectParts;
}

Expand Down Expand Up @@ -88,9 +96,48 @@ public function extract(Select $select)
public function recreate(array $selectParts)
{
$select = $this->resourceConnection->getConnection()->select();

$select = $this->processColumns($select, $selectParts);

foreach ($selectParts as $partName => $partValue) {
$select->setPart($partName, $partValue);
}

return $select;
}

/**
* Process COLUMNS part values and add this part into select.
*
* If each column contains information about select expression
* an object with the type of this expression going to be created and assigned to this column.
*
* @param Select $select
* @param array $selectParts
* @return Select
*/
private function processColumns(Select $select, array &$selectParts)
{
if (!empty($selectParts[Select::COLUMNS]) && is_array($selectParts[Select::COLUMNS])) {
$part = [];

foreach ($selectParts[Select::COLUMNS] as $columnEntry) {
list($correlationName, $column, $alias) = $columnEntry;
if (is_array($column) && !empty($column['class'])) {
$expression = $this->objectManager->create(
$column['class'],
isset($column['arguments']) ? $column['arguments'] : []
);
$part[] = [$correlationName, $expression, $alias];
} else {
$part[] = $columnEntry;
}
}

$select->setPart(Select::COLUMNS, $part);
unset($selectParts[Select::COLUMNS]);
}

return $select;
}
}
9 changes: 4 additions & 5 deletions app/code/Magento/Analytics/Test/Unit/Cron/CollectDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Magento\Analytics\Test\Unit\Cron;

use Magento\Analytics\Cron\CollectData;
use Magento\Analytics\Model\ExportDataHandler;
use Magento\Analytics\Model\ExportDataHandlerInterface;
use Magento\Analytics\Model\SubscriptionStatusProvider;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

Expand All @@ -16,7 +16,7 @@
class CollectDataTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ExportDataHandler|\PHPUnit_Framework_MockObject_MockObject
* @var ExportDataHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $exportDataHandlerMock;

Expand All @@ -40,9 +40,8 @@ class CollectDataTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
$this->exportDataHandlerMock = $this->getMockBuilder(ExportDataHandler::class)
->disableOriginalConstructor()
->getMock();
$this->exportDataHandlerMock = $this->getMockBuilder(ExportDataHandlerInterface::class)
->getMockForAbstractClass();

$this->subscriptionStatusMock = $this->getMockBuilder(SubscriptionStatusProvider::class)
->disableOriginalConstructor()
Expand Down
Loading

0 comments on commit c7e2e11

Please sign in to comment.