Completed
Pull Request — master (#4)
by Tim
02:29
created

AttributeOptionExportObserver::process()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 2
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\AttributeOptionExportObserver
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-attribute
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Attribute\Observers;
22
23
use TechDivision\Import\Attribute\Utils\ColumnKeys;
24
25
/**
26
 * Observer that exports the attribute options to an additional CSV file for further processing.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import-attribute
32
 * @link      http://www.techdivision.com
33
 */
34
class AttributeOptionExportObserver extends AbstractAttributeImportObserver
35
{
36
37
    /**
38
     * The artefact type.
39
     *
40
     * @var string
41
     */
42
    const ARTEFACT_TYPE = 'option-import';
43
44
    /**
45
     * Process the observer's business logic.
46
     *
47
     * @return void
48
     */
49
    protected function process()
50
    {
51
52
        // initialize the array with the artefacts
53
        $artefacts = array();
54
55
        // load the attribute option values
56
        $attributeOptionValues = $this->getValue(ColumnKeys::ATTRIBUTE_OPTION_VALUES, array(), array($this, 'explode'));
57
        $attributeOptionPositions = $this->getValue(ColumnKeys::ATTRIBUTE_OPTION_POSITIONS, array(), array($this, 'explode'));
58
59
        // iterate over the attribute option values and export them
60
        foreach ($attributeOptionValues as $key => $attributeOptionValue) {
61
            // initialize and add the new artefact
62
            $artefacts[] = $this->newArtefact(
63
                array(
64
                    ColumnKeys::STORE_VIEW_CODE => $this->getValue(ColumnKeys::STORE_VIEW_CODE),
65
                    ColumnKeys::ATTRIBUTE_CODE  => $this->getValue(ColumnKeys::ATTRIBUTE_CODE),
66
                    ColumnKeys::VALUE           => $attributeOptionValue,
67
                    ColumnKeys::POSITION        => isset($attributeOptionPositions[$key]) ? $attributeOptionPositions[$key] : 0
68
                ),
69
                array(
70
                    ColumnKeys::STORE_VIEW_CODE => ColumnKeys::STORE_VIEW_CODE,
71
                    ColumnKeys::ATTRIBUTE_CODE  => ColumnKeys::ATTRIBUTE_CODE,
72
                    ColumnKeys::VALUE           => ColumnKeys::VALUE,
73
                    ColumnKeys::POSITION        => ColumnKeys::POSITION
74
                )
75
            );
76
        }
77
78
        // add the array with the artefacts
79
        $this->addArtefacts($artefacts);
80
    }
81
82
    /**
83
     * Create's and return's a new empty artefact entity.
84
     *
85
     * @param array $columns             The array with the column data
86
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
87
     *
88
     * @return array The new artefact entity
89
     */
90
    protected function newArtefact(array $columns, array $originalColumnNames)
91
    {
92
        return $this->getSubject()->newArtefact($columns, $originalColumnNames);
93
    }
94
95
    /**
96
     * Add the passed product type artefacts to the product with the
97
     * last entity ID.
98
     *
99
     * @param array $artefacts The product type artefacts
100
     *
101
     * @return void
102
     * @uses \TechDivision\Import\Product\Bundle\Subjects\BunchSubject::getLastEntityId()
103
     */
104
    protected function addArtefacts(array $artefacts)
105
    {
106
        $this->getSubject()->addArtefacts(AttributeOptionExportObserver::ARTEFACT_TYPE, $artefacts);
107
    }
108
}
109