Completed
Push — master ( 929e68...8142ec )
by Tim
10s
created

ProductVariantObserver::process()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 79
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 79
ccs 0
cts 51
cp 0
rs 6.5649
c 0
b 0
f 0
cc 7
eloc 40
nc 11
nop 0
crap 56

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\Product\Variant\Observers\ProductVariantObserver
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-product-variant
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Variant\Observers;
22
23
use TechDivision\Import\Utils\ProductTypes;
24
use TechDivision\Import\Product\Variant\Utils\ColumnKeys;
25
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
26
27
/**
28
 * A SLSB that handles the process to import product bunches.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-product-variant
34
 * @link      http://www.techdivision.com
35
 */
36
class ProductVariantObserver extends AbstractProductImportObserver
37
{
38
39
    /**
40
     * The artefact type.
41
     *
42
     * @var string
43
     */
44
    const ARTEFACT_TYPE = 'variants';
45
46
    /**
47
     * Process the observer's business logic.
48
     *
49
     * @return array The processed row
50
     */
51
    protected function process()
52
    {
53
54
        // query whether or not we've found a configurable product
55
        if ($this->getValue(ColumnKeys::PRODUCT_TYPE) !== ProductTypes::CONFIGURABLE) {
56
            return;
57
        }
58
59
        // query whether or not, we've configurables
60
        if ($configurableVariations = $this->getValue(ColumnKeys::CONFIGURABLE_VARIATIONS)) {
61
            // load the variation labels, if available
62
            $configurableVariationLabels = $this->getValue(ColumnKeys::CONFIGURABLE_VARIATION_LABELS);
63
64
            // create an array with the variation labels (attribute code as key)
65
            $varLabels = array();
66
            foreach ($this->explode($configurableVariationLabels, '|') as $variationLabel) {
67
                if (strstr($variationLabel, '=')) {
68
                    list ($key, $value) = $this->explode($variationLabel, '=');
69
                    $varLabels[$key] = $value;
70
                }
71
            }
72
73
            // intialize the array for the variations
74
            $artefacts = array();
75
76
            // load the parent SKU from the row
77
            $parentSku = $this->getValue(ColumnKeys::SKU);
78
79
            // load the store view code
80
            $storeViewCode = $this->getValue(ColumnKeys::STORE_VIEW_CODE);
81
82
            // load the product's attribute set code
83
            $attributeSetCode = $this->getValue(ColumnKeys::ATTRIBUTE_SET_CODE);
84
85
            // iterate over all variations and import them
86
            foreach ($this->explode($configurableVariations, '|') as $variation) {
87
                // sku=Configurable Product 48-option 2,configurable_variation=option 2
88
                list ($sku, $option) = $this->explode($variation);
89
90
                // explode the variations child ID as well as option code and value
91
                list (, $childSku) = $this->explode($sku, '=');
92
                list ($optionCode, $optionValue) = $this->explode($option, '=');
93
94
                // load the apropriate variation label
95
                $varLabel = '';
96
                if (isset($varLabels[$optionCode])) {
97
                    $varLabel = $varLabels[$optionCode];
98
                }
99
100
                // initialize the product variation itself
101
                $variation = $this->newArtefact(
102
                    array(
103
                        ColumnKeys::STORE_VIEW_CODE         => $storeViewCode,
104
                        ColumnKeys::ATTRIBUTE_SET_CODE      => $attributeSetCode,
105
                        ColumnKeys::VARIANT_PARENT_SKU      => $parentSku,
106
                        ColumnKeys::VARIANT_CHILD_SKU       => $childSku,
107
                        ColumnKeys::VARIANT_ATTRIBUTE_CODE  => $optionCode,
108
                        ColumnKeys::VARIANT_OPTION_VALUE    => $optionValue,
109
                        ColumnKeys::VARIANT_VARIATION_LABEL => $varLabel
110
                    ),
111
                    array(
112
                        ColumnKeys::STORE_VIEW_CODE         => ColumnKeys::STORE_VIEW_CODE,
113
                        ColumnKeys::ATTRIBUTE_SET_CODE      => ColumnKeys::ATTRIBUTE_SET_CODE,
114
                        ColumnKeys::VARIANT_PARENT_SKU      => ColumnKeys::SKU,
115
                        ColumnKeys::VARIANT_CHILD_SKU       => ColumnKeys::CONFIGURABLE_VARIATIONS,
116
                        ColumnKeys::VARIANT_ATTRIBUTE_CODE  => ColumnKeys::CONFIGURABLE_VARIATIONS,
117
                        ColumnKeys::VARIANT_OPTION_VALUE    => ColumnKeys::CONFIGURABLE_VARIATIONS,
118
                        ColumnKeys::VARIANT_VARIATION_LABEL => ColumnKeys::CONFIGURABLE_VARIATION_LABELS
119
                    )
120
                );
121
122
                // append the product variation
123
                $artefacts[] = $variation;
124
            }
125
126
            // append the variations to the subject
127
            $this->addArtefacts($artefacts);
128
        }
129
    }
130
131
    /**
132
     * Create's and return's a new empty artefact entity.
133
     *
134
     * @param array $columns             The array with the column data
135
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
136
     *
137
     * @return array The new artefact entity
138
     */
139
    protected function newArtefact(array $columns, array $originalColumnNames)
140
    {
141
        return $this->getSubject()->newArtefact($columns, $originalColumnNames);
142
    }
143
144
    /**
145
     * Add the passed product type artefacts to the product with the
146
     * last entity ID.
147
     *
148
     * @param array $artefacts The product type artefacts
149
     *
150
     * @return void
151
     * @uses \TechDivision\Import\Product\Variant\Subjects\BunchSubject::getLastEntityId()
152
     */
153
    protected function addArtefacts(array $artefacts)
154
    {
155
        $this->getSubject()->addArtefacts(ProductVariantObserver::ARTEFACT_TYPE, $artefacts);
156
    }
157
}
158