Completed
Pull Request — master (#15)
by Tim
04:41
created

mapAttributeCodeByHeaderMapping()   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 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Observers\ProductAttributeObserver
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Observers;
22
23
use TechDivision\Import\Utils\StoreViewCodes;
24
use TechDivision\Import\Product\Utils\ColumnKeys;
25
use TechDivision\Import\Product\Utils\MemberNames;
26
use TechDivision\Import\Product\Observers\AbstractProductImportObserver;
27
28
/**
29
 * Observer that creates/updates the product's attributes.
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
35
 * @link      http://www.techdivision.com
36
 */
37
class ProductAttributeObserver extends AbstractProductImportObserver
38
{
39
40
    /**
41
     * The ID of the attribute to create the values for.
42
     *
43
     * @var integer
44
     */
45
    protected $attributeId;
46
47
    /**
48
     * The attribute code of the attribute to create the values for.
49
     *
50
     * @var string
51
     */
52
    protected $attributeCode;
53
54
    /**
55
     * The backend type of the attribute to create the values for.
56
     *
57
     * @var string
58
     */
59
    protected $backendType;
60
61
    /**
62
     * The the persist method for the found backend type.
63
     *
64
     * @var string
65
     */
66
    protected $persistMethod;
67
68
    /**
69
     * Will be invoked by the action on the events the listener has been registered for.
70
     *
71
     * @param array $row The row to handle
72
     *
73
     * @return array The modified row
74
     * @see \TechDivision\Import\Product\Observers\ImportObserverInterface::handle()
75
     */
76
    public function handle(array $row)
77
    {
78
79
        // load the header information
80
        $headers = $this->getHeaders();
81
82
        // initialize the store view code
83
        $this->prepareStoreViewCode($row);
84
85
        // load the attributes by the found attribute set
86
        $attributes = $this->getAttributes();
87
88
        // iterate over the attribute related by the found attribute set
89
        foreach ($attributes as $attribute) {
90
            // load the attribute code/ID
91
            $attributeCode = $attribute[MemberNames::ATTRIBUTE_CODE];
92
            $attributeId = (integer) $attribute[MemberNames::ATTRIBUTE_ID];
93
94
            // query weather or not we've a mapping, if yes, map the attribute code
95
            $attributeCode = $this->mapAttributeCodeByHeaderMapping($attributeCode);
96
97
            // query whether or not we found the column in the CSV file
98
            if (!isset($headers[$attributeCode]) ||
99
                !isset($row[$headers[$attributeCode]])
100
            ) {
101
                continue;
102
            }
103
104
            // query whether or not, a value is available
105
            if ($row[$headers[$attributeCode]] === null ||
106
                trim($row[$headers[$attributeCode]]) === ''
107
            ) {
108
                continue;
109
            }
110
111
            // load the backend type => to find the apropriate entity
112
            $backendType = $attribute[MemberNames::BACKEND_TYPE];
113
            if ($backendType == null) {
114
                $this->getSystemLogger()->warning(
115
                    sprintf('Found EMTPY backend type for attribute %s', $attributeCode)
116
                );
117
                continue;
118
            }
119
120
            // load the supported backend types
121
            $backendTypes = $this->getBackendTypes();
122
123
            // query whether or not we've found a supported backend type
124
            if (isset($backendTypes[$backendType])) {
125
                // initialize the persist method for the found backend type
126
                $this->setPersistMethod($backendTypes[$backendType]);
127
128
                // initialize attribute ID/code and backend type
129
                $this->setAttributeId($attributeId);
130
                $this->setAttributeCode($attributeCode);
131
                $this->setBackendType($backendType);
132
133
                // load the row's value and persist it
134
                $this->processAttribute($row[$headers[$attributeCode]]);
135
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
136
            } else {
137
                $this->getSystemLogger()->debug(
138
                    sprintf('Found invalid backend type %s for attribute %s', $backendType, $attributeCode)
139
                );
140
            }
141
        }
142
143
        // returns the row
144
        return $row;
145
    }
146
147
    /**
148
     * This method finally persists the passed value by invoking the
149
     * persist method defined by the attribute's backend type.
150
     *
151
     * @param mixed $value The value to persist
152
     *
153
     * @return void
154
     */
155
    public function processAttribute($value)
156
    {
157
158
        // laod the callbacks for the actual attribute code
159
        $callbacks = $this->getCallbacksByType($this->getAttributeCode());
160
161
        // invoke the pre-cast callbacks
162
        foreach ($callbacks as $callback) {
163
            $value = $callback->handle($value);
164
        }
165
166
        // load the ID of the product that has been created recently
167
        $lastEntityId = $this->getPrimaryKey();
168
169
        // load the ID of the attribute to create the values for
170
        $attributeId = $this->getAttributeId();
171
172
        // load the store ID
173
        $storeId = $this->getRowStoreId();
174
175
        // load the backend type of the actual attribute
176
        $backendType = $this->getBackendType();
177
178
        // cast the value based on the backend type
179
        $castedValue = $this->castValueByBackendType($backendType, $value);
180
181
        // prepare the attribute values
182
        $attribute = array($lastEntityId, $attributeId, $storeId, $castedValue);
183
184
        // initialize and persist the entity attribute
185
        $persistMethod = $this->getPersistMethod();
186
        $this->$persistMethod($attribute);
187
    }
188
189
    /**
190
     * Return's the PK to create the product => attribute relation.
191
     *
192
     * @return integer The PK to create the relation with
193
     */
194
    public function getPrimaryKey()
195
    {
196
        return $this->getLastEntityId();
197
    }
198
199
    /**
200
     * Set's the persist method for the found backend type.
201
     *
202
     * @param string $persistMethod The persist method
203
     *
204
     * @return void
205
     */
206
    public function setPersistMethod($persistMethod)
207
    {
208
        $this->persistMethod = $persistMethod;
209
    }
210
211
    /**
212
     * Return's the persist method for the found backend type.
213
     *
214
     * @return string The persist method
215
     */
216
    public function getPersistMethod()
217
    {
218
        return $this->persistMethod;
219
    }
220
221
    /**
222
     * Set's the backend type of the attribute to create the values for.
223
     *
224
     * @param string $backendType The backend type
225
     *
226
     * @return void
227
     */
228
    public function setBackendType($backendType)
229
    {
230
        $this->backendType = $backendType;
231
    }
232
233
    /**
234
     * Return's the backend type of the attribute to create the values for.
235
     *
236
     * @return string The backend type
237
     */
238
    public function getBackendType()
239
    {
240
        return $this->backendType;
241
    }
242
243
    /**
244
     * Set's the attribute code of the attribute to create the values for.
245
     *
246
     * @param string $attributeCode The attribute code
247
     *
248
     * @return void
249
     */
250
    public function setAttributeCode($attributeCode)
251
    {
252
        $this->attributeCode = $attributeCode;
253
    }
254
255
    /**
256
     * Return's the attribute code of the attribute to create the values for.
257
     *
258
     * @return string The attribute code
259
     */
260
    public function getAttributeCode()
261
    {
262
        return $this->attributeCode;
263
    }
264
265
    /**
266
     * Set's the ID of the attribute to create the values for.
267
     *
268
     * @param integer $attributeId The attribute ID
269
     *
270
     * @return void
271
     */
272
    public function setAttributeId($attributeId)
273
    {
274
        $this->attributeId = $attributeId;
275
    }
276
277
    /**
278
     * Return's the ID of the attribute to create the values for.
279
     *
280
     * @return integer The attribute ID
281
     */
282
    public function getAttributeId()
283
    {
284
        return $this->attributeId;
285
    }
286
287
    /**
288
     * Map the passed attribute code, if a header mapping exists and return the
289
     * mapped mapping.
290
     *
291
     * @param string $attributeCode The attribute code to map
292
     *
293
     * @return string The mapped attribute code, or the original one
294
     */
295
    public function mapAttributeCodeByHeaderMapping($attributeCode)
296
    {
297
        return $this->getSubject()->mapAttributeCodeByHeaderMapping($attributeCode);
298
    }
299
300
    /**
301
     * Return's the array with callbacks for the passed type.
302
     *
303
     * @param string $type The type of the callbacks to return
304
     *
305
     * @return array The callbacks
306
     */
307
    public function getCallbacksByType($type)
308
    {
309
        return $this->getSubject()->getCallbacksByType($type);
310
    }
311
312
    /**
313
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
314
     *
315
     * @return array The mapping for the supported backend types
316
     */
317
    public function getBackendTypes()
318
    {
319
        return $this->getSubject()->getBackendTypes();
320
    }
321
322
    /**
323
     * Return's the attributes for the attribute set of the product that has to be created.
324
     *
325
     * @return array The attributes
326
     * @throws \Exception
327
     */
328
    public function getAttributes()
329
    {
330
        return $this->getSubject()->getAttributes();
331
    }
332
333
    /**
334
     * Return's the store ID of the actual row.
335
     *
336
     * @return integer The ID of the actual store
337
     * @throws \Exception Is thrown, if the store with the actual code is not available
338
     */
339
    public function getRowStoreId()
340
    {
341
        return $this->getSubject()->getRowStoreId();
342
    }
343
344
    /**
345
     * Persist's the passed product varchar attribute.
346
     *
347
     * @param array $attribute The attribute to persist
348
     *
349
     * @return void
350
     */
351
    public function persistProductVarcharAttribute($attribute)
352
    {
353
        $this->getSubject()->persistProductVarcharAttribute($attribute);
354
    }
355
356
    /**
357
     * Persist's the passed product integer attribute.
358
     *
359
     * @param array $attribute The attribute to persist
360
     *
361
     * @return void
362
     */
363
    public function persistProductIntAttribute($attribute)
364
    {
365
        $this->getSubject()->persistProductIntAttribute($attribute);
366
    }
367
368
    /**
369
     * Persist's the passed product decimal attribute.
370
     *
371
     * @param array $attribute The attribute to persist
372
     *
373
     * @return void
374
     */
375
    public function persistProductDecimalAttribute($attribute)
376
    {
377
        $this->getSubject()->persistProductDecimalAttribute($attribute);
378
    }
379
380
    /**
381
     * Persist's the passed product datetime attribute.
382
     *
383
     * @param array $attribute The attribute to persist
384
     *
385
     * @return void
386
     */
387
    public function persistProductDatetimeAttribute($attribute)
388
    {
389
        $this->getSubject()->persistProductDatetimeAttribute($attribute);
390
    }
391
392
    /**
393
     * Persist's the passed product text attribute.
394
     *
395
     * @param array $attribute The attribute to persist
396
     *
397
     * @return void
398
     */
399
    public function persistProductTextAttribute($attribute)
400
    {
401
        $this->getSubject()->persistProductTextAttribute($attribute);
402
    }
403
}
404