Completed
Push — master ( 235157...2175ac )
by Tim
10s
created

BundleSelectionPriceObserver::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\BundleSelectionPriceObserver
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\StoreViewCodes;
24
use TechDivision\Import\Product\Bundle\Utils\ColumnKeys;
25
use TechDivision\Import\Product\Bundle\Utils\MemberNames;
26
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
27
28
/**
29
 * Oberserver that provides functionality for the bundle selection price replace operation.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-product-bundle
35
 * @link      http://www.techdivision.com
36
 */
37
class BundleSelectionPriceObserver extends AbstractProductImportObserver
38
{
39
40
    /**
41
     * Will be invoked by the action on the events the listener has been registered for.
42
     *
43
     * @param array $row The row to handle
44
     *
45
     * @return array The modified row
46
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
47
     */
48
    public function handle(array $row)
49
    {
50
51
        // initialize the row
52
        $this->setRow($row);
0 ignored issues
show
Bug introduced by
The method setRow() does not seem to exist on object<TechDivision\Impo...SelectionPriceObserver>.

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...
53
54
        // process the functionality and return the row
55
        $this->process();
56
57
        // return the processed row
58
        return $this->getRow();
0 ignored issues
show
Bug introduced by
The method getRow() does not seem to exist on object<TechDivision\Impo...SelectionPriceObserver>.

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
61
    /**
62
     * Process the observer's business logic.
63
     *
64
     * @return array The processed row
65
     */
66
    protected function process()
67
    {
68
69
        // prepare the store view code
70
        $this->prepareStoreViewCode($this->getRow());
0 ignored issues
show
Bug introduced by
The method getRow() does not seem to exist on object<TechDivision\Impo...SelectionPriceObserver>.

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...
71
72
        // load the store view code
73
        $storeViewCode = $this->getStoreViewCode(StoreViewCodes::ADMIN);
74
75
        // check if we're in default store
76
        if (!$this->isDefaultStore($storeViewCode)) {
77
            // prepare and initialize the bundle selection price
78
            $productBundleSelectionPrice = $this->initializeBundleSelectionPrice($this->prepareAttributes());
79
80
            // persist the bundle selection price
81
            $this->persistProductBundleSelectionPrice($productBundleSelectionPrice);
82
        }
83
    }
84
85
    /**
86
     * Prepare the attributes of the entity that has to be persisted.
87
     *
88
     * @return array The prepared attributes
89
     */
90
    protected function prepareAttributes()
91
    {
92
93
        // load the store view code
94
        $storeViewCode = $this->getStoreViewCode(StoreViewCodes::ADMIN);
95
96
        // load the store/website ID
97
        $store = $this->getStoreByStoreCode($storeViewCode);
98
        $websiteId = $store[MemberNames::WEBSITE_ID];
99
100
        // load the default values
101
        $selectionPriceType = $this->mapPriceType($this->getValue(ColumnKeys::BUNDLE_VALUE_PRICE_TYPE));
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...SelectionPriceObserver>.

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
        $selectionPriceValue = $this->getValue(ColumnKeys::BUNDLE_VALUE_PRICE);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...SelectionPriceObserver>.

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...
103
104
        // load the selection ID for the child SKU
105
        $selectionId = $this->getChildSkuSelectionMapping($this->getValue(ColumnKeys::BUNDLE_VALUE_SKU));
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...SelectionPriceObserver>.

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...
106
107
        // return the prepared bundle selection price
108
        return $this->initializeEntity(
0 ignored issues
show
Bug introduced by
The method initializeEntity() does not seem to exist on object<TechDivision\Impo...SelectionPriceObserver>.

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...
109
            array(
110
                MemberNames::SELECTION_ID          => $selectionId,
111
                MemberNames::WEBSITE_ID            => $websiteId,
112
                MemberNames::SELECTION_PRICE_TYPE  => $selectionPriceType,
113
                MemberNames::SELECTION_PRICE_VALUE => $selectionPriceValue
114
            )
115
        );
116
    }
117
118
    /**
119
     * Initialize the bundle selection price with the passed attributes and returns an instance.
120
     *
121
     * @param array $attr The bundle selection price attributes
122
     *
123
     * @return array The initialized bundle selection price
124
     */
125
    protected function initializeBundleSelectionPrice(array $attr)
126
    {
127
        return $attr;
128
    }
129
130
    /**
131
     * Return's the mapping for the passed price type.
132
     *
133
     * @param string $priceType The price type to map
134
     *
135
     * @return integer The mapped price type
136
     * @throws \Exception Is thrown, if the passed price type can't be mapped
137
     */
138
    protected function mapPriceType($priceType)
139
    {
140
        return $this->getSubject()->mapPriceType($priceType);
141
    }
142
143
    /**
144
     * Return's the selection ID for the passed child SKU.
145
     *
146
     * @param string $childSku The child SKU to return the selection ID for
147
     *
148
     * @return integer The last created selection ID
149
     */
150
    protected function getChildSkuSelectionMapping($childSku)
151
    {
152
        return $this->getSubject()->getChildSkuSelectionMapping($childSku);
153
    }
154
155
    /**
156
     * Query whether or not the passed store view code is the default one.
157
     *
158
     * @param string $storeViewCode The store view code to be queried
159
     *
160
     * @return boolean TRUE if the passed store view code is the default one, else FALSE
161
     */
162
    protected function isDefaultStore($storeViewCode)
163
    {
164
        return StoreViewCodes::ADMIN === strtolower($storeViewCode);
165
    }
166
167
    /**
168
     * Return's the store for the passed store code.
169
     *
170
     * @param string $storeCode The store code to return the store for
171
     *
172
     * @return array The requested store
173
     * @throws \Exception Is thrown, if the requested store is not available
174
     */
175
    protected function getStoreByStoreCode($storeCode)
176
    {
177
        return $this->getSubject()->getStoreByStoreCode($storeCode);
178
    }
179
180
    /**
181
     * Persist's the passed product bundle selection price data and return's the ID.
182
     *
183
     * @param array $productBundleSelectionPrice The product bundle selection price data to persist
184
     *
185
     * @return void
186
     */
187
    protected function persistProductBundleSelectionPrice($productBundleSelectionPrice)
188
    {
189
        $this->getSubject()->persistProductBundleSelectionPrice($productBundleSelectionPrice);
190
    }
191
}
192