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

ProductMapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
namespace Wambo\Catalog\Mapper;
3
4
use Wambo\Catalog\Error\ProductException;
5
use Wambo\Catalog\Model\Content;
6
use Wambo\Catalog\Model\Product;
7
use Wambo\Catalog\Model\SKU;
8
use Wambo\Catalog\Model\Slug;
9
use Wambo\Catalog\Validation\SKUValidator;
10
use Wambo\Catalog\Validation\SlugValidator;
11
12
/**
13
 * Class ProductMapper creates \Wambo\Model\Product models from data bags with product data.
14
 *
15
 * @package Wambo\Mapper
16
 */
17
class ProductMapper
18
{
19
    const FIELD_SKU = "sku";
20
    const FIELD_SLUG = "slug";
21
    const FIELD_TITLE = "title";
22
23
    /**
24
     * @var array $mandatoryFields A list of all mandatory fields of a Product
25
     */
26
    private $mandatoryFields = [self::FIELD_SKU, self::FIELD_SLUG, self::FIELD_TITLE];
27
28
    /**
29
     * @var ContentMapper
30
     */
31
    private $contentMapper;
32
33
    /**
34
     * Creates a new ProductMapper instance
35
     *
36
     * @param ContentMapper $contentMapper A class for mapping product content
37
     */
38 10
    public function __construct(ContentMapper $contentMapper)
39
    {
40 10
        $this->contentMapper = $contentMapper;
41 10
    }
42
43
    /**
44
     * Get a Catalog model from an array of unstructured product data
45
     *
46
     * @param array $productData An array containing product attributes
47
     *
48
     * @return Product
49
     *
50
     * @throws ProductException If a mandatory field is missing
51
     * @throws ProductException If the no Product could be created from the given product data
52
     */
53 10
    public function getProduct(array $productData)
54
    {
55
        // check if all mandatory fields are present
56 10
        foreach ($this->mandatoryFields as $mandatoryField) {
57 10
            if (!array_key_exists($mandatoryField, $productData)) {
58 10
                throw new ProductException("The field '$mandatoryField' is missing in the given product data");
59
            }
60
        }
61
62
        // try to create a product from the available data
63
        try {
64
65
            // sku
66 5
            $sku = new SKU($productData[self::FIELD_SKU]);
67
68
            // slug
69 4
            $slug = new Slug($productData[self::FIELD_SLUG]);
70
71
            // product title
72 3
            $title = $productData[self::FIELD_TITLE];
73
74
            // product content
75 3
            $content = $this->contentMapper->getContent($productData);
76
77 3
            $product = new Product($sku, $slug, $title, $content);
78 3
            return $product;
79
80 2
        } catch (\Exception $productException) {
81 2
            throw new ProductException(sprintf("Failed to create a product from the given data: %s",
82 2
                $productException->getMessage()), $productException);
83
        }
84
    }
85
}