1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Product\Grouped\Services\ProductGroupedProcessor |
5
|
|
|
* |
6
|
|
|
* PHP version 7 |
7
|
|
|
* |
8
|
|
|
* @author Tim Wagner <[email protected]> |
9
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/MIT |
11
|
|
|
* @link https://github.com/techdivision/import-product-grouped |
12
|
|
|
* @link http://www.techdivision.com |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace TechDivision\Import\Product\Grouped\Services; |
16
|
|
|
|
17
|
|
|
use TechDivision\Import\Dbal\Actions\ActionInterface; |
18
|
|
|
use TechDivision\Import\Dbal\Connection\ConnectionInterface; |
19
|
|
|
use TechDivision\Import\Product\Repositories\ProductRelationRepositoryInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The product link processor implementation. |
23
|
|
|
* |
24
|
|
|
* @author Tim Wagner <[email protected]> |
25
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
26
|
|
|
* @license https://opensource.org/licenses/MIT |
27
|
|
|
* @link https://github.com/techdivision/import-product-grouped |
28
|
|
|
* @link http://www.techdivision.com |
29
|
|
|
*/ |
30
|
|
|
class ProductGroupedProcessor implements ProductGroupedProcessorInterface |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A PDO connection initialized with the values from the Doctrine EntityManager. |
35
|
|
|
* |
36
|
|
|
* @var \TechDivision\Import\Dbal\Connection\ConnectionInterface |
37
|
|
|
*/ |
38
|
|
|
protected $connection; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The repository to access product relations. |
42
|
|
|
* |
43
|
|
|
* @var \TechDivision\Import\Product\Repositories\ProductRelationRepositoryInterface |
44
|
|
|
*/ |
45
|
|
|
protected $productRelationRepository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The action for product relation CRUD methods. |
49
|
|
|
* |
50
|
|
|
* @var \TechDivision\Import\Dbal\Actions\ActionInterface |
51
|
|
|
*/ |
52
|
|
|
protected $productRelationAction; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Initialize the processor with the necessary assembler and repository instances. |
56
|
|
|
* |
57
|
|
|
* @param \TechDivision\Import\Dbal\Connection\ConnectionInterface $connection The connection to use |
58
|
|
|
* @param \TechDivision\Import\Product\Repositories\ProductRelationRepositoryInterface $productRelationRepository The product relation repository to use |
59
|
|
|
* @param \TechDivision\Import\Dbal\Actions\ActionInterface $productRelationAction The product relation action to use |
60
|
|
|
*/ |
61
|
|
|
public function __construct( |
62
|
|
|
ConnectionInterface $connection, |
63
|
|
|
ProductRelationRepositoryInterface $productRelationRepository, |
64
|
|
|
ActionInterface $productRelationAction |
65
|
|
|
) { |
66
|
|
|
$this->setConnection($connection); |
67
|
|
|
$this->setProductRelationRepository($productRelationRepository); |
68
|
|
|
$this->setProductRelationAction($productRelationAction); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set's the passed connection. |
73
|
|
|
* |
74
|
|
|
* @param \TechDivision\Import\Dbal\Connection\ConnectionInterface $connection The connection to set |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function setConnection(ConnectionInterface $connection) |
79
|
|
|
{ |
80
|
|
|
$this->connection = $connection; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return's the connection. |
85
|
|
|
* |
86
|
|
|
* @return \TechDivision\Import\Dbal\Connection\ConnectionInterface The connection instance |
87
|
|
|
*/ |
88
|
|
|
public function getConnection() |
89
|
|
|
{ |
90
|
|
|
return $this->connection; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO |
95
|
|
|
* object instance are not committed until you end the transaction by calling ProductProcessor::commit(). |
96
|
|
|
* Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection |
97
|
|
|
* to autocommit mode. |
98
|
|
|
* |
99
|
|
|
* @return boolean Returns TRUE on success or FALSE on failure |
100
|
|
|
* @link http://php.net/manual/en/pdo.begintransaction.php |
101
|
|
|
*/ |
102
|
|
|
public function beginTransaction() |
103
|
|
|
{ |
104
|
|
|
return $this->connection->beginTransaction(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Commits a transaction, returning the database connection to autocommit mode until the next call to |
109
|
|
|
* ProductProcessor::beginTransaction() starts a new transaction. |
110
|
|
|
* |
111
|
|
|
* @return boolean Returns TRUE on success or FALSE on failure |
112
|
|
|
* @link http://php.net/manual/en/pdo.commit.php |
113
|
|
|
*/ |
114
|
|
|
public function commit() |
115
|
|
|
{ |
116
|
|
|
return $this->connection->commit(); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction(). |
121
|
|
|
* |
122
|
|
|
* If the database was set to autocommit mode, this function will restore autocommit mode after it has |
123
|
|
|
* rolled back the transaction. |
124
|
|
|
* |
125
|
|
|
* Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition |
126
|
|
|
* language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit |
127
|
|
|
* COMMIT will prevent you from rolling back any other changes within the transaction boundary. |
128
|
|
|
* |
129
|
|
|
* @return boolean Returns TRUE on success or FALSE on failure |
130
|
|
|
* @link http://php.net/manual/en/pdo.rollback.php |
131
|
|
|
*/ |
132
|
|
|
public function rollBack() |
133
|
|
|
{ |
134
|
|
|
return $this->connection->rollBack(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set's the repository to access product relations. |
139
|
|
|
* |
140
|
|
|
* @param \TechDivision\Import\Product\Repositories\ProductRelationRepositoryInterface $productRelationRepository The repository instance |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function setProductRelationRepository(ProductRelationRepositoryInterface $productRelationRepository) |
145
|
|
|
{ |
146
|
|
|
$this->productRelationRepository = $productRelationRepository; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Return's the repository to access product relations. |
151
|
|
|
* |
152
|
|
|
* @return \TechDivision\Import\Product\Repositories\ProductRelationRepositoryInterface The repository instance |
153
|
|
|
*/ |
154
|
|
|
public function getProductRelationRepository() |
155
|
|
|
{ |
156
|
|
|
return $this->productRelationRepository; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set's the action with the product relation CRUD methods. |
161
|
|
|
* |
162
|
|
|
* @param \TechDivision\Import\Dbal\Actions\ActionInterface $productRelationAction The action with the product relation CRUD methods |
163
|
|
|
* |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
public function setProductRelationAction(ActionInterface $productRelationAction) |
167
|
|
|
{ |
168
|
|
|
$this->productRelationAction = $productRelationAction; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Return's the action with the product relation CRUD methods. |
173
|
|
|
* |
174
|
|
|
* @return \TechDivision\Import\Dbal\Actions\ActionInterface The action instance |
175
|
|
|
*/ |
176
|
|
|
public function getProductRelationAction() |
177
|
|
|
{ |
178
|
|
|
return $this->productRelationAction; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Load's the product relation with the passed parent/child ID. |
183
|
|
|
* |
184
|
|
|
* @param integer $parentId The entity ID of the product relation's parent product |
185
|
|
|
* @param integer $childId The entity ID of the product relation's child product |
186
|
|
|
* |
187
|
|
|
* @return array The product relation |
188
|
|
|
*/ |
189
|
|
|
public function loadProductRelation($parentId, $childId) |
190
|
|
|
{ |
191
|
|
|
return $this->getProductRelationRepository()->findOneByParentIdAndChildId($parentId, $childId); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Persist's the passed product relation data and return's the ID. |
196
|
|
|
* |
197
|
|
|
* @param array $productRelation The product relation data to persist |
198
|
|
|
* @param string|null $name The name of the prepared statement that has to be executed |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
public function persistProductRelation($productRelation, $name = null) |
203
|
|
|
{ |
204
|
|
|
return $this->getProductRelationAction()->persist($productRelation, $name); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Deletes the passed product relation data. |
209
|
|
|
* |
210
|
|
|
* @param array $row The product relation to be deleted |
211
|
|
|
* @param string|null $name The name of the prepared statement that has to be executed |
212
|
|
|
* |
213
|
|
|
* @return void |
214
|
|
|
*/ |
215
|
|
|
public function deleteProductRelation(array $row, string $name = null): void |
216
|
|
|
{ |
217
|
|
|
$this->getProductRelationAction()->delete($row, $name); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|