Completed
Push — master ( e99017...2e2d17 )
by Tim
9s
created

VariantObserver::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 54
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 9.6716
c 0
b 0
f 0
ccs 0
cts 27
cp 0
cc 3
eloc 23
nc 3
nop 1
crap 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Variant\Observers\VariantObserver
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-variant
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Variant\Observers;
22
23
use TechDivision\Import\Utils\StoreViewCodes;
24
use TechDivision\Import\Product\Utils\MemberNames;
25
use TechDivision\Import\Product\Variant\Utils\ColumnKeys;
26
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
27
28
/**
29
 * A SLSB that handles the process to import product bunches.
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-variant
35
 * @link      http://www.techdivision.com
36
 */
37
class VariantObserver 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
        // load the header information
52
        $headers = $this->getHeaders();
53
54
        // prepare the store view code
55
        $this->prepareStoreViewCode($row);
56
57
        // extract the parent/child ID as well as option value and variation label from the row
58
        $parentSku = $row[$headers[ColumnKeys::VARIANT_PARENT_SKU]];
59
        $childSku = $row[$headers[ColumnKeys::VARIANT_CHILD_SKU]];
60
        $optionValue = $row[$headers[ColumnKeys::VARIANT_OPTION_VALUE]];
61
        $variationLabel = $row[$headers[ColumnKeys::VARIANT_VARIATION_LABEL]];
62
63
        // load parent/child IDs
64
        $parentId = $this->mapParentSku($parentSku);
65
        $childId = $this->mapChildSku($childSku);
66
67
        // create the product relation
68
        $this->persistProductRelation(array($parentId, $childId));
69
        $this->persistProductSuperLink(array($childId, $parentId));
70
71
        // load the store ID
72
        $store = $this->getStoreByStoreCode($this->getStoreViewCode(StoreViewCodes::ADMIN));
73
        $storeId = $store[MemberNames::STORE_ID];
74
75
        // load the EAV attribute
76
        $eavAttribute = $this->getEavAttributeByOptionValueAndStoreId($optionValue, $storeId);
77
78
        // query whether or not, the parent ID have changed
79
        if (!$this->isParentId($parentId)) {
80
            // preserve the parent ID
81
            $this->setParentId($parentId);
82
83
            // load the attribute ID
84
            $attributeId = $eavAttribute[MemberNames::ATTRIBUTE_ID];
85
            // if yes, create the super attribute load the ID of the new super attribute
86
            $productSuperAttributeId = $this->persistProductSuperAttribute(array($parentId, $attributeId, 0));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $productSuperAttributeId is correct as $this->persistProductSup...ntId, $attributeId, 0)) (which targets TechDivision\Import\Prod...ProductSuperAttribute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
87
88
            // query whether or not we've to create super attribute labels
89
            if (empty($variationLabel)) {
90
                $variationLabel = $eavAttribute[MemberNames::FRONTENT_LABEL];
91
            }
92
93
            // prepare the super attribute label
94
            $params = array($productSuperAttributeId, $storeId, 0, $variationLabel);
95
            // save the super attribute label
96
            $this->persistProductSuperAttributeLabel($params);
97
        }
98
99
        // returns the row
100
        return $row;
101
    }
102
103
    /**
104
     * Map's the passed SKU of the parent product to it's PK.
105
     *
106
     * @param string $parentSku The SKU of the parent product
107
     *
108
     * @return integer The primary key used to create relations
109
     */
110
    public function mapParentSku($parentSku)
111
    {
112
        return $this->mapSkuToEntityId($parentSku);
113
    }
114
115
    /**
116
     * Map's the passed SKU of the child product to it's PK.
117
     *
118
     * @param string $childSku The SKU of the child product
119
     *
120
     * @return integer The primary key used to create relations
121
     */
122
    public function mapChildSku($childSku)
123
    {
124
        return $this->mapSkuToEntityId($childSku);
125
    }
126
127
    /**
128
     * Return the entity ID for the passed SKU.
129
     *
130
     * @param string $sku The SKU to return the entity ID for
131
     *
132
     * @return integer The mapped entity ID
133
     * @throws \Exception Is thrown if the SKU is not mapped yet
134
     */
135
    public function mapSkuToEntityId($sku)
136
    {
137
        return $this->getSubject()->mapSkuToEntityId($sku);
138
    }
139
140
    /**
141
     * Return's TRUE if the passed ID is the parent one.
142
     *
143
     * @param integer $parentId The parent ID to check
144
     *
145
     * @return boolean TRUE if the passed ID is the parent one
146
     */
147
    public function isParentId($parentId)
148
    {
149
        return $this->getParentId() === $parentId;
150
    }
151
152
    /**
153
     * Set's the ID of the parent product to relate the variant with.
154
     *
155
     * @param integer $parentId The ID of the parent product
156
     *
157
     * @return void
158
     */
159
    public function setParentId($parentId)
160
    {
161
        $this->getSubject()->setParentId($parentId);
162
    }
163
164
    /**
165
     * Return's the ID of the parent product to relate the variant with.
166
     *
167
     * @return integer The ID of the parent product
168
     */
169
    public function getParentId()
170
    {
171
        return $this->getSubject()->getParentId();
172
    }
173
174
    /**
175
     * Return's the store for the passed store code.
176
     *
177
     * @param string $storeCode The store code to return the store for
178
     *
179
     * @return array The requested store
180
     * @throws \Exception Is thrown, if the requested store is not available
181
     */
182
    public function getStoreByStoreCode($storeCode)
183
    {
184
        return $this->getSubject()->getStoreByStoreCode($storeCode);
185
    }
186
187
    /**
188
     * Return's an array with the available stores.
189
     *
190
     * @return array The available stores
191
     */
192
    public function getStores()
193
    {
194
        return $this->getSubject()->getStores();
195
    }
196
197
    /**
198
     * Return's the first EAV attribute for the passed option value and store ID.
199
     *
200
     * @param string $optionValue The option value of the EAV attributes
201
     * @param string $storeId     The store ID of the EAV attribues
202
     *
203
     * @return array The array with the EAV attribute
204
     */
205
    public function getEavAttributeByOptionValueAndStoreId($optionValue, $storeId)
206
    {
207
        return $this->getSubject()->getEavAttributeByOptionValueAndStoreId($optionValue, $storeId);
208
    }
209
210
    /**
211
     * Persist's the passed product relation data and return's the ID.
212
     *
213
     * @param array $productRelation The product relation data to persist
214
     *
215
     * @return void
216
     */
217
    public function persistProductRelation($productRelation)
218
    {
219
        return $this->getSubject()->persistProductRelation($productRelation);
220
    }
221
222
    /**
223
     * Persist's the passed product super link data and return's the ID.
224
     *
225
     * @param array $productSuperLink The product super link data to persist
226
     *
227
     * @return void
228
     */
229
    public function persistProductSuperLink($productSuperLink)
230
    {
231
        return $this->getSubject()->persistProductSuperLink($productSuperLink);
232
    }
233
234
    /**
235
     * Persist's the passed product super attribute data and return's the ID.
236
     *
237
     * @param array $productSuperAttribute The product super attribute data to persist
238
     *
239
     * @return void
240
     */
241
    public function persistProductSuperAttribute($productSuperAttribute)
242
    {
243
        return $this->getSubject()->persistProductSuperAttribute($productSuperAttribute);
244
    }
245
246
    /**
247
     * Persist's the passed product super attribute label data and return's the ID.
248
     *
249
     * @param array $productSuperAttributeLabel The product super attribute label data to persist
250
     *
251
     * @return void
252
     */
253
    public function persistProductSuperAttributeLabel($productSuperAttributeLabel)
254
    {
255
        return $this->getSubject()->persistProductSuperAttributeLabel($productSuperAttributeLabel);
256
    }
257
}
258