Completed
Push — master ( a9b7ce...a1aea0 )
by Tim
11s
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
     * Will be invoked by the action on the events the listener has been registered for.
48
     *
49
     * @param array $row The row to handle
50
     *
51
     * @return array The modified row
52
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
53
     */
54
    public function handle(array $row)
55
    {
56
57
        // initialize the row
58
        $this->setRow($row);
0 ignored issues
show
Bug introduced by
The method setRow() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
59
60
        // process the functionality and return the row
61
        $this->process();
62
63
        // return the processed row
64
        return $this->getRow();
0 ignored issues
show
Bug introduced by
The method getRow() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
65
    }
66
67
    /**
68
     * Process the observer's business logic.
69
     *
70
     * @return array The processed row
71
     */
72
    protected function process()
73
    {
74
75
        // query whether or not we've found a configurable product
76
        if ($this->getValue(ColumnKeys::PRODUCT_TYPE) !== ProductTypes::CONFIGURABLE) {
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
77
            return;
78
        }
79
80
        // query whether or not, we've configurables
81
        if ($configurableVariations = $this->getValue(ColumnKeys::CONFIGURABLE_VARIATIONS)) {
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
82
            // load the variation labels, if available
83
            $configurableVariationLabels = $this->getValue(ColumnKeys::CONFIGURABLE_VARIATION_LABELS);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
84
85
            // create an array with the variation labels (attribute code as key)
86
            $varLabels = array();
87
            foreach ($this->explode($configurableVariationLabels, '|') as $variationLabel) {
0 ignored issues
show
Bug introduced by
The method explode() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
88
                if (strstr($variationLabel, '=')) {
89
                    list ($key, $value) = $this->explode($variationLabel, '=');
0 ignored issues
show
Bug introduced by
The method explode() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
90
                    $varLabels[$key] = $value;
91
                }
92
            }
93
94
            // intialize the array for the variations
95
            $artefacts = array();
96
97
            // load the parent SKU from the row
98
            $parentSku = $this->getValue(ColumnKeys::SKU);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
99
100
            // load the store view code
101
            $storeViewCode = $this->getValue(ColumnKeys::STORE_VIEW_CODE);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
102
103
            // iterate over all variations and import them
104
            foreach ($this->explode($configurableVariations, '|') as $variation) {
0 ignored issues
show
Bug introduced by
The method explode() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
105
                // sku=Configurable Product 48-option 2,configurable_variation=option 2
106
                list ($sku, $option) = $this->explode($variation);
0 ignored issues
show
Bug introduced by
The method explode() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
107
108
                // explode the variations child ID as well as option code and value
109
                list (, $childSku) = $this->explode($sku, '=');
0 ignored issues
show
Bug introduced by
The method explode() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
110
                list ($optionCode, $optionValue) = $this->explode($option, '=');
0 ignored issues
show
Bug introduced by
The method explode() does not seem to exist on object<TechDivision\Impo...ProductVariantObserver>.

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...
111
112
                // load the apropriate variation label
113
                $varLabel = '';
114
                if (isset($varLabels[$optionCode])) {
115
                    $varLabel = $varLabels[$optionCode];
116
                }
117
118
                // append the product variation
119
                $artefacts[] = array(
120
                    ColumnKeys::STORE_VIEW_CODE         => $storeViewCode,
121
                    ColumnKeys::VARIANT_PARENT_SKU      => $parentSku,
122
                    ColumnKeys::VARIANT_CHILD_SKU       => $childSku,
123
                    ColumnKeys::VARIANT_OPTION_VALUE    => $optionValue,
124
                    ColumnKeys::VARIANT_VARIATION_LABEL => $varLabel
125
                );
126
            }
127
128
            // append the variations to the subject
129
            $this->addArtefacts($artefacts);
130
        }
131
    }
132
133
    /**
134
     * Add the passed product type artefacts to the product with the
135
     * last entity ID.
136
     *
137
     * @param array $artefacts The product type artefacts
138
     *
139
     * @return void
140
     * @uses \TechDivision\Import\Product\Variant\Subjects\BunchSubject::getLastEntityId()
141
     */
142
    protected function addArtefacts(array $artefacts)
143
    {
144
        $this->getSubject()->addArtefacts(ProductVariantObserver::ARTEFACT_TYPE, $artefacts);
145
    }
146
}
147