Passed
Pull Request — develop (#1)
by Andreas
02:52
created

CatalogMapper::getCatalog()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 45
ccs 21
cts 21
cp 1
rs 8.439
cc 5
eloc 24
nc 12
nop 1
crap 5
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];
0 ignored issues
show
Unused Code introduced by
The property $mandatoryFields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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
}