Completed
Push — master ( 557405...0c2dcd )
by Tim
10s
created

ProductBundleObserver::newArtefact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Bundle\Observers\ProductBundleObserver
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-bundle
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Bundle\Observers;
22
23
use TechDivision\Import\Utils\ProductTypes;
24
use TechDivision\Import\Product\Bundle\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-bundle
34
 * @link      http://www.techdivision.com
35
 */
36
class ProductBundleObserver extends AbstractProductImportObserver
37
{
38
39
    /**
40
     * The artefact type.
41
     *
42
     * @var string
43
     */
44
    const ARTEFACT_TYPE = 'bundles';
45
46
    /**
47
     *
48
     * @var array
49
     */
50
    protected $columns = array(
51
        'name'        => ColumnKeys::BUNDLE_VALUE_NAME,
52
        'type'        => ColumnKeys::BUNDLE_VALUE_TYPE,
53
        'required'    => ColumnKeys::BUNDLE_VALUE_REQUIRED,
54
        'sku'         => ColumnKeys::BUNDLE_VALUE_SKU,
55
        'price'       => ColumnKeys::BUNDLE_VALUE_PRICE,
56
        'default'     => ColumnKeys::BUNDLE_VALUE_DEFAULT,
57
        'default_qty' => ColumnKeys::BUNDLE_VALUE_DEFAULT_QTY,
58
        'price_type'  => ColumnKeys::BUNDLE_VALUE_PRICE_TYPE
59
    );
60
61
    /**
62
     * Process the observer's business logic.
63
     *
64
     * @return array The processed row
65
     */
66
    protected function process()
67
    {
68
69
        // query whether or not we've found a bundle product
70
        if ($this->getValue(ColumnKeys::PRODUCT_TYPE) !== ProductTypes::BUNDLE) {
71
            return;
72
        }
73
74
        // query whether or not, we've a bundle
75
        if ($bundleValues = $this->getValue(ColumnKeys::BUNDLE_VALUES)) {
76
            // initialize the array for the product bundles
77
            $artefacts = array();
78
79
            // load the parent SKU from the row
80
            $parentSku = $this->getValue(ColumnKeys::SKU);
81
82
            // initialize the bundle with the found values
83
            foreach ($this->explode($bundleValues, '|') as $bundleValue) {
84
                // initialize the product bundle itself
85
                $bundle = $this->newArtefact(
86
                    array(
87
                        ColumnKeys::BUNDLE_PARENT_SKU    => $parentSku,
88
                        ColumnKeys::STORE_VIEW_CODE      => $this->getValue(ColumnKeys::STORE_VIEW_CODE),
89
                        ColumnKeys::BUNDLE_SKU_TYPE      => $this->getValue(ColumnKeys::BUNDLE_SKU_TYPE),
90
                        ColumnKeys::BUNDLE_PRICE_TYPE    => $this->getValue(ColumnKeys::BUNDLE_PRICE_TYPE),
91
                        ColumnKeys::BUNDLE_PRICE_VIEW    => $this->getValue(ColumnKeys::BUNDLE_PRICE_VIEW),
92
                        ColumnKeys::BUNDLE_WEIGHT_TYPE   => $this->getValue(ColumnKeys::BUNDLE_WEIGHT_TYPE),
93
                        ColumnKeys::BUNDLE_SHIPMENT_TYPE => $this->getValue(ColumnKeys::BUNDLE_SHIPMENT_TYPE),
94
                    ),
95
                    array(
96
                        ColumnKeys::BUNDLE_PARENT_SKU        => ColumnKeys::SKU,
97
                        ColumnKeys::STORE_VIEW_CODE          => ColumnKeys::STORE_VIEW_CODE,
98
                        ColumnKeys::BUNDLE_SKU_TYPE          => ColumnKeys::BUNDLE_SKU_TYPE,
99
                        ColumnKeys::BUNDLE_PRICE_TYPE        => ColumnKeys::BUNDLE_PRICE_TYPE,
100
                        ColumnKeys::BUNDLE_PRICE_VIEW        => ColumnKeys::BUNDLE_PRICE_VIEW,
101
                        ColumnKeys::BUNDLE_WEIGHT_TYPE       => ColumnKeys::BUNDLE_WEIGHT_TYPE,
102
                        ColumnKeys::BUNDLE_SHIPMENT_TYPE     => ColumnKeys::BUNDLE_SHIPMENT_TYPE,
103
                        ColumnKeys::BUNDLE_VALUE_NAME        => ColumnKeys::BUNDLE_VALUES,
104
                        ColumnKeys::BUNDLE_VALUE_TYPE        => ColumnKeys::BUNDLE_VALUES,
105
                        ColumnKeys::BUNDLE_VALUE_REQUIRED    => ColumnKeys::BUNDLE_VALUES,
106
                        ColumnKeys::BUNDLE_VALUE_SKU         => ColumnKeys::BUNDLE_VALUES,
107
                        ColumnKeys::BUNDLE_VALUE_PRICE       => ColumnKeys::BUNDLE_VALUES,
108
                        ColumnKeys::BUNDLE_VALUE_DEFAULT     => ColumnKeys::BUNDLE_VALUES,
109
                        ColumnKeys::BUNDLE_VALUE_DEFAULT_QTY => ColumnKeys::BUNDLE_VALUES,
110
                        ColumnKeys::BUNDLE_VALUE_PRICE_TYPE  => ColumnKeys::BUNDLE_VALUES
111
                    )
112
                );
113
114
                // initialize the columns
115
                foreach ($this->columns as $columnKey) {
116
                    $bundle[$columnKey] = null;
117
                }
118
119
                // set the values
120
                $values = array();
121
                foreach (explode(',', $bundleValue) as $values) {
122
                    list ($key, $value) = explode('=', $values);
123
                    $bundle[$this->columns[$key]] = $value;
124
                }
125
126
                // prepare and append the bundle data
127
                $artefacts[] = $bundle;
128
            }
129
130
            // append the bundles to the subject
131
            $this->addArtefacts($artefacts);
132
        }
133
    }
134
135
    /**
136
     * Create's and return's a new empty artefact entity.
137
     *
138
     * @param array $columns             The array with the column data
139
     * @param array $originalColumnNames The array with a mapping from the old to the new column names
140
     *
141
     * @return array The new artefact entity
142
     */
143
    protected function newArtefact(array $columns, array $originalColumnNames)
144
    {
145
        return $this->getSubject()->newArtefact($columns, $originalColumnNames);
146
    }
147
148
    /**
149
     * Add the passed product type artefacts to the product with the
150
     * last entity ID.
151
     *
152
     * @param array $artefacts The product type artefacts
153
     *
154
     * @return void
155
     * @uses \TechDivision\Import\Product\Bundle\Subjects\BunchSubject::getLastEntityId()
156
     */
157
    protected function addArtefacts(array $artefacts)
158
    {
159
        $this->getSubject()->addArtefacts(ProductBundleObserver::ARTEFACT_TYPE, $artefacts);
160
    }
161
}
162