Completed
Pull Request — master (#7)
by Rafael
03:49
created

ConfigCollection::map()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4286
cc 2
eloc 8
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
        $configCollection = new \Iris\Transfer\Catalog\ConfigCollection();
23
        
24
        foreach ($externalData as $c) {
25
26
            $data[] = new \Iris\Transfer\Catalog\Config([
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
27
                'sku'               => $c['sku'],
28
                'name'              => $c['name'],
29
                'description'       => $c['description'],
30
                'brand'             => $c['brand'],
31
                'price'             => $c['price'],
32
                'special_price'     => $c['special_price'],
33
                'special_from_date' => $c['special_from_date'],
34
                'special_to_date'   => $c['special_to_date'],
35
                'attributes'        => $c['attributes'],
36
                'attribute_set'     => $c['attribute_set'],
37
                'images'            => $this->getImageMapping()
38
                                            ->assign($c['images']),
39
                'simple_collection' => $this->getSimpleCollectionMapping()
40
                                            ->assign($c['simple_collection'])
41
                ]);
42
        }
43
44
        return $configCollection;
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