|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\Repositories\ProductVarcharRepository |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import-product |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Repositories; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Product\Utils\ParamNames; |
|
24
|
|
|
use TechDivision\Import\Product\Utils\SqlStatementKeys; |
|
25
|
|
|
use TechDivision\Import\Cache\CacheAdapterInterface; |
|
26
|
|
|
use TechDivision\Import\Repositories\AbstractRepository; |
|
27
|
|
|
use TechDivision\Import\Connection\ConnectionInterface; |
|
28
|
|
|
use TechDivision\Import\Repositories\SqlStatementRepositoryInterface; |
|
29
|
|
|
use TechDivision\Import\Product\Utils\MemberNames; |
|
30
|
|
|
use TechDivision\Import\Product\Utils\CacheKeys; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Repository implementation to load product varchar attribute data. |
|
34
|
|
|
* |
|
35
|
|
|
* @author Tim Wagner <[email protected]> |
|
36
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
37
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
38
|
|
|
* @link https://github.com/techdivision/import-product |
|
39
|
|
|
* @link http://www.techdivision.com |
|
40
|
|
|
*/ |
|
41
|
|
|
class ProductVarcharRepository extends AbstractRepository implements ProductVarcharRepositoryInterface |
|
42
|
|
|
{ |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The cache adapter instance. |
|
46
|
|
|
* |
|
47
|
|
|
* @var \TechDivision\Import\Cache\CacheAdapterInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $cacheAdapter; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The prepared statement to load the existing product varchar attributes with the passed entity/store ID. |
|
53
|
|
|
* |
|
54
|
|
|
* @var \PDOStatement |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $productVarcharsStmt; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The prepared statement to load the existing product varchar attribute with the passed attribute code |
|
60
|
|
|
* entity type/store ID. |
|
61
|
|
|
* |
|
62
|
|
|
* @var \PDOStatement |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $productVarcharsByAttributeCodeAndEntityTypeIdAndStoreIdStmt; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* The prepared statement to load the existing product varchar attribute with the passed attribute code |
|
68
|
|
|
* entity type/store ID as well as the passed value. |
|
69
|
|
|
* |
|
70
|
|
|
* @var \PDOStatement |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $productVarcharByAttributeCodeAndEntityTypeIdAndStoreIdAndValueStmt; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Initialize the repository with the passed connection and utility class name. |
|
76
|
|
|
* . |
|
77
|
|
|
* @param \TechDivision\Import\Connection\ConnectionInterface $connection The connection instance |
|
78
|
|
|
* @param \TechDivision\Import\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The SQL repository instance |
|
79
|
|
|
* @param \TechDivision\Import\Cache\CacheAdapterInterface $cacheAdapter The cache adapter instance |
|
80
|
|
|
*/ |
|
81
|
|
|
public function __construct( |
|
82
|
|
|
ConnectionInterface $connection, |
|
83
|
|
|
SqlStatementRepositoryInterface $sqlStatementRepository, |
|
84
|
|
|
CacheAdapterInterface $cacheAdapter |
|
85
|
|
|
) { |
|
86
|
|
|
|
|
87
|
|
|
// pass the connection the SQL statement repository to the parent class |
|
88
|
|
|
parent::__construct($connection, $sqlStatementRepository); |
|
89
|
|
|
|
|
90
|
|
|
// set the cache adapter instance |
|
91
|
|
|
$this->cacheAdapter = $cacheAdapter; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns the cache adapter instance used to warm the repository. |
|
96
|
|
|
* |
|
97
|
|
|
* @return \TechDivision\Import\Cache\CacheAdapterInterface The repository's cache adapter instance |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getCacheAdapter() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->cacheAdapter; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Return's the primary key name of the entity. |
|
106
|
|
|
* |
|
107
|
|
|
* @return string The name of the entity's primary key |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getPrimaryKeyName() |
|
110
|
|
|
{ |
|
111
|
|
|
return MemberNames::VALUE_ID; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Initializes the repository's prepared statements. |
|
116
|
|
|
* |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
public function init() |
|
120
|
|
|
{ |
|
121
|
|
|
|
|
122
|
|
|
// initialize the prepared statements |
|
123
|
|
|
$this->productVarcharsStmt = |
|
124
|
|
|
$this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::PRODUCT_VARCHARS)); |
|
125
|
|
|
$this->productVarcharsByAttributeCodeAndEntityTypeIdAndStoreIdStmt = |
|
126
|
|
|
$this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::PRODUCT_VARCHAR_BY_ATTRIBUTE_CODE_AND_ENTITY_TYPE_ID_AND_STORE_ID)); |
|
127
|
|
|
$this->productVarcharByAttributeCodeAndEntityTypeIdAndStoreIdAndValueStmt = |
|
128
|
|
|
$this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::PRODUCT_VARCHAR_BY_ATTRIBUTE_CODE_AND_ENTITY_TYPE_ID_AND_STORE_ID_AND_VALUE)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Load's and return's the varchar attributes with the passed primary key/store ID. |
|
133
|
|
|
* |
|
134
|
|
|
* @param integer $pk The primary key of the attributes |
|
135
|
|
|
* @param integer $storeId The store ID of the attributes |
|
136
|
|
|
* |
|
137
|
|
|
* @return array The varchar attributes |
|
138
|
|
|
*/ |
|
139
|
|
|
public function findAllByPrimaryKeyAndStoreId($pk, $storeId) |
|
140
|
|
|
{ |
|
141
|
|
|
|
|
142
|
|
|
// prepare the params |
|
143
|
|
|
$params = array( |
|
144
|
|
|
ParamNames::PK => $pk, |
|
145
|
|
|
ParamNames::STORE_ID => $storeId |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
// load and return the product varchar attributes with the passed primary key/store ID |
|
149
|
|
|
$this->productVarcharsStmt->execute($params); |
|
150
|
|
|
|
|
151
|
|
|
// fetch the values and return them |
|
152
|
|
|
while ($record = $this->productVarcharsStmt->fetch(\PDO::FETCH_ASSOC)) { |
|
153
|
|
|
yield $record; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Load's and return's the varchar attributes with the passed params. |
|
159
|
|
|
* |
|
160
|
|
|
* @param integer $attributeCode The attribute code of the varchar attribute |
|
161
|
|
|
* @param integer $entityTypeId The entity type ID of the varchar attribute |
|
162
|
|
|
* @param integer $storeId The store ID of the varchar attribute |
|
163
|
|
|
* |
|
164
|
|
|
* @return array The varchar attributes |
|
165
|
|
|
*/ |
|
166
|
|
|
public function findAllByAttributeCodeAndEntityTypeIdAndStoreId($attributeCode, $entityTypeId, $storeId) |
|
167
|
|
|
{ |
|
168
|
|
|
|
|
169
|
|
|
// prepare the params |
|
170
|
|
|
$params = array( |
|
171
|
|
|
ParamNames::ATTRIBUTE_CODE => $attributeCode, |
|
172
|
|
|
ParamNames::ENTITY_TYPE_ID => $entityTypeId, |
|
173
|
|
|
ParamNames::STORE_ID => $storeId, |
|
174
|
|
|
); |
|
175
|
|
|
|
|
176
|
|
|
// load and return the product varchar attributes with the passed primary key/store ID |
|
177
|
|
|
$this->productVarcharsByAttributeCodeAndEntityTypeIdAndStoreIdStmt->execute($params); |
|
178
|
|
|
|
|
179
|
|
|
// fetch the values and return them |
|
180
|
|
|
while ($record = $this->productVarcharsByAttributeCodeAndEntityTypeIdAndStoreIdStmt->fetch(\PDO::FETCH_ASSOC)) { |
|
181
|
|
|
yield $record; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Load's and return's the varchar attribute with the passed params. |
|
187
|
|
|
* |
|
188
|
|
|
* @param integer $attributeCode The attribute code of the varchar attribute |
|
189
|
|
|
* @param integer $entityTypeId The entity type ID of the varchar attribute |
|
190
|
|
|
* @param integer $storeId The store ID of the varchar attribute |
|
191
|
|
|
* @param string $value The value of the varchar attribute |
|
192
|
|
|
* |
|
193
|
|
|
* @return array|null The varchar attribute |
|
194
|
|
|
*/ |
|
195
|
|
|
public function findOneByAttributeCodeAndEntityTypeIdAndStoreIdAndValue($attributeCode, $entityTypeId, $storeId, $value) |
|
196
|
|
|
{ |
|
197
|
|
|
|
|
198
|
|
|
// prepare the params |
|
199
|
|
|
$params = array( |
|
200
|
|
|
ParamNames::ATTRIBUTE_CODE => $attributeCode, |
|
201
|
|
|
ParamNames::ENTITY_TYPE_ID => $entityTypeId, |
|
202
|
|
|
ParamNames::STORE_ID => $storeId, |
|
203
|
|
|
ParamNames::VALUE => $value |
|
204
|
|
|
); |
|
205
|
|
|
|
|
206
|
|
|
// prepare the cache key |
|
207
|
|
|
$cacheKey = $this->cacheAdapter->cacheKey(array(SqlStatementKeys::PRODUCT_VARCHAR_BY_ATTRIBUTE_CODE_AND_ENTITY_TYPE_ID_AND_STORE_ID_AND_VALUE => $params)); |
|
208
|
|
|
|
|
209
|
|
|
// query whether or not the item has already been cached |
|
210
|
|
|
if ($this->cacheAdapter->isCached($cacheKey)) { |
|
211
|
|
|
return $this->cacheAdapter->fromCache($cacheKey); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
// load and return the product varchar attribute with the passed parameters |
|
215
|
|
|
$this->productVarcharByAttributeCodeAndEntityTypeIdAndStoreIdAndValueStmt->execute($params); |
|
216
|
|
|
|
|
217
|
|
|
// query whether or not the product varchar value is available in the database |
|
218
|
|
View Code Duplication |
if ($productVarchar = $this->productVarcharByAttributeCodeAndEntityTypeIdAndStoreIdAndValueStmt->fetch(\PDO::FETCH_ASSOC)) { |
|
|
|
|
|
|
219
|
|
|
// prepare the unique cache key for the EAV attribute option value |
|
220
|
|
|
$uniqueKey = array(CacheKeys::PRODUCT_VARCHAR => $productVarchar[$this->getPrimaryKeyName()]); |
|
221
|
|
|
// add the EAV attribute option value to the cache, register the cache key reference as well |
|
222
|
|
|
$this->cacheAdapter->toCache($uniqueKey, $productVarchar, array($cacheKey => $uniqueKey)); |
|
|
|
|
|
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
// finally, return it |
|
226
|
|
|
return $productVarchar; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.