-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathQueryFactory.php
182 lines (168 loc) · 4.92 KB
/
QueryFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Copyright 2023 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
*/
namespace Magento\QueryXml\Model;
use Magento\QueryXml\Model\DB\SelectBuilderFactory;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\DB\Select;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\QueryXml\Model\Config\ConfigInterface;
/**
* Creates Query object according to configuration
*
* Factory for @see \Magento\QueryXml\Model\Query
*/
class QueryFactory
{
/**
* @var ConfigInterface
*/
private $config;
/**
* @var SelectBuilderFactory
*/
private $selectBuilderFactory;
/**
* @var DB\Assembler\AssemblerInterface[]
*/
private $assemblers;
/**
* @var CacheInterface
*/
private $queryCache;
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var SelectHydrator
*/
private $selectHydrator;
/**
* @var Json
*/
private $jsonSerializer;
/**
* QueryFactory constructor.
*
* @param CacheInterface $queryCache
* @param SelectHydrator $selectHydrator
* @param ObjectManagerInterface $objectManager
* @param SelectBuilderFactory $selectBuilderFactory
* @param ConfigInterface $config
* @param array $assemblers
* @param Json $jsonSerializer
*/
public function __construct(
CacheInterface $queryCache,
SelectHydrator $selectHydrator,
ObjectManagerInterface $objectManager,
SelectBuilderFactory $selectBuilderFactory,
ConfigInterface $config,
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;
}
/**
* Returns query connection name according to configuration
*
* @param string $queryConfig
* @return string
*/
private function getQueryConnectionName($queryConfig)
{
$connectionName = 'default';
if (isset($queryConfig['connection'])) {
$connectionName = $queryConfig['connection'];
}
return $connectionName;
}
/**
* Create query according to configuration settings
*
* @param string $queryName
* @return Query
*/
private function constructQuery($queryName)
{
$queryConfig = $this->config->get($queryName);
$selectBuilder = $this->selectBuilderFactory->create(['queryConfig' => $queryConfig]);
$selectBuilder->setConnectionName($this->getQueryConnectionName($queryConfig));
foreach ($this->assemblers as $assembler) {
$selectBuilder = $assembler->assemble($selectBuilder, $queryConfig);
}
$select = $selectBuilder->create();
return $this->createQueryObject(
$select,
$selectBuilder->getConnectionName(),
$queryConfig
);
}
/**
* Creates query by name
*
* @param string $queryName
* @return Query
*/
public function create($queryName)
{
$cached = $this->queryCache->load($queryName);
if ($cached) {
$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(
$this->jsonSerializer->serialize($query),
$queryName,
['collections']
);
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
]
);
}
}