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

BundleSelectionObserver::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 7

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 18
loc 18
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 6
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
     * 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...undleSelectionObserver>.

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...undleSelectionObserver>.

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 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...
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...undleSelectionObserver>.

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
        // return immediately if we're have no store view code set
73
        if (StoreViewCodes::ADMIN !== $this->getStoreViewCode(StoreViewCodes::ADMIN)) {
74
            return;
75
        }
76
77
        // prepare, initialize and persist the product bundle selection data
78
        $productBundleSelection = $this->initializeBundleSelection($this->prepareAttributes());
79
        $selectionId = $this->persistProductBundleSelection($productBundleSelection);
80
81
        // add the mapping for the child SKU => selection ID
82
        $this->addChildSkuSelectionIdMapping($this->getValue(ColumnKeys::BUNDLE_VALUE_SKU), $selectionId);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...undleSelectionObserver>.

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...
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 product bundle option SKU
94
        $parentSku = $this->getValue(ColumnKeys::BUNDLE_PARENT_SKU);
0 ignored issues
show
Bug introduced by
The method getValue() does not seem to exist on object<TechDivision\Impo...undleSelectionObserver>.

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...
95
96
        // load parent/option ID
97
        $parentId = $this->mapSkuToEntityId($parentSku);
98
99
        // load the actual option ID
100
        $optionId = $this->getLastOptionId();
101
102
        // load the child ID
103
        $childSku = $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...undleSelectionObserver>.

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...
104
        $childId = $this->mapSkuToEntityId($childSku);
105
106
        // load the default values
107
        $selectionCanChangeQty = 1;
108
        $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...undleSelectionObserver>.

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
        $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...undleSelectionObserver>.

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

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

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...
112
113
        // laod the position counter
114
        $position = $this->raisePositionCounter();
115
116
        // prepare the product bundle selection data
117
        return $this->initializeEntity(
0 ignored issues
show
Bug introduced by
The method initializeEntity() does not seem to exist on object<TechDivision\Impo...undleSelectionObserver>.

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...
118
            array(
119
                MemberNames::OPTION_ID                => $optionId,
120
                MemberNames::PARENT_PRODUCT_ID        => $parentId,
121
                MemberNames::PRODUCT_ID               => $childId,
122
                MemberNames::POSITION                 => $position,
123
                MemberNames::IS_DEFAULT               => $isDefault,
124
                MemberNames::SELECTION_PRICE_TYPE     => $selectionPriceType,
125
                MemberNames::SELECTION_PRICE_VALUE    => $selectionPriceValue,
126
                MemberNames::SELECTION_QTY            => $selectionQty,
127
                MemberNames::SELECTION_CAN_CHANGE_QTY => $selectionCanChangeQty
128
            )
129
        );
130
    }
131
132
    /**
133
     * Initialize the bundle selection with the passed attributes and returns an instance.
134
     *
135
     * @param array $attr The bundle selection attributes
136
     *
137
     * @return array The initialized bundle selection
138
     */
139
    protected function initializeBundleSelection(array $attr)
140
    {
141
        return $attr;
142
    }
143
144
    /**
145
     * Return's the last created option ID.
146
     *
147
     * @return integer $optionId The last created option ID
148
     */
149
    protected function getLastOptionId()
150
    {
151
        return $this->getSubject()->getLastOptionId();
152
    }
153
154
    /**
155
     * Save's the mapping of the child SKU and the selection ID.
156
     *
157
     * @param string  $childSku    The child SKU of the selection
158
     * @param integer $selectionId The selection ID to save
159
     *
160
     * @return void
161
     */
162
    protected function addChildSkuSelectionIdMapping($childSku, $selectionId)
163
    {
164
        $this->getSubject()->addChildSkuSelectionIdMapping($childSku, $selectionId);
165
    }
166
167
    /**
168
     * Returns the acutal value of the position counter and raise's it by one.
169
     *
170
     * @return integer The actual value of the position counter
171
     */
172
    protected function raisePositionCounter()
173
    {
174
        return $this->getSubject()->raisePositionCounter();
175
    }
176
177
    /**
178
     * Return's the option ID for the passed name.
179
     *
180
     * @param string $name The name to return the option ID for
181
     *
182
     * @return integer The option ID for the passed name
183
     * @throws \Exception Is thrown, if no option ID for the passed name is available
184
     */
185
    protected function getOptionIdForName($name)
186
    {
187
        return $this->getSubject()-> getOptionIdForName($name);
188
    }
189
190
    /**
191
     * Return's the mapping for the passed price type.
192
     *
193
     * @param string $priceType The price type to map
194
     *
195
     * @return integer The mapped price type
196
     * @throws \Exception Is thrown, if the passed price type can't be mapped
197
     */
198
    protected function mapPriceType($priceType)
199
    {
200
        return $this->getSubject()->mapPriceType($priceType);
201
    }
202
203
    /**
204
     * Return the entity ID for the passed SKU.
205
     *
206
     * @param string $sku The SKU to return the entity ID for
207
     *
208
     * @return integer The mapped entity ID
209
     * @throws \Exception Is thrown if the SKU is not mapped yet
210
     */
211
    protected function mapSkuToEntityId($sku)
212
    {
213
        return $this->getSubject()->mapSkuToEntityId($sku);
214
    }
215
216
    /**
217
     * Persist's the passed product bundle selection data and return's the ID.
218
     *
219
     * @param array $productBundleSelection The product bundle selection data to persist
220
     *
221
     * @return string The ID of the persisted entity
222
     */
223
    protected function persistProductBundleSelection($productBundleSelection)
224
    {
225
        return $this->getSubject()->persistProductBundleSelection($productBundleSelection);
226
    }
227
}
228