Completed
Pull Request — master (#33)
by Tim
04:03
created

BunchSubject::mapAttributeCodeByHeaderMapping()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Subjects\BunchSubject
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\Subjects;
22
23
use TechDivision\Import\Product\Utils\VisibilityKeys;
24
use TechDivision\Import\Subjects\ExportableTrait;
25
use TechDivision\Import\Subjects\ExportableSubjectInterface;
26
27
/**
28
 * The subject implementation that handles the business logic to persist products.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-product
34
 * @link      http://www.techdivision.com
35
 */
36
class BunchSubject extends AbstractProductSubject implements ExportableSubjectInterface
37
{
38
39
    /**
40
     * The trait that implements the export functionality.
41
     *
42
     * @var \TechDivision\Import\Subjects\ExportableTrait
43
     */
44
    use ExportableTrait;
45
46
    /**
47
     * The mapping for the supported backend types (for the product entity) => persist methods.
48
     *
49
     * @var array
50
     */
51
    protected $backendTypes = array(
52
        'datetime' => array('persistProductDatetimeAttribute', 'loadProductDatetimeAttribute'),
53
        'decimal'  => array('persistProductDecimalAttribute', 'loadProductDecimalAttribute'),
54
        'int'      => array('persistProductIntAttribute', 'loadProductIntAttribute'),
55
        'text'     => array('persistProductTextAttribute', 'loadProductTextAttribute'),
56
        'varchar'  => array('persistProductVarcharAttribute', 'loadProductVarcharAttribute')
57
    );
58
59
    /**
60
     * Mappings for the table column => CSV column header.
61
     *
62
     * @var array
63
     */
64
    protected $headerStockMappings = array(
65
        'qty'                         => array('qty', 'float'),
66
        'min_qty'                     => array('out_of_stock_qty', 'float'),
67
        'use_config_min_qty'          => array('use_config_min_qty', 'int'),
68
        'is_qty_decimal'              => array('is_qty_decimal', 'int'),
69
        'backorders'                  => array('allow_backorders', 'int'),
70
        'use_config_backorders'       => array('use_config_backorders', 'int'),
71
        'min_sale_qty'                => array('min_cart_qty', 'float'),
72
        'use_config_min_sale_qty'     => array('use_config_min_sale_qty', 'int'),
73
        'max_sale_qty'                => array('max_cart_qty', 'float'),
74
        'use_config_max_sale_qty'     => array('use_config_max_sale_qty', 'int'),
75
        'is_in_stock'                 => array('is_in_stock', 'int'),
76
        'notify_stock_qty'            => array('notify_on_stock_below', 'float'),
77
        'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'),
78
        'manage_stock'                => array('manage_stock', 'int'),
79
        'use_config_manage_stock'     => array('use_config_manage_stock', 'int'),
80
        'use_config_qty_increments'   => array('use_config_qty_increments', 'int'),
81
        'qty_increments'              => array('qty_increments', 'float'),
82
        'use_config_enable_qty_inc'   => array('use_config_enable_qty_inc', 'int'),
83
        'enable_qty_increments'       => array('enable_qty_increments', 'int'),
84
        'is_decimal_divided'          => array('is_decimal_divided', 'int'),
85
    );
86
87
    /**
88
     * The array with the available visibility keys.
89
     *
90
     * @var array
91
     */
92
    protected $availableVisibilities = array(
93
        'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE,
94
        'Catalog'                  => VisibilityKeys::VISIBILITY_IN_CATALOG,
95
        'Search'                   => VisibilityKeys::VISIBILITY_IN_SEARCH,
96
        'Catalog, Search'          => VisibilityKeys::VISIBILITY_BOTH
97
    );
98
99
    /**
100
     * The attribute set of the product that has to be created.
101
     *
102
     * @var array
103
     */
104
    protected $attributeSet = array();
105
106
    /**
107
     * The category IDs the product is related with.
108
     *
109
     * @var array
110
     */
111
    protected $productCategoryIds = array();
112
113
    /**
114
     * Set's the attribute set of the product that has to be created.
115
     *
116
     * @param array $attributeSet The attribute set
117
     *
118
     * @return void
119
     */
120 1
    public function setAttributeSet(array $attributeSet)
121
    {
122 1
        $this->attributeSet = $attributeSet;
123 1
    }
124
125
    /**
126
     * Return's the attribute set of the product that has to be created.
127
     *
128
     * @return array The attribute set
129
     */
130
    public function getAttributeSet()
131
    {
132
        return $this->attributeSet;
133
    }
134
135
    /**
136
     * Cast's the passed value based on the backend type information.
137
     *
138
     * @param string $backendType The backend type to cast to
139
     * @param mixed  $value       The value to be casted
140
     *
141
     * @return mixed The casted value
142
     */
143 1
    public function castValueByBackendType($backendType, $value)
144
    {
145
146
        // cast the value to a valid timestamp
147 1
        if ($backendType === 'datetime') {
148
            return \DateTime::createFromFormat($this->getSourceDateFormat(), $value)->format('Y-m-d H:i:s');
149
        }
150
151
        // cast the value to a float value
152 1
        if ($backendType === 'float') {
153 1
            return (float) $value;
154
        }
155
156
        // cast the value to an integer
157 1
        if ($backendType === 'int') {
158 1
            return (int) $value;
159
        }
160
161
        // we don't need to cast strings
162
        return $value;
163
    }
164
165
    /**
166
     * Return's the mappings for the table column => CSV column header.
167
     *
168
     * @return array The header stock mappings
169
     */
170 1
    public function getHeaderStockMappings()
171
    {
172 1
        return $this->headerStockMappings;
173
    }
174
175
    /**
176
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
177
     *
178
     * @return array The mapping for the supported backend types
179
     */
180
    public function getBackendTypes()
181
    {
182
        return $this->backendTypes;
183
    }
184
185
    /**
186
     * Return's the visibility key for the passed visibility string.
187
     *
188
     * @param string $visibility The visibility string to return the key for
189
     *
190
     * @return integer The requested visibility key
191
     * @throws \Exception Is thrown, if the requested visibility is not available
192
     */
193 View Code Duplication
    public function getVisibilityIdByValue($visibility)
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...
194
    {
195
196
        // query whether or not, the requested visibility is available
197
        if (isset($this->availableVisibilities[$visibility])) {
198
            return $this->availableVisibilities[$visibility];
199
        }
200
201
        // throw an exception, if not
202
        throw new \Exception(
203
            sprintf(
204
                'Found invalid visibility %s in file %s on line %d',
205
                $visibility,
206
                $this->getFilename(),
207
                $this->getLineNumber()
208
            )
209
        );
210
    }
211
212
    /**
213
     * Add the passed category ID to the product's category list.
214
     *
215
     * @param integer $categoryId The category ID to add
216
     *
217
     * @return void
218
     */
219
    public function addProductCategoryId($categoryId)
220
    {
221
        $this->productCategoryIds[$this->getLastEntityId()][$categoryId] = $this->getLastEntityId();
222
    }
223
224
    /**
225
     * Return's the list with category IDs the product is related with.
226
     *
227
     * @return array The product's category IDs
228
     */
229
    public function getProductCategoryIds()
230
    {
231
232
        // initialize the array with the product's category IDs
233
        $categoryIds = array();
234
235
        // query whether or not category IDs are available for the actual product entity
236
        if (isset($this->productCategoryIds[$lastEntityId = $this->getLastEntityId()])) {
237
            $categoryIds = $this->productCategoryIds[$lastEntityId];
238
        }
239
240
        // return the array with the product's category IDs
241
        return $categoryIds;
242
    }
243
244
    /**
245
     * Return's the attribute option value with the passed value and store ID.
246
     *
247
     * @param mixed   $value   The option value
248
     * @param integer $storeId The ID of the store
249
     *
250
     * @return array|boolean The attribute option value instance
251
     */
252
    public function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId)
253
    {
254
        return $this->getProductProcessor()->getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId);
255
    }
256
257
    /**
258
     * Return's the URL rewrites for the passed URL entity type and ID.
259
     *
260
     * @param string  $entityType The entity type to load the URL rewrites for
261
     * @param integer $entityId   The entity ID to laod the rewrites for
262
     *
263
     * @return array The URL rewrites
264
     */
265 1
    public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId)
266
    {
267 1
        return $this->getProductProcessor()->getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId);
268
    }
269
270
    /**
271
     * Load's and return's the product with the passed SKU.
272
     *
273
     * @param string $sku The SKU of the product to load
274
     *
275
     * @return array The product
276
     */
277
    public function loadProduct($sku)
278
    {
279
        return $this->getProductProcessor()->loadProduct($sku);
280
    }
281
282
    /**
283
     * Load's and return's the product website relation with the passed product and website ID.
284
     *
285
     * @param string $productId The product ID of the relation
286
     * @param string $websiteId The website ID of the relation
287
     *
288
     * @return array The product website
289
     */
290
    public function loadProductWebsite($productId, $websiteId)
291
    {
292
        return $this->getProductProcessor()->loadProductWebsite($productId, $websiteId);
293
    }
294
295
    /**
296
     * Return's the category product relation with the passed category/product ID.
297
     *
298
     * @param integer $categoryId The category ID of the category product relation to return
299
     * @param integer $productId  The product ID of the category product relation to return
300
     *
301
     * @return array The category product relation
302
     */
303
    public function loadCategoryProduct($categoryId, $productId)
304
    {
305
        return $this->getProductProcessor()->loadCategoryProduct($categoryId, $productId);
306
    }
307
308
    /**
309
     * Load's and return's the stock status with the passed product/website/stock ID.
310
     *
311
     * @param integer $productId The product ID of the stock status to load
312
     * @param integer $websiteId The website ID of the stock status to load
313
     * @param integer $stockId   The stock ID of the stock status to load
314
     *
315
     * @return array The stock status
316
     */
317
    public function loadStockStatus($productId, $websiteId, $stockId)
318
    {
319
        return $this->getProductProcessor()->loadStockStatus($productId, $websiteId, $stockId);
320
    }
321
322
    /**
323
     * Load's and return's the stock status with the passed product/website/stock ID.
324
     *
325
     * @param integer $productId The product ID of the stock item to load
326
     * @param integer $websiteId The website ID of the stock item to load
327
     * @param integer $stockId   The stock ID of the stock item to load
328
     *
329
     * @return array The stock item
330
     */
331
    public function loadStockItem($productId, $websiteId, $stockId)
332
    {
333
        return $this->getProductProcessor()->loadStockItem($productId, $websiteId, $stockId);
334
    }
335
336
    /**
337
     * Load's and return's the datetime attribute with the passed entity/attribute/store ID.
338
     *
339
     * @param integer $entityId    The entity ID of the attribute
340
     * @param integer $attributeId The attribute ID of the attribute
341
     * @param integer $storeId     The store ID of the attribute
342
     *
343
     * @return array|null The datetime attribute
344
     */
345
    public function loadProductDatetimeAttribute($entityId, $attributeId, $storeId)
346
    {
347
        return $this->getProductProcessor()->loadProductDatetimeAttribute($entityId, $attributeId, $storeId);
348
    }
349
350
    /**
351
     * Load's and return's the decimal attribute with the passed entity/attribute/store ID.
352
     *
353
     * @param integer $entityId    The entity ID of the attribute
354
     * @param integer $attributeId The attribute ID of the attribute
355
     * @param integer $storeId     The store ID of the attribute
356
     *
357
     * @return array|null The decimal attribute
358
     */
359
    public function loadProductDecimalAttribute($entityId, $attributeId, $storeId)
360
    {
361
        return $this->getProductProcessor()->loadProductDecimalAttribute($entityId, $attributeId, $storeId);
362
    }
363
364
    /**
365
     * Load's and return's the integer attribute with the passed entity/attribute/store ID.
366
     *
367
     * @param integer $entityId    The entity ID of the attribute
368
     * @param integer $attributeId The attribute ID of the attribute
369
     * @param integer $storeId     The store ID of the attribute
370
     *
371
     * @return array|null The integer attribute
372
     */
373
    public function loadProductIntAttribute($entityId, $attributeId, $storeId)
374
    {
375
        return $this->getProductProcessor()->loadProductIntAttribute($entityId, $attributeId, $storeId);
376
    }
377
378
    /**
379
     * Load's and return's the text attribute with the passed entity/attribute/store ID.
380
     *
381
     * @param integer $entityId    The entity ID of the attribute
382
     * @param integer $attributeId The attribute ID of the attribute
383
     * @param integer $storeId     The store ID of the attribute
384
     *
385
     * @return array|null The text attribute
386
     */
387
    public function loadProductTextAttribute($entityId, $attributeId, $storeId)
388
    {
389
        return $this->getProductProcessor()->loadProductTextAttribute($entityId, $attributeId, $storeId);
390
    }
391
392
    /**
393
     * Load's and return's the varchar attribute with the passed entity/attribute/store ID.
394
     *
395
     * @param integer $entityId    The entity ID of the attribute
396
     * @param integer $attributeId The attribute ID of the attribute
397
     * @param integer $storeId     The store ID of the attribute
398
     *
399
     * @return array|null The varchar attribute
400
     */
401
    public function loadProductVarcharAttribute($entityId, $attributeId, $storeId)
402
    {
403
        return $this->getProductProcessor()->loadProductVarcharAttribute($entityId, $attributeId, $storeId);
404
    }
405
406
    /**
407
     * Persist's the passed product data and return's the ID.
408
     *
409
     * @param array $product The product data to persist
410
     *
411
     * @return string The ID of the persisted entity
412
     */
413
    public function persistProduct($product)
414
    {
415
        return $this->getProductProcessor()->persistProduct($product);
416
    }
417
418
    /**
419
     * Persist's the passed product varchar attribute.
420
     *
421
     * @param array $attribute The attribute to persist
422
     *
423
     * @return void
424
     */
425
    public function persistProductVarcharAttribute($attribute)
426
    {
427
        $this->getProductProcessor()->persistProductVarcharAttribute($attribute);
428
    }
429
430
    /**
431
     * Persist's the passed product integer attribute.
432
     *
433
     * @param array $attribute The attribute to persist
434
     *
435
     * @return void
436
     */
437
    public function persistProductIntAttribute($attribute)
438
    {
439
        $this->getProductProcessor()->persistProductIntAttribute($attribute);
440
    }
441
442
    /**
443
     * Persist's the passed product decimal attribute.
444
     *
445
     * @param array $attribute The attribute to persist
446
     *
447
     * @return void
448
     */
449
    public function persistProductDecimalAttribute($attribute)
450
    {
451
        $this->getProductProcessor()->persistProductDecimalAttribute($attribute);
452
    }
453
454
    /**
455
     * Persist's the passed product datetime attribute.
456
     *
457
     * @param array $attribute The attribute to persist
458
     *
459
     * @return void
460
     */
461
    public function persistProductDatetimeAttribute($attribute)
462
    {
463
        $this->getProductProcessor()->persistProductDatetimeAttribute($attribute);
464
    }
465
466
    /**
467
     * Persist's the passed product text attribute.
468
     *
469
     * @param array $attribute The attribute to persist
470
     *
471
     * @return void
472
     */
473
    public function persistProductTextAttribute($attribute)
474
    {
475
        $this->getProductProcessor()->persistProductTextAttribute($attribute);
476
    }
477
478
    /**
479
     * Persist's the passed product website data and return's the ID.
480
     *
481
     * @param array $productWebsite The product website data to persist
482
     *
483
     * @return void
484
     */
485
    public function persistProductWebsite($productWebsite)
486
    {
487
        $this->getProductProcessor()->persistProductWebsite($productWebsite);
488
    }
489
490
    /**
491
     * Persist's the passed category product relation.
492
     *
493
     * @param array $categoryProduct The category product relation to persist
494
     *
495
     * @return void
496
     */
497
    public function persistCategoryProduct($categoryProduct)
498
    {
499
        $this->getProductProcessor()->persistCategoryProduct($categoryProduct);
500
    }
501
502
    /**
503
     * Persist's the passed stock item data and return's the ID.
504
     *
505
     * @param array $stockItem The stock item data to persist
506
     *
507
     * @return void
508
     */
509
    public function persistStockItem($stockItem)
510
    {
511
        $this->getProductProcessor()->persistStockItem($stockItem);
512
    }
513
514
    /**
515
     * Persist's the passed stock status data and return's the ID.
516
     *
517
     * @param array $stockStatus The stock status data to persist
518
     *
519
     * @return void
520
     */
521
    public function persistStockStatus($stockStatus)
522
    {
523
        $this->getProductProcessor()->persistStockStatus($stockStatus);
524
    }
525
526
    /**
527
     * Persist's the URL write with the passed data.
528
     *
529
     * @param array $row The URL rewrite to persist
530
     *
531
     * @return void
532
     */
533 1
    public function persistUrlRewrite($row)
534
    {
535 1
        $this->getProductProcessor()->persistUrlRewrite($row);
536 1
    }
537
538
    /**
539
     * Delete's the entity with the passed attributes.
540
     *
541
     * @param array       $row  The attributes of the entity to delete
542
     * @param string|null $name The name of the prepared statement that has to be executed
543
     *
544
     * @return void
545
     */
546
    public function deleteProduct($row, $name = null)
547
    {
548
        $this->getProductProcessor()->deleteProduct($row, $name);
549
    }
550
551
    /**
552
     * Delete's the URL rewrite(s) with the passed attributes.
553
     *
554
     * @param array       $row  The attributes of the entity to delete
555
     * @param string|null $name The name of the prepared statement that has to be executed
556
     *
557
     * @return void
558
     */
559 1
    public function deleteUrlRewrite($row, $name = null)
560
    {
561 1
        $this->getProductProcessor()->deleteUrlRewrite($row, $name);
562 1
    }
563
564
    /**
565
     * Delete's the stock item(s) with the passed attributes.
566
     *
567
     * @param array       $row  The attributes of the entity to delete
568
     * @param string|null $name The name of the prepared statement that has to be executed
569
     *
570
     * @return void
571
     */
572
    public function deleteStockItem($row, $name = null)
573
    {
574
        $this->getProductProcessor()->deleteStockItem($row, $name);
575
    }
576
577
    /**
578
     * Delete's the stock status with the passed attributes.
579
     *
580
     * @param array       $row  The attributes of the entity to delete
581
     * @param string|null $name The name of the prepared statement that has to be executed
582
     *
583
     * @return void
584
     */
585
    public function deleteStockStatus($row, $name = null)
586
    {
587
        $this->getProductProcessor()->deleteStockStatus($row, $name);
588
    }
589
590
    /**
591
     * Delete's the product website relations with the passed attributes.
592
     *
593
     * @param array       $row  The attributes of the entity to delete
594
     * @param string|null $name The name of the prepared statement that has to be executed
595
     *
596
     * @return void
597
     */
598
    public function deleteProductWebsite($row, $name = null)
599
    {
600
        $this->getProductProcessor()->deleteProductWebsite($row, $name);
601
    }
602
603
    /**
604
     * Delete's the category product relations with the passed attributes.
605
     *
606
     * @param array       $row  The attributes of the entity to delete
607
     * @param string|null $name The name of the prepared statement that has to be executed
608
     *
609
     * @return void
610
     */
611
    public function deleteCategoryProduct($row, $name = null)
612
    {
613
        $this->getProductProcessor()->deleteCategoryProduct($row, $name);
614
    }
615
}
616