-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.php
295 lines (221 loc) · 8.37 KB
/
test.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
$arrTest[0] = ['name' => 'test', 'sku' => 'test2'];
$arrTest[1] = ['name' => 'test3', 'sku' => 'test4'];
class ProductImportController
{
$product_feed = fgetcsv("http://www.external-domain.com/product_feed.csv");
$product_price_feed = new price_feed_web_service;
if(!$product_feed) {
// return errors to user
}
if(!(new ProductImport($arrExistingProducts, new ProductFactory))->build($product_feed, $product_price_feed)) {
// return errors to user
}
//return success message to user
}
// this class may well be overkill
// but it saves us from having to instantiate
// the class directly in the product import
class ProductFactory
{
public static function create() {
return new Product;
}
}
class ProductImport
{
private $validationErrors = [];
private $arrParameters = [];
private $counter = 0;
private $sql = '';
private $productFactory;
/**
*
* @var type
*/
private $requiredHeaders = array(
'name',
'sku',
); //headers we expect
public function __construct(array $arrExistingProducts, ProductFactory $productFactory) {
// get array of all products so we can check new ones don't already exist
// before inserting db should also have unique constraint
$this->arrExistingProducts = $arrExistingProducts;
$this->productFactory = $productFactory;
}
public function build(array $arrData, ProductPriceFeed $product_price_feed) {
$price_data = $product_price_feed->get_prices();
// would need to reformat price array so that the product id is the key
if(empty($arrData) || empty($price_data)) {
return false;
}
$this->counter = 0;
$this->sql = "INSERT INTO table (product_id, product_name, price) VALUES (";
foreach($arrData as $key => $arrProduct) {
if(!$this->validateHeaders($arrProduct)) {
return false;
}
// would normally recommend passing this in through the constructor
$this->objProduct = $this->productFactory::create();
if($this->counter > 0) {
$this->sql .= "(";
}
// following methods will build placeholders for query as we loop through products
// coud just use the save method in the product object here and have multiple queries running
// continue for each attribute
$this->validateProductName($arrProduct);
$this->validateProductPrice($arrProduct, $price_data);
$this->validateProductId($arrProduct);
// check for errors
$this->validationFailures = array_merge($this->objProduct->getValidationFailures(), $this->validationErrors);
if(count($this->validationFailures) > 0) {
// can either continue or stop if one fails depending on what you want to do
// add to the errors array so we can get it from the controller later and display back to the user
return false;
}
$this->sql = rtrim($this->sql, ",") . "), ";
$this->counter++;
}
if(!$this->save()) {
// could have more elaborate error messages here from db
$this->validationFailures[] = "Unable to save to database";
return false;
}
return true;
}
private function save() {
// final query
$sql = rtrim(trim($this->sql), ",");
try {
// run query in transaction so we cann rollback later if needs be
$this->db->beginTransaction();
if(!$this->db->save()) {
$this->db->rollback();
return false;
}
$this->db->commit();
} catch(PDOException $exception) {
$this->validationFailures[] = $e->getMessage();
return false;
}
return true;
}
private function validateProductName($arrProduct) {
// use the validation in the product object setters as technically this class shouldnt know about the details of a product. We will also use the getter to get the value back out just in case theres any casting within the getter
if(!$this->objProduct->setName($arrProduct['name'])) {
return false;
}
$this->arrParameters["name_{$this->counter}"] = $this->objProduct->getName();
$this->sql .= ":name_{$this->counter},";
return true;
}
private function validateProductPrice($arrProduct, $price_data) {
// have made the assumption here the product ids between the 2 data sets will match
// assume that the price feed array has been reformatted to have the product_id as the key
if(empty($price_data[$arrProduct['product_id']]) || empty(empty($price_data[$arrProduct['product_id']]['price'])) {
$this->validationErrors[] = "Invalid price data";
return false;
}
if(!$this->objProduct->setPrice($arrProduct['product_price'])) {
return false;
}
$this->arrParameters["price_{$this->counter}"] = $this->objProduct->getPrice();
$this->sql .= ":price_{$this->counter},";
return true;
}
private function validateProductId($arrProduct) {
// check id doesn't already exist
if(!checkIfProductExists($arrProduct['product_id'])) {
return false;
}
if(!$this->objProduct->setProductId($arrProduct['product_id'])) {
return false;
}
$this->arrParameters["id_{$this->counter}"] = $this->objProduct->getProductId();
$this->sql .= ":id_{$this->counter},";
return true;
}
/**
*
* @param type $arrData
* @return boolean
*/
private function validateHeaders ($arrData)
{
$foundHeaders = array_keys ($arrData);
if ( $foundHeaders !== $this->requiredHeaders )
{
$this->validationErrors[] = "Incorrect headers found";
return false;
}
return true;
}
/**
*
* @param type $productName
* @return boolean
*/
private function checkIfProductExists($productId) {
if (isset($this->arrExistingProducts[$productId]))
{
$this->validationErrors[] = 'The product you are trying to create already exists';
return false;
}
return true;
}
}
class Product {
private $name;
private $price;
private $product_id;
private $validationFailures = [];
public function setName($name) {
// each setter should contain validation for the specific attribute
if(!is_string($name) || strlen($name) < 3) {
$this->validationFailures[] = "Name must be at least 3 characters";
return false;
}
$this->name = (string) $name;
return true;
}
public function getName() {
return (string) $this->name;
}
public function getPrice() {
return (float) $this->price;
}
public function getProductId($product_id) {
return (int) $this->product_id;
}
public function setProductId($product_id) {
if(!is_numeric($product_id) || empty($product_id)) {
$this->validationFailures[] = "Invalid product id field";
return false;
}
$this->product_id = (int) $product_id;
return true;
}
public function setPrice($price) {
if(!is_float($price) || empty($price)) {
$this->validationFailures[] = "Invalid price field";
return false;
}
$this->price = (float) $price;
return true;
}
/**
* @return array
*/
public function getValidationFailures(): array
{
return $this->validationFailures;
}
public function save()
{
if (count($this->validationFailures) > 0) {
return false;
}
// continue with save (see question 3)
}
}
?>