|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Product\Repositories\ProductRepository |
|
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 |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Repositories; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Product\Utils\MemberNames; |
|
24
|
|
|
use TechDivision\Import\Repositories\AbstractCachedRepository; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Repository implementation to load product data. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Tim Wagner <[email protected]> |
|
30
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
32
|
|
|
* @link https://github.com/techdivision/import |
|
33
|
|
|
* @link http://www.techdivision.com |
|
34
|
|
|
*/ |
|
35
|
|
|
class ProductRepository extends AbstractCachedRepository implements ProductRepositoryInterface |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The prepared statement to load a product with the passed SKU. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \PDOStatement |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $productStmt; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The prepared statement to load the existing products. |
|
47
|
|
|
* |
|
48
|
|
|
* @var \PDOStatement |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $productsStmt; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Return's the primary key name of the entity. |
|
54
|
|
|
* |
|
55
|
|
|
* @return string The name of the entity's primary key |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getPrimaryKeyName() |
|
58
|
|
|
{ |
|
59
|
|
|
return MemberNames::ENTITY_ID; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Initializes the repository's prepared statements. |
|
64
|
|
|
* |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
View Code Duplication |
public function init() |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
|
|
70
|
|
|
// load the utility class name |
|
71
|
|
|
$utilityClassName = $this->getUtilityClassName(); |
|
72
|
|
|
|
|
73
|
|
|
// initialize the prepared statements |
|
74
|
|
|
$this->productStmt = |
|
75
|
|
|
$this->getConnection()->prepare($this->getUtilityClass()->find($utilityClassName::PRODUCT)); |
|
76
|
|
|
$this->productsStmt = |
|
77
|
|
|
$this->getConnection()->prepare($this->getUtilityClass()->find($utilityClassName::PRODUCTS)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Return's the available products. |
|
82
|
|
|
* |
|
83
|
|
|
* @return array The available products |
|
84
|
|
|
*/ |
|
85
|
|
|
public function findAll() |
|
86
|
|
|
{ |
|
87
|
|
|
// load and return the available products |
|
88
|
|
|
$this->productsStmt->execute(); |
|
89
|
|
|
return $this->productsStmt->fetchAll(\PDO::FETCH_ASSOC); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Return's the product with the passed SKU. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $sku The SKU of the product to return |
|
96
|
|
|
* |
|
97
|
|
|
* @return array|null The product |
|
98
|
|
|
*/ |
|
99
|
|
|
public function findOneBySku($sku) |
|
100
|
|
|
{ |
|
101
|
|
|
|
|
102
|
|
|
// return the cached result if available |
|
103
|
|
|
if ($this->isCached($sku)) { |
|
104
|
|
|
return $this->fromCache($sku); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// if not, try to load the product with the passed SKU |
|
108
|
|
|
$this->productStmt->execute(array(MemberNames::SKU => $sku)); |
|
109
|
|
|
|
|
110
|
|
|
// query whether or not the product is available in the database |
|
111
|
|
|
if ($product = $this->productStmt->fetch(\PDO::FETCH_ASSOC)) { |
|
112
|
|
|
// add the product to the cache, register the SKU reference as well |
|
113
|
|
|
$this->toCache( |
|
114
|
|
|
$product[$this->getPrimaryKeyName()], |
|
115
|
|
|
$product, |
|
116
|
|
|
array($sku => $product[$this->getPrimaryKeyName()]) |
|
117
|
|
|
); |
|
118
|
|
|
// finally, return it |
|
119
|
|
|
return $product; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
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.