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-product |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Product\Repositories; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Product\Utils\CacheKeys; |
24
|
|
|
use TechDivision\Import\Product\Utils\MemberNames; |
25
|
|
|
use TechDivision\Import\Product\Utils\SqlStatementKeys; |
26
|
|
|
use TechDivision\Import\Repositories\AbstractFinderRepository; |
27
|
|
|
use TechDivision\Import\Connection\ConnectionInterface; |
28
|
|
|
use TechDivision\Import\Repositories\SqlStatementRepositoryInterface; |
29
|
|
|
use TechDivision\Import\Repositories\Finders\FinderFactoryInterface; |
30
|
|
|
use TechDivision\Import\Utils\PrimaryKeyUtilInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Repository implementation to load product 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 ProductRepository extends AbstractFinderRepository implements ProductRepositoryInterface |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The primary key utility instance. |
46
|
|
|
* |
47
|
|
|
* @var \TechDivision\Import\Utils\PrimaryKeyUtilInterface |
48
|
|
|
*/ |
49
|
|
|
protected $primaryKeyUtil; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initialize the repository with the passed connection and utility class name. |
53
|
|
|
* . |
54
|
|
|
* @param \TechDivision\Import\Connection\ConnectionInterface $connection The connection instance |
55
|
|
|
* @param \TechDivision\Import\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The SQL repository instance |
56
|
|
|
* @param \TechDivision\Import\Repositories\Finders\FinderFactoryInterface $finderFactory The finder factory instance |
57
|
|
|
* @param \TechDivision\Import\Utils\PrimaryKeyUtilInterface $primaryKeyUtil The primary key utility instance |
58
|
|
|
*/ |
59
|
|
|
public function __construct( |
60
|
|
|
ConnectionInterface $connection, |
61
|
|
|
SqlStatementRepositoryInterface $sqlStatementRepository, |
62
|
|
|
FinderFactoryInterface $finderFactory, |
63
|
|
|
PrimaryKeyUtilInterface $primaryKeyUtil |
64
|
|
|
) { |
65
|
|
|
|
66
|
|
|
// set the primary key utility instance |
67
|
|
|
$this->primaryKeyUtil = $primaryKeyUtil; |
68
|
|
|
|
69
|
|
|
// pass the connection, SQL statement repository and the primary key utility to the parent class |
70
|
|
|
parent::__construct($connection, $sqlStatementRepository, $finderFactory); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Initializes the repository's prepared statements. |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function init() |
79
|
|
|
{ |
80
|
|
|
|
81
|
|
|
// initialize the prepared statements |
82
|
|
|
$this->addFinder($this->finderFactory->createFinder($this, SqlStatementKeys::PRODUCT)); |
83
|
|
|
$this->addFinder($this->finderFactory->createFinder($this, SqlStatementKeys::PRODUCTS)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return's the finder's entity name. |
88
|
|
|
* |
89
|
|
|
* @return string The finder's entity name |
90
|
|
|
*/ |
91
|
|
|
public function getEntityName() |
92
|
|
|
{ |
93
|
|
|
return CacheKeys::PRODUCT; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return's the primary key name of the entity. |
98
|
|
|
* |
99
|
|
|
* @return string The name of the entity's primary key |
100
|
|
|
*/ |
101
|
|
|
public function getPrimaryKeyName() |
102
|
|
|
{ |
103
|
|
|
return $this->primaryKeyUtil->getPrimaryKeyMemberName(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return's the entity unique key name. |
108
|
|
|
* |
109
|
|
|
* @return string The name of the entity's unique key |
110
|
|
|
*/ |
111
|
|
|
public function getUniqueKeyName() |
112
|
|
|
{ |
113
|
|
|
return MemberNames::SKU; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Return's the available products. |
118
|
|
|
* |
119
|
|
|
* @return array The available products |
120
|
|
|
*/ |
121
|
|
|
public function findAll() |
122
|
|
|
{ |
123
|
|
|
foreach ($this->getFinder(SqlStatementKeys::PRODUCTS)->find() as $result) { |
124
|
|
|
yield $result; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Return's the product with the passed SKU. |
130
|
|
|
* |
131
|
|
|
* @param string $sku The SKU of the product to return |
132
|
|
|
* |
133
|
|
|
* @return array|null The product |
134
|
|
|
*/ |
135
|
|
|
public function findOneBySku($sku) |
136
|
|
|
{ |
137
|
|
|
return $this->getFinder(SqlStatementKeys::PRODUCT)->find(array($this->getUniqueKeyName() => $sku)); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|