Completed
Push — master ( 909726...642b7c )
by Tim
9s
created

ProductBundleObserver::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
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
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
     * Will be invoked by the action on the events the listener has been registered for.
63
     *
64
     * @param array $row The row to handle
65
     *
66
     * @return array The modified row
67
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
68
     */
69
    public function handle(array $row)
70
    {
71
72
        // initialize the row
73
        $this->setRow($row);
74
75
        // process the functionality and return the row
76
        $this->process();
77
78
        // return the processed row
79
        return $this->getRow();
80
    }
81
82
    /**
83
     * Process the observer's business logic.
84
     *
85
     * @return array The processed row
86
     */
87
    protected function process()
88
    {
89
90
        // query whether or not we've found a bundle product
91
        if ($this->getValue(ColumnKeys::PRODUCT_TYPE) !== ProductTypes::BUNDLE) {
92
            return;
93
        }
94
95
        // query whether or not, we've a bundle
96
        if ($bundleValues = $this->getValue(ColumnKeys::BUNDLE_VALUES)) {
97
            // initialize the array for the product bundles
98
            $artefacts = array();
99
100
            // load the parent SKU from the row
101
            $parentSku = $this->getValue(ColumnKeys::SKU);
102
103
            // initialize the bundle with the found values
104
            foreach ($this->explode($bundleValues, '|') as $bundleValue) {
105
                // initialize the product bundle itself
106
                $bundle = array(
107
                    ColumnKeys::BUNDLE_PARENT_SKU    => $parentSku,
108
                    ColumnKeys::STORE_VIEW_CODE      => $this->getValue(ColumnKeys::STORE_VIEW_CODE),
109
                    ColumnKeys::BUNDLE_SKU_TYPE      => $this->getValue(ColumnKeys::BUNDLE_SKU_TYPE),
110
                    ColumnKeys::BUNDLE_PRICE_TYPE    => $this->getValue(ColumnKeys::BUNDLE_PRICE_TYPE),
111
                    ColumnKeys::BUNDLE_PRICE_VIEW    => $this->getValue(ColumnKeys::BUNDLE_PRICE_VIEW),
112
                    ColumnKeys::BUNDLE_WEIGHT_TYPE   => $this->getValue(ColumnKeys::BUNDLE_WEIGHT_TYPE),
113
                    ColumnKeys::BUNDLE_SHIPMENT_TYPE => $this->getValue(ColumnKeys::BUNDLE_SHIPMENT_TYPE),
114
                );
115
116
                // initialize the columns
117
                foreach ($this->columns as $columnKey) {
118
                    $bundle[$columnKey] = null;
119
                }
120
121
                // set the values
122
                $values = array();
123
                foreach (explode(',', $bundleValue) as $values) {
124
                    list ($key, $value) = explode('=', $values);
125
                    $bundle[$this->columns[$key]] = $value;
126
                }
127
128
                // prepare and append the bundle data
129
                $artefacts[] = $bundle;
130
            }
131
132
            // append the bundles to the subject
133
            $this->addArtefacts($artefacts);
134
        }
135
    }
136
137
    /**
138
     * Add the passed product type artefacts to the product with the
139
     * last entity ID.
140
     *
141
     * @param array $artefacts The product type artefacts
142
     *
143
     * @return void
144
     * @uses \TechDivision\Import\Product\Bundle\Subjects\BunchSubject::getLastEntityId()
145
     */
146
    protected function addArtefacts(array $artefacts)
147
    {
148
        $this->getSubject()->addArtefacts(ProductBundleObserver::ARTEFACT_TYPE, $artefacts);
149
    }
150
}
151