Completed
Pull Request — master (#16)
by Tim
07:10
created

AttributeOptionSwatchExportObserver::process()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 57
Code Lines 29

Duplication

Lines 6
Ratio 10.53 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 6
loc 57
rs 7.0745
c 1
b 0
f 0
cc 9
eloc 29
nc 6
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * TechDivision\Import\Attribute\Observers\AttributeOptionValueExportObserver
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 option swatch values 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 AttributeOptionSwatchExportObserver extends AbstractAttributeExportObserver
35
{
36
37
    /**
38
     * The artefact type.
39
     *
40
     * @var string
41
     */
42
    const ARTEFACT_TYPE = 'option-swatch-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
        // prepare the store view code
58
        $this->prepareStoreViewCode();
0 ignored issues
show
Deprecated Code introduced by
The method TechDivision\Import\Obse...:prepareStoreViewCode() has been deprecated with message: Will be removed with version 1.0.0, use subject method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
59
60
        // initialize the array with the artefacts
61
        $artefacts = array();
62
63
        // load the attribute option values for the custom store views
64
        $attributeOptionValues = $this->getValue(ColumnKeys::ATTRIBUTE_OPTION_VALUES, array(), array($this, 'explode'));
65
        $attributeOptionSwatch = $this->explode($this->getValue(ColumnKeys::ATTRIBUTE_OPTION_SWATCH), $this->getMultipleValueDelimiter());
0 ignored issues
show
Deprecated Code introduced by
The method TechDivision\Import\Obse...ultipleValueDelimiter() has been deprecated with message: Will be removed with version 1.0.0, use subject method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
Deprecated Code introduced by
The method TechDivision\Import\Obse...ractObserver::explode() has been deprecated with message: Will be removed with version 1.0.0, use subject method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
66
67
        // iterate over the attribute option values and export them
68
        foreach ($attributeOptionValues as $key => $attributeOptionValue) {
69
            // load the artefacts with the admin store values
70
            $adminValueArtefacts = $this->getArtefactsByTypeAndEntityId(AttributeOptionExportObserver::ARTEFACT_TYPE, $this->getLastEntityId());
0 ignored issues
show
Deprecated Code introduced by
The method TechDivision\Import\Obse...rver::getLastEntityId() has been deprecated with message: Will be removed with version 1.0.0, use subject method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
71
72
            // initialize the attribute option swatch data
73
            $optionSwatch = array();
74
            if (isset($attributeOptionSwatch[$key])) {
75
                // prepare the EAV attribute option swatch values
76 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...
Deprecated Code introduced by
The method TechDivision\Import\Obse...ractObserver::explode() has been deprecated with message: Will be removed with version 1.0.0, use subject method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
77
                    $explodedSwatch = $this->explode($value, '=');
0 ignored issues
show
Deprecated Code introduced by
The method TechDivision\Import\Obse...ractObserver::explode() has been deprecated with message: Will be removed with version 1.0.0, use subject method instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

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