Completed
Pull Request — master (#5)
by Tim
02:21
created

AttributeOptionExportObserver::addArtefacts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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 AbstractAttributeExportObserver
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
        // do NOT export the value if we're NOT in the admin store view
53
        if (!$this->isAdminStore()) {
54
            return;
55
        }
56
57
        // initialize the array with the artefacts
58
        $artefacts = array();
59
60
        // load the attribute option values/positions
61
        $attributeOptionValues = $this->getValue(ColumnKeys::ATTRIBUTE_OPTION_VALUES, array(), array($this, 'explode'));
62
        $attributeOptionSortOrder = $this->getValue(ColumnKeys::ATTRIBUTE_OPTION_SORT_ORDER, array(), array($this, 'explode'));
63
        $attributeOptionSwatch = $this->explode($this->getValue(ColumnKeys::ATTRIBUTE_OPTION_SWATCH), $this->getMultipleValueDelimiter());
0 ignored issues
show
Bug introduced by
The method getMultipleValueDelimiter() does not seem to exist on object<TechDivision\Impo...teOptionExportObserver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
65
        // iterate over the attribute option values and export them
66
        foreach ($attributeOptionValues as $key => $attributeOptionValue) {
67
            // initialize the attribute option swatch data
68
            $optionSwatch = array();
69
            if (isset($attributeOptionSwatch[$key])) {
70 View Code Duplication
                foreach ($this->explode($attributeOptionSwatch[$key]) as $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
                    $explodedSwatch = $this->explode($value, '=');
72
                    if (isset($explodedSwatch[0]) && isset($explodedSwatch[1])) {
73
                        $optionSwatch[$explodedSwatch[0]] = $explodedSwatch[1];
74
                    }
75
                }
76
            }
77
78
            // initialize and add the new artefact
79
            $artefacts[] = $this->newArtefact(
80
                array(
81
                    ColumnKeys::ATTRIBUTE_CODE  => $this->getValue(ColumnKeys::ATTRIBUTE_CODE),
82
                    ColumnKeys::VALUE           => $attributeOptionValue,
83
                    ColumnKeys::SORT_ORDER      => isset($attributeOptionSortOrder[$key]) ? $attributeOptionSortOrder[$key] : 0,
84
                    ColumnKeys::SWATCH_TYPE     => isset($optionSwatch[ColumnKeys::TYPE]) ? $optionSwatch[ColumnKeys::TYPE] : null,
85
                    ColumnKeys::SWATCH_VALUE    => isset($optionSwatch[ColumnKeys::VALUE]) ? $optionSwatch[ColumnKeys::VALUE] : null
86
                ),
87
                array(
88
                    ColumnKeys::ATTRIBUTE_CODE  => ColumnKeys::ATTRIBUTE_CODE,
89
                    ColumnKeys::VALUE           => ColumnKeys::VALUE,
90
                    ColumnKeys::SORT_ORDER      => ColumnKeys::ATTRIBUTE_OPTION_SORT_ORDER,
91
                    ColumnKeys::SWATCH_TYPE     => ColumnKeys::ATTRIBUTE_OPTION_SWATCH,
92
                    ColumnKeys::SWATCH_VALUE    => ColumnKeys::ATTRIBUTE_OPTION_SWATCH
93
                )
94
            );
95
        }
96
97
        // add the array with the artefacts
98
        $this->addArtefacts($artefacts);
99
    }
100
101
    /**
102
     * Return's the artefact type used for export.
103
     *
104
     * @return string The artefact type
105
     */
106
    protected function getArtefactType()
107
    {
108
        return AttributeOptionExportObserver::ARTEFACT_TYPE;
109
    }
110
}
111