1
|
|
|
<?php |
2
|
|
|
namespace Wambo\Catalog\Mapper; |
3
|
|
|
|
4
|
|
|
use Wambo\Catalog\Error\CatalogException; |
5
|
|
|
use Wambo\Catalog\Model\Catalog; |
6
|
|
|
use Wambo\Catalog\Model\Product; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class CatalogMapper creates \Wambo\Model\Product models from catalog data |
10
|
|
|
* |
11
|
|
|
* @package Wambo\Mapper |
12
|
|
|
*/ |
13
|
|
|
class CatalogMapper |
14
|
|
|
{ |
15
|
|
|
const FIELD_SKU = "sku"; |
16
|
|
|
const FIELD_TITLE = "title"; |
17
|
|
|
const FIELD_SUMMARY = "summary"; |
18
|
|
|
const FIELD_SLUG = "slug"; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array $mandatoryFields A list of all mandatory fields of a Product |
22
|
|
|
*/ |
23
|
|
|
private $mandatoryFields = [self::FIELD_SKU, self::FIELD_TITLE, self::FIELD_SUMMARY, self::FIELD_SLUG]; |
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ProductMapper |
26
|
|
|
*/ |
27
|
|
|
private $productMapper; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Creates a new instance of the CatalogMapper class. |
31
|
|
|
* |
32
|
|
|
* @param ProductMapper $productMapper A ProductMapper instance for converting unstructured product data to Product |
33
|
|
|
* models |
34
|
|
|
*/ |
35
|
13 |
|
public function __construct(ProductMapper $productMapper) |
36
|
|
|
{ |
37
|
13 |
|
$this->productMapper = $productMapper; |
38
|
13 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get a Catalog model from the an array of catalog data |
42
|
|
|
* |
43
|
|
|
* @param array $catalogData An array containing a product catalog |
44
|
|
|
* |
45
|
|
|
* @return Catalog |
46
|
|
|
* |
47
|
|
|
* @throws CatalogException If the catalog cannot be created |
48
|
|
|
*/ |
49
|
9 |
|
public function getCatalog(array $catalogData) |
50
|
|
|
{ |
51
|
|
|
/** @var array $skuIndex A list of all SKUs */ |
52
|
9 |
|
$skuIndex = []; |
53
|
|
|
|
54
|
|
|
/** @var array $slugIndex A list of all product slugs */ |
55
|
9 |
|
$slugIndex = []; |
56
|
|
|
|
57
|
9 |
|
$index = 1; |
58
|
9 |
|
$products = []; |
59
|
9 |
|
foreach ($catalogData as $catalogItem) { |
60
|
|
|
|
61
|
|
|
try { |
62
|
|
|
// convert the product data into a Product model |
63
|
8 |
|
$product = $this->productMapper->getProduct($catalogItem); |
64
|
|
|
|
65
|
|
|
// check for duplicate SKUs |
66
|
7 |
|
$sku = strtolower($product->getSku()->__toString()); |
67
|
7 |
|
if (array_key_exists($sku, $skuIndex)) { |
68
|
2 |
|
throw new CatalogException(sprintf("Cannot add a second product with the SKU '%s' to the catalog", |
69
|
|
|
$sku)); |
70
|
|
|
} |
71
|
7 |
|
$skuIndex[$sku] = 1; |
72
|
|
|
|
73
|
|
|
// check for duplicate Slugs |
74
|
7 |
|
$slug = strtolower($product->getSlug()->__toString()); |
75
|
7 |
|
if (array_key_exists($slug, $slugIndex)) { |
76
|
2 |
|
throw new CatalogException(sprintf("Cannot add a second product with the Slug '%s' to the catalog", |
77
|
|
|
$slug)); |
78
|
|
|
} |
79
|
7 |
|
$slugIndex[$slug] = 1; |
80
|
|
|
|
81
|
|
|
// add the product to the catalog |
82
|
7 |
|
$products[] = $product; |
83
|
|
|
|
84
|
5 |
|
} catch (\Exception $productException) { |
85
|
5 |
|
throw new CatalogException(sprintf("Cannot convert catalog item %s into a product: %s", $index, |
86
|
5 |
|
$productException->getMessage()), $productException); |
87
|
|
|
} |
88
|
|
|
|
89
|
7 |
|
$index++; |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
return new Catalog($products); |
93
|
|
|
} |
94
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.