Skip to content

[Analytics] Code refactor & covered unit test in Analytics/ReportXml/QueryFactory #26761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 50 additions & 19 deletions app/code/Magento/Analytics/ReportXml/QueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Analytics\ReportXml;

use Magento\Analytics\ReportXml\DB\SelectBuilderFactory;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\DB\Select;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Serialize\Serializer\Json;

/**
* Creates Query object according to configuration
*
* Factory for @see \Magento\Analytics\ReportXml\Query
*/
class QueryFactory
Expand Down Expand Up @@ -45,6 +49,11 @@ class QueryFactory
*/
private $selectHydrator;

/**
* @var Json
*/
private $jsonSerializer;

/**
* QueryFactory constructor.
*
Expand All @@ -54,21 +63,24 @@ class QueryFactory
* @param SelectBuilderFactory $selectBuilderFactory
* @param Config $config
* @param array $assemblers
* @param Json $jsonSerializer
*/
public function __construct(
CacheInterface $queryCache,
SelectHydrator $selectHydrator,
ObjectManagerInterface $objectManager,
SelectBuilderFactory $selectBuilderFactory,
Config $config,
array $assemblers
array $assemblers,
Json $jsonSerializer
) {
$this->config = $config;
$this->selectBuilderFactory = $selectBuilderFactory;
$this->assemblers = $assemblers;
$this->queryCache = $queryCache;
$this->objectManager = $objectManager;
$this->selectHydrator = $selectHydrator;
$this->jsonSerializer = $jsonSerializer;
}

/**
Expand Down Expand Up @@ -101,14 +113,10 @@ private function constructQuery($queryName)
$selectBuilder = $assembler->assemble($selectBuilder, $queryConfig);
}
$select = $selectBuilder->create();
return $this->objectManager->create(
Query::class,
[
'select' => $select,
'selectHydrator' => $this->selectHydrator,
'connectionName' => $selectBuilder->getConnectionName(),
'config' => $queryConfig
]
return $this->createQueryObject(
$select,
$selectBuilder->getConnectionName(),
$queryConfig
);
}

Expand All @@ -122,19 +130,42 @@ public function create($queryName)
{
$cached = $this->queryCache->load($queryName);
if ($cached) {
$queryData = json_decode($cached, true);
return $this->objectManager->create(
Query::class,
[
'select' => $this->selectHydrator->recreate($queryData['select_parts']),
'selectHydrator' => $this->selectHydrator,
'connectionName' => $queryData['connectionName'],
'config' => $queryData['config']
]
$queryData = $this->jsonSerializer->unserialize($cached);
return $this->createQueryObject(
$this->selectHydrator->recreate($queryData['select_parts']),
$queryData['connectionName'],
$queryData['config']
);
}
$query = $this->constructQuery($queryName);
$this->queryCache->save(json_encode($query), $queryName);
$this->queryCache->save(
$this->jsonSerializer->serialize($query),
$queryName
);
return $query;
}

/**
* Create query class using objectmanger
*
* @param Select $select
* @param string $connection
* @param array $queryConfig
* @return Query
*/
private function createQueryObject(
Select $select,
string $connection,
array $queryConfig
) {
return $this->objectManager->create(
Query::class,
[
'select' => $select,
'selectHydrator' => $this->selectHydrator,
'connectionName' => $connection,
'config' => $queryConfig
]
);
}
}
Loading