Completed
Pull Request — master (#7)
by Rafael
09:00
created

ConfigCollection::assign()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 2
eloc 19
nc 2
nop 1
1
<?php
2
3
namespace Iris\Mapping;
4
5
class ConfigCollection extends Base
6
{
7
    /**
8
     * @var \Iris\Mapping\Image
9
     */
10
    private $imageMapping;
11
12
    /**
13
     * @var \Iris\Mapping\SimpleCollection
14
     */
15
    private $simpleCollectionMapping;
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function assign(array $externalData)
21
    {
22
        $data = [];
23
        foreach ($externalData as $c) {
24
25
            $data[] = [
26
                'sku'               => $c['sku'],
27
                'name'              => $c['name'],
28
                'description'       => $c['description'],
29
                'brand'             => $c['brand'],
30
                'price'             => $c['price'],
31
                'special_price'     => $c['special_price'],
32
                'special_from_date' => $c['special_from_date'],
33
                'special_to_date'   => $c['special_to_date'],
34
                'attributes'        => $c['attributes'],
35
                'attribute_set'     => $c['attribute_set'],
36
                'images'            => $this->getImageMapping()
37
                ->assign($c['images']),
38
                    'simple_collection' => $this->getSimpleCollectionMapping()
39
                    ->assign($c['simple_collection'])
40
                ];
41
42
        }
43
44
        return new \Iris\Transfer\Catalog\ConfigCollection($data);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     * @param \Iris\Transfer\Catalog\ConfigCollection $internalData
50
     */
51
    public function map($internalData)
52
    {
53
        $externalData = [];
54
        foreach ($internalData as $config) {
55
            $externalData[] = [
56
                'sku'               => $config->getSku(),
57
                'simple_collection' => $this->getSimpleCollectionMapping()
58
                                            ->map($config->getSimpleCollection())
59
            ];
60
        }
61
62
        return $externalData;
63
    }
64
65
    /**
66
     * @return \Iris\Mapping\Image
67
     */
68
    public function getImageMapping()
69
    {
70
        $this->imageMapping || $this->setImageMapping(Image::getInstance());
71
        return $this->imageMapping;
72
    }
73
74
    /**
75
     * @param \Iris\Mapping\Image $map
76
     * @return \Iris\Mapping\ConfigCollection
77
     */
78
    public function setImageMapping(Image $map)
79
    {
80
        $this->imageMapping = $map;
81
        return $this;
82
    }
83
84
    /**
85
     * @return \Iris\Mapping\SimpleCollection
86
     */
87
    public function getSimpleCollectionMapping()
88
    {
89
        $this->simpleCollectionMapping ||
90
            $this->setSimpleCollectionMapping(SimpleCollection::getInstance());
91
92
        return $this->simpleCollectionMapping;
93
    }
94
95
    /**
96
     * @param \Iris\Mapping\SimpleCollection $map
97
     * @return \Iris\Mapping\ConfigCollection
98
     */
99
    public function setSimpleCollectionMapping(SimpleCollection $map)
100
    {
101
        $this->simpleCollectionMapping = $map;
102
        return $this;
103
    }
104
}
105