ProductGroupedObserver::process()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 40
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 40
rs 9.7333
cc 4
nc 4
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Grouped\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 2019 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-grouped
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Grouped\Observers;
22
23
use TechDivision\Import\Product\Link\Utils\ProductTypes;
24
use TechDivision\Import\Product\Grouped\Utils\ColumnKeys;
25
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
26
27
/**
28
 * The observer that exports the data that is necessary to create the grouped products to a separate CSV file.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2019 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-grouped
34
 * @link      http://www.techdivision.com
35
 */
36
class ProductGroupedObserver extends AbstractProductImportObserver
37
{
38
39
    /**
40
     * The artefact type.
41
     *
42
     * @var string
43
     */
44
    const ARTEFACT_TYPE = 'grouped';
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::GROUPED) {
56
            return;
57
        }
58
59
        // query whether or not, we've associated SKUs
60
        if ($associatedSkus = $this->getValue(ColumnKeys::ASSOCIATED_SKUS)) {
61
            // intialize the array for the grouped products
62
            $artefacts = array();
63
64
            // load the parent SKU from the row
65
            $parentSku = $this->getValue(ColumnKeys::SKU);
66
67
            // iterate over all associated SKUs and import them, e. g. the complete value will look like
68
            // 24-MB01=0.0000,24-MB04=0.0000,24-MB03=0.0000
69
            foreach ($this->explode($associatedSkus) as $grouped) {
70
                // explode the SKU and the configurable attribute values, e. g. 24-MB04=0.0000
71
                list ($childSku, ) = $this->explode($grouped, '=');
72
73
                // initialize the product variation itself
74
                $variation = $this->newArtefact(
75
                    array(
76
                        ColumnKeys::GROUPED_PARENT_SKU => $parentSku,
77
                        ColumnKeys::GROUPED_CHILD_SKU  => $childSku
78
                    ),
79
                    array(
80
                        ColumnKeys::GROUPED_PARENT_SKU => ColumnKeys::SKU,
81
                        ColumnKeys::GROUPED_CHILD_SKU  => ColumnKeys::ASSOCIATED_SKUS
82
                    )
83
                );
84
85
                // append the product variation
86
                $artefacts[] = $variation;
87
            }
88
89
            // append the variations to the subject
90
            $this->addArtefacts($artefacts);
91
        }
92
    }
93
94
    /**
95
     * Create's and return's a new empty artefact entity.
96
     *
97
     * @param array $columns             The array with the column data
98
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
99
     *
100
     * @return array The new artefact entity
101
     */
102
    protected function newArtefact(array $columns, array $originalColumnNames)
103
    {
104
        return $this->getSubject()->newArtefact($columns, $originalColumnNames);
105
    }
106
107
    /**
108
     * Add the passed product type artefacts to the product with the
109
     * last entity ID.
110
     *
111
     * @param array $artefacts The product type artefacts
112
     *
113
     * @return void
114
     * @uses \TechDivision\Import\Product\Variant\Subjects\BunchSubject::getLastEntityId()
115
     */
116
    protected function addArtefacts(array $artefacts)
117
    {
118
        $this->getSubject()->addArtefacts(ProductGroupedObserver::ARTEFACT_TYPE, $artefacts);
119
    }
120
}
121