Completed
Pull Request — master (#8)
by Tim
02:57
created

addChildSkuSelectionIdMapping()   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\BundleSelectionObserver
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\Observers\AbstractProductImportObserver;
26
use TechDivision\Import\Product\Bundle\Utils\MemberNames;
27
28
/**
29
 * Oberserver that provides functionality for the bundle selection 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 BundleSelectionObserver extends AbstractProductImportObserver
38
{
39
40
    /**
41
     * Process the observer's business logic.
42
     *
43
     * @return array The processed row
44
     */
45 View Code Duplication
    protected function process()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
48
        // prepare the store view code
49
        $this->prepareStoreViewCode($this->getRow());
0 ignored issues
show
Unused Code introduced by
The call to BundleSelectionObserver::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
        // return immediately if we're have no store view code set
52
        if (StoreViewCodes::ADMIN !== $this->getStoreViewCode(StoreViewCodes::ADMIN)) {
53
            return;
54
        }
55
56
        // prepare, initialize and persist the product bundle selection data
57
        $productBundleSelection = $this->initializeBundleSelection($this->prepareAttributes());
58
        $selectionId = $this->persistProductBundleSelection($productBundleSelection);
59
60
        // add the mapping for the child SKU => selection ID
61
        $this->addChildSkuSelectionIdMapping($this->getValue(ColumnKeys::BUNDLE_VALUE_SKU), $selectionId);
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 product bundle option SKU
73
        $parentSku = $this->getValue(ColumnKeys::BUNDLE_PARENT_SKU);
74
75
        // load parent/option ID
76
        $parentId = $this->mapSkuToEntityId($parentSku);
77
78
        // load the actual option ID
79
        $optionId = $this->getLastOptionId();
80
81
        // load the child ID
82
        $childSku = $this->getValue(ColumnKeys::BUNDLE_VALUE_SKU);
83
        $childId = $this->mapSkuToEntityId($childSku);
84
85
        // load the default values
86
        $selectionCanChangeQty = 1;
87
        $selectionPriceType = $this->mapPriceType($this->getValue(ColumnKeys::BUNDLE_VALUE_PRICE_TYPE));
88
        $selectionPriceValue = $this->getValue(ColumnKeys::BUNDLE_VALUE_PRICE);
89
        $selectionQty = $this->getValue(ColumnKeys::BUNDLE_VALUE_DEFAULT_QTY);
90
        $isDefault = $this->getValue(ColumnKeys::BUNDLE_VALUE_DEFAULT);
91
92
        // laod the position counter
93
        $position = $this->raisePositionCounter();
94
95
        // prepare the product bundle selection data
96
        return $this->initializeEntity(
97
            array(
98
                MemberNames::OPTION_ID                => $optionId,
99
                MemberNames::PARENT_PRODUCT_ID        => $parentId,
100
                MemberNames::PRODUCT_ID               => $childId,
101
                MemberNames::POSITION                 => $position,
102
                MemberNames::IS_DEFAULT               => $isDefault,
103
                MemberNames::SELECTION_PRICE_TYPE     => $selectionPriceType,
104
                MemberNames::SELECTION_PRICE_VALUE    => $selectionPriceValue,
105
                MemberNames::SELECTION_QTY            => $selectionQty,
106
                MemberNames::SELECTION_CAN_CHANGE_QTY => $selectionCanChangeQty
107
            )
108
        );
109
    }
110
111
    /**
112
     * Initialize the bundle selection with the passed attributes and returns an instance.
113
     *
114
     * @param array $attr The bundle selection attributes
115
     *
116
     * @return array The initialized bundle selection
117
     */
118
    protected function initializeBundleSelection(array $attr)
119
    {
120
        return $attr;
121
    }
122
123
    /**
124
     * Return's the last created option ID.
125
     *
126
     * @return integer $optionId The last created option ID
127
     */
128
    protected function getLastOptionId()
129
    {
130
        return $this->getSubject()->getLastOptionId();
131
    }
132
133
    /**
134
     * Save's the mapping of the child SKU and the selection ID.
135
     *
136
     * @param string  $childSku    The child SKU of the selection
137
     * @param integer $selectionId The selection ID to save
138
     *
139
     * @return void
140
     */
141
    protected function addChildSkuSelectionIdMapping($childSku, $selectionId)
142
    {
143
        $this->getSubject()->addChildSkuSelectionIdMapping($childSku, $selectionId);
144
    }
145
146
    /**
147
     * Returns the acutal value of the position counter and raise's it by one.
148
     *
149
     * @return integer The actual value of the position counter
150
     */
151
    protected function raisePositionCounter()
152
    {
153
        return $this->getSubject()->raisePositionCounter();
154
    }
155
156
    /**
157
     * Return's the option ID for the passed name.
158
     *
159
     * @param string $name The name to return the option ID for
160
     *
161
     * @return integer The option ID for the passed name
162
     * @throws \Exception Is thrown, if no option ID for the passed name is available
163
     */
164
    protected function getOptionIdForName($name)
165
    {
166
        return $this->getSubject()-> getOptionIdForName($name);
167
    }
168
169
    /**
170
     * Return's the mapping for the passed price type.
171
     *
172
     * @param string $priceType The price type to map
173
     *
174
     * @return integer The mapped price type
175
     * @throws \Exception Is thrown, if the passed price type can't be mapped
176
     */
177
    protected function mapPriceType($priceType)
178
    {
179
        return $this->getSubject()->mapPriceType($priceType);
180
    }
181
182
    /**
183
     * Return the entity ID for the passed SKU.
184
     *
185
     * @param string $sku The SKU to return the entity ID for
186
     *
187
     * @return integer The mapped entity ID
188
     * @throws \Exception Is thrown if the SKU is not mapped yet
189
     */
190
    protected function mapSkuToEntityId($sku)
191
    {
192
        return $this->getSubject()->mapSkuToEntityId($sku);
193
    }
194
195
    /**
196
     * Persist's the passed product bundle selection data and return's the ID.
197
     *
198
     * @param array $productBundleSelection The product bundle selection data to persist
199
     *
200
     * @return string The ID of the persisted entity
201
     */
202
    protected function persistProductBundleSelection($productBundleSelection)
203
    {
204
        return $this->getSubject()->persistProductBundleSelection($productBundleSelection);
205
    }
206
}
207