Completed
Pull Request — master (#5)
by Tim
10:32
created

ProductVariantObserver::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
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
            // iterate over all variations and import them
83
            foreach ($this->explode($configurableVariations, '|') as $variation) {
84
                // sku=Configurable Product 48-option 2,configurable_variation=option 2
85
                list ($sku, $option) = $this->explode($variation);
86
87
                // explode the variations child ID as well as option code and value
88
                list (, $childSku) = $this->explode($sku, '=');
89
                list ($optionCode, $optionValue) = $this->explode($option, '=');
90
91
                // load the apropriate variation label
92
                $varLabel = '';
93
                if (isset($varLabels[$optionCode])) {
94
                    $varLabel = $varLabels[$optionCode];
95
                }
96
97
                // append the product variation
98
                $artefacts[] = array(
99
                    ColumnKeys::STORE_VIEW_CODE         => $storeViewCode,
100
                    ColumnKeys::VARIANT_PARENT_SKU      => $parentSku,
101
                    ColumnKeys::VARIANT_CHILD_SKU       => $childSku,
102
                    ColumnKeys::VARIANT_ATTRIBUTE_CODE  => $optionCode,
103
                    ColumnKeys::VARIANT_OPTION_VALUE    => $optionValue,
104
                    ColumnKeys::VARIANT_VARIATION_LABEL => $varLabel
105
                );
106
            }
107
108
            // append the variations to the subject
109
            $this->addArtefacts($artefacts);
110
        }
111
    }
112
113
    /**
114
     * Add the passed product type artefacts to the product with the
115
     * last entity ID.
116
     *
117
     * @param array $artefacts The product type artefacts
118
     *
119
     * @return void
120
     * @uses \TechDivision\Import\Product\Variant\Subjects\BunchSubject::getLastEntityId()
121
     */
122
    protected function addArtefacts(array $artefacts)
123
    {
124
        $this->getSubject()->addArtefacts(ProductVariantObserver::ARTEFACT_TYPE, $artefacts);
125
    }
126
}
127