|
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
|
|
|
public function __construct(ProductMapper $productMapper) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->productMapper = $productMapper; |
|
38
|
|
|
} |
|
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
|
|
|
public function getCatalog(array $catalogData) |
|
50
|
|
|
{ |
|
51
|
|
|
/** @var array $skuIndex A list of all SKUs */ |
|
52
|
|
|
$skuIndex = []; |
|
53
|
|
|
|
|
54
|
|
|
/** @var array $slugIndex A list of all product slugs */ |
|
55
|
|
|
$slugIndex = []; |
|
56
|
|
|
|
|
57
|
|
|
$index = 1; |
|
58
|
|
|
$products = []; |
|
59
|
|
|
foreach ($catalogData as $catalogItem) { |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
|
|
// convert the product data into a Product model |
|
63
|
|
|
$product = $this->productMapper->getProduct($catalogItem); |
|
64
|
|
|
|
|
65
|
|
|
// check for duplicate SKUs |
|
66
|
|
|
$sku = strtolower($product->getSku()->__toString()); |
|
67
|
|
|
if (array_key_exists($sku, $skuIndex)) { |
|
68
|
|
|
throw new CatalogException(sprintf("Cannot add a second product with the SKU '%s' to the catalog", |
|
69
|
|
|
$sku)); |
|
70
|
|
|
} |
|
71
|
|
|
$skuIndex[$sku] = 1; |
|
72
|
|
|
|
|
73
|
|
|
// check for duplicate Slugs |
|
74
|
|
|
$slug = strtolower($product->getSlug()->__toString()); |
|
75
|
|
|
if (array_key_exists($slug, $slugIndex)) { |
|
76
|
|
|
throw new CatalogException(sprintf("Cannot add a second product with the Slug '%s' to the catalog", |
|
77
|
|
|
$slug)); |
|
78
|
|
|
} |
|
79
|
|
|
$slugIndex[$slug] = 1; |
|
80
|
|
|
|
|
81
|
|
|
// add the product to the catalog |
|
82
|
|
|
$products[] = $product; |
|
83
|
|
|
|
|
84
|
|
|
} catch (\Exception $productException) { |
|
85
|
|
|
throw new CatalogException(sprintf("Cannot convert catalog item %s into a product: %s", $index, |
|
86
|
|
|
$productException->getMessage()), $productException); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$index++; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return new Catalog($products); |
|
93
|
|
|
} |
|
94
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.