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

BundleSelectionPriceObserver::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 6
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
     * Process the observer's business logic.
42
     *
43
     * @return array The processed row
44
     */
45
    protected function process()
46
    {
47
48
        // prepare the store view code
49
        $this->prepareStoreViewCode($this->getRow());
0 ignored issues
show
Unused Code introduced by
The call to BundleSelectionPriceObse...:prepareStoreViewCode() has too many arguments starting with $this->getRow().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
50
51
        // load the store view code
52
        $storeViewCode = $this->getStoreViewCode(StoreViewCodes::ADMIN);
53
54
        // check if we're in default store
55
        if (!$this->isDefaultStore($storeViewCode)) {
56
            // prepare and initialize the bundle selection price
57
            $productBundleSelectionPrice = $this->initializeBundleSelectionPrice($this->prepareAttributes());
58
59
            // persist the bundle selection price
60
            $this->persistProductBundleSelectionPrice($productBundleSelectionPrice);
61
        }
62
    }
63
64
    /**
65
     * Prepare the attributes of the entity that has to be persisted.
66
     *
67
     * @return array The prepared attributes
68
     */
69
    protected function prepareAttributes()
70
    {
71
72
        // load the store view code
73
        $storeViewCode = $this->getStoreViewCode(StoreViewCodes::ADMIN);
74
75
        // load the store/website ID
76
        $store = $this->getStoreByStoreCode($storeViewCode);
77
        $websiteId = $store[MemberNames::WEBSITE_ID];
78
79
        // load the default values
80
        $selectionPriceType = $this->mapPriceType($this->getValue(ColumnKeys::BUNDLE_VALUE_PRICE_TYPE));
81
        $selectionPriceValue = $this->getValue(ColumnKeys::BUNDLE_VALUE_PRICE);
82
83
        // load the selection ID for the child SKU
84
        $selectionId = $this->getChildSkuSelectionMapping($this->getValue(ColumnKeys::BUNDLE_VALUE_SKU));
85
86
        // return the prepared bundle selection price
87
        return $this->initializeEntity(
88
            array(
89
                MemberNames::SELECTION_ID          => $selectionId,
90
                MemberNames::WEBSITE_ID            => $websiteId,
91
                MemberNames::SELECTION_PRICE_TYPE  => $selectionPriceType,
92
                MemberNames::SELECTION_PRICE_VALUE => $selectionPriceValue
93
            )
94
        );
95
    }
96
97
    /**
98
     * Initialize the bundle selection price with the passed attributes and returns an instance.
99
     *
100
     * @param array $attr The bundle selection price attributes
101
     *
102
     * @return array The initialized bundle selection price
103
     */
104
    protected function initializeBundleSelectionPrice(array $attr)
105
    {
106
        return $attr;
107
    }
108
109
    /**
110
     * Return's the mapping for the passed price type.
111
     *
112
     * @param string $priceType The price type to map
113
     *
114
     * @return integer The mapped price type
115
     * @throws \Exception Is thrown, if the passed price type can't be mapped
116
     */
117
    protected function mapPriceType($priceType)
118
    {
119
        return $this->getSubject()->mapPriceType($priceType);
120
    }
121
122
    /**
123
     * Return's the selection ID for the passed child SKU.
124
     *
125
     * @param string $childSku The child SKU to return the selection ID for
126
     *
127
     * @return integer The last created selection ID
128
     */
129
    protected function getChildSkuSelectionMapping($childSku)
130
    {
131
        return $this->getSubject()->getChildSkuSelectionMapping($childSku);
132
    }
133
134
    /**
135
     * Query whether or not the passed store view code is the default one.
136
     *
137
     * @param string $storeViewCode The store view code to be queried
138
     *
139
     * @return boolean TRUE if the passed store view code is the default one, else FALSE
140
     */
141
    protected function isDefaultStore($storeViewCode)
142
    {
143
        return StoreViewCodes::ADMIN === strtolower($storeViewCode);
144
    }
145
146
    /**
147
     * Return's the store for the passed store code.
148
     *
149
     * @param string $storeCode The store code to return the store for
150
     *
151
     * @return array The requested store
152
     * @throws \Exception Is thrown, if the requested store is not available
153
     */
154
    protected function getStoreByStoreCode($storeCode)
155
    {
156
        return $this->getSubject()->getStoreByStoreCode($storeCode);
157
    }
158
159
    /**
160
     * Persist's the passed product bundle selection price data and return's the ID.
161
     *
162
     * @param array $productBundleSelectionPrice The product bundle selection price data to persist
163
     *
164
     * @return void
165
     */
166
    protected function persistProductBundleSelectionPrice($productBundleSelectionPrice)
167
    {
168
        $this->getSubject()->persistProductBundleSelectionPrice($productBundleSelectionPrice);
169
    }
170
}
171