Completed
Push — master ( c19e5b...bbf387 )
by Tim
8s
created

BunchSubject::exportArtefacts()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 32
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 15
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 14
nc 6
nop 0
crap 30

2 Methods

Rating   Name   Duplication   Size   Complexity  
A BunchSubject::getHeaderStockMappings() 0 4 1
A BunchSubject::getBackendTypes() 0 4 1
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 attribute code => CSV column header.
61
     *
62
     * @var array
63
     */
64
    protected $headerMappings = array(
65
        'status' => 'product_online',
66
        'tax_class_id' => 'tax_class_name',
67
        'price_type'  => 'bundle_price_type',
68
        'sku_type' => 'bundle_sku_type',
69
        'price_view' => 'bundle_price_view',
70
        'weight_type' => 'bundle_weight_type',
71
        'image' => 'base_image',
72
        'image_label' => 'base_image_label',
73
        'thumbnail' => 'thumbnail_image',
74
        'thumbnail_label' => 'thumbnail_image_label',
75
        'shipment_type' => 'bundle_shipment_type'
76
    );
77
78
    /**
79
     * Mappings for the table column => CSV column header.
80
     *
81
     * @var array
82
     */
83
    protected $headerStockMappings = array(
84
        'qty'                         => array('qty', 'float'),
85
        'min_qty'                     => array('out_of_stock_qty', 'float'),
86
        'use_config_min_qty'          => array('use_config_min_qty', 'int'),
87
        'is_qty_decimal'              => array('is_qty_decimal', 'int'),
88
        'backorders'                  => array('allow_backorders', 'int'),
89
        'use_config_backorders'       => array('use_config_backorders', 'int'),
90
        'min_sale_qty'                => array('min_cart_qty', 'float'),
91
        'use_config_min_sale_qty'     => array('use_config_min_sale_qty', 'int'),
92
        'max_sale_qty'                => array('max_cart_qty', 'float'),
93
        'use_config_max_sale_qty'     => array('use_config_max_sale_qty', 'int'),
94
        'is_in_stock'                 => array('is_in_stock', 'int'),
95
        'notify_stock_qty'            => array('notify_on_stock_below', 'float'),
96
        'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'),
97
        'manage_stock'                => array('manage_stock', 'int'),
98
        'use_config_manage_stock'     => array('use_config_manage_stock', 'int'),
99
        'use_config_qty_increments'   => array('use_config_qty_increments', 'int'),
100
        'qty_increments'              => array('qty_increments', 'float'),
101
        'use_config_enable_qty_inc'   => array('use_config_enable_qty_inc', 'int'),
102
        'enable_qty_increments'       => array('enable_qty_increments', 'int'),
103
        'is_decimal_divided'          => array('is_decimal_divided', 'int'),
104
    );
105
106
    /**
107
     * The array with the available visibility keys.
108
     *
109
     * @var array
110
     */
111
    protected $availableVisibilities = array(
112
        'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE,
113
        'Catalog'                  => VisibilityKeys::VISIBILITY_IN_CATALOG,
114
        'Search'                   => VisibilityKeys::VISIBILITY_IN_SEARCH,
115
        'Catalog, Search'          => VisibilityKeys::VISIBILITY_BOTH
116
    );
117
118
    /**
119
     * The attribute set of the product that has to be created.
120
     *
121
     * @var array
122
     */
123
    protected $attributeSet = array();
124
125
    /**
126
     * The category IDs the product is related with.
127
     *
128
     * @var array
129
     */
130
    protected $productCategoryIds = array();
131
132
    /**
133
     * Set's the attribute set of the product that has to be created.
134
     *
135
     * @param array $attributeSet The attribute set
136
     *
137
     * @return void
138
     */
139 1
    public function setAttributeSet(array $attributeSet)
140
    {
141 1
        $this->attributeSet = $attributeSet;
142 1
    }
143
144
    /**
145
     * Return's the attribute set of the product that has to be created.
146
     *
147
     * @return array The attribute set
148
     */
149
    public function getAttributeSet()
150
    {
151
        return $this->attributeSet;
152
    }
153
154
    /**
155
     * Cast's the passed value based on the backend type information.
156
     *
157
     * @param string $backendType The backend type to cast to
158
     * @param mixed  $value       The value to be casted
159
     *
160
     * @return mixed The casted value
161
     */
162 1
    public function castValueByBackendType($backendType, $value)
163
    {
164
165
        // cast the value to a valid timestamp
166 1
        if ($backendType === 'datetime') {
167
            return \DateTime::createFromFormat($this->getSourceDateFormat(), $value)->format('Y-m-d H:i:s');
168
        }
169
170
        // cast the value to a float value
171 1
        if ($backendType === 'float') {
172 1
            return (float) $value;
173
        }
174
175
        // cast the value to an integer
176 1
        if ($backendType === 'int') {
177 1
            return (int) $value;
178
        }
179
180
        // we don't need to cast strings
181
        return $value;
182
    }
183
184
    /**
185
     * Return's the mappings for the table column => CSV column header.
186
     *
187
     * @return array The header stock mappings
188
     */
189 1
    public function getHeaderStockMappings()
190
    {
191 1
        return $this->headerStockMappings;
192
    }
193
194
    /**
195
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
196
     *
197
     * @return array The mapping for the supported backend types
198
     */
199
    public function getBackendTypes()
200
    {
201
        return $this->backendTypes;
202
    }
203
204
    /**
205
     * Return's the visibility key for the passed visibility string.
206
     *
207
     * @param string $visibility The visibility string to return the key for
208
     *
209
     * @return integer The requested visibility key
210
     * @throws \Exception Is thrown, if the requested visibility is not available
211
     */
212 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...
213
    {
214
215
        // query whether or not, the requested visibility is available
216
        if (isset($this->availableVisibilities[$visibility])) {
217
            return $this->availableVisibilities[$visibility];
218
        }
219
220
        // throw an exception, if not
221
        throw new \Exception(
222
            sprintf(
223
                'Found invalid visibility %s in file %s on line %d',
224
                $visibility,
225
                $this->getFilename(),
226
                $this->getLineNumber()
227
            )
228
        );
229
    }
230
231
    /**
232
     * Map the passed attribute code, if a header mapping exists and return the
233
     * mapped mapping.
234
     *
235
     * @param string $attributeCode The attribute code to map
236
     *
237
     * @return string The mapped attribute code, or the original one
238
     */
239
    public function mapAttributeCodeByHeaderMapping($attributeCode)
240
    {
241
242
        // query weather or not we've a mapping, if yes, map the attribute code
243
        if (isset($this->headerMappings[$attributeCode])) {
244
            $attributeCode = $this->headerMappings[$attributeCode];
245
        }
246
247
        // return the (mapped) attribute code
248
        return $attributeCode;
249
    }
250
251
    /**
252
     * Add the passed category ID to the product's category list.
253
     *
254
     * @param integer $categoryId The category ID to add
255
     *
256
     * @return void
257
     */
258
    public function addProductCategoryId($categoryId)
259
    {
260
        $this->productCategoryIds[$this->getLastEntityId()][$categoryId] = $this->getLastEntityId();
261
    }
262
263
    /**
264
     * Return's the list with category IDs the product is related with.
265
     *
266
     * @return array The product's category IDs
267
     */
268
    public function getProductCategoryIds()
269
    {
270
271
        // initialize the array with the product's category IDs
272
        $categoryIds = array();
273
274
        // query whether or not category IDs are available for the actual product entity
275
        if (isset($this->productCategoryIds[$lastEntityId = $this->getLastEntityId()])) {
276
            $categoryIds = $this->productCategoryIds[$lastEntityId];
277
        }
278
279
        // return the array with the product's category IDs
280
        return $categoryIds;
281
    }
282
283
    /**
284
     * Return's the attribute option value with the passed value and store ID.
285
     *
286
     * @param mixed   $value   The option value
287
     * @param integer $storeId The ID of the store
288
     *
289
     * @return array|boolean The attribute option value instance
290
     */
291
    public function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId)
292
    {
293
        return $this->getProductProcessor()->getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId);
294
    }
295
296
    /**
297
     * Return's the URL rewrites for the passed URL entity type and ID.
298
     *
299
     * @param string  $entityType The entity type to load the URL rewrites for
300
     * @param integer $entityId   The entity ID to laod the rewrites for
301
     *
302
     * @return array The URL rewrites
303
     */
304 1
    public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId)
305
    {
306 1
        return $this->getProductProcessor()->getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId);
307
    }
308
309
    /**
310
     * Load's and return's the product with the passed SKU.
311
     *
312
     * @param string $sku The SKU of the product to load
313
     *
314
     * @return array The product
315
     */
316
    public function loadProduct($sku)
317
    {
318
        return $this->getProductProcessor()->loadProduct($sku);
319
    }
320
321
    /**
322
     * Load's and return's the product website relation with the passed product and website ID.
323
     *
324
     * @param string $productId The product ID of the relation
325
     * @param string $websiteId The website ID of the relation
326
     *
327
     * @return array The product website
328
     */
329
    public function loadProductWebsite($productId, $websiteId)
330
    {
331
        return $this->getProductProcessor()->loadProductWebsite($productId, $websiteId);
332
    }
333
334
    /**
335
     * Return's the category product relation with the passed category/product ID.
336
     *
337
     * @param integer $categoryId The category ID of the category product relation to return
338
     * @param integer $productId  The product ID of the category product relation to return
339
     *
340
     * @return array The category product relation
341
     */
342
    public function loadCategoryProduct($categoryId, $productId)
343
    {
344
        return $this->getProductProcessor()->loadCategoryProduct($categoryId, $productId);
345
    }
346
347
    /**
348
     * Load's and return's the stock status with the passed product/website/stock ID.
349
     *
350
     * @param integer $productId The product ID of the stock status to load
351
     * @param integer $websiteId The website ID of the stock status to load
352
     * @param integer $stockId   The stock ID of the stock status to load
353
     *
354
     * @return array The stock status
355
     */
356
    public function loadStockStatus($productId, $websiteId, $stockId)
357
    {
358
        return $this->getProductProcessor()->loadStockStatus($productId, $websiteId, $stockId);
359
    }
360
361
    /**
362
     * Load's and return's the stock status with the passed product/website/stock ID.
363
     *
364
     * @param integer $productId The product ID of the stock item to load
365
     * @param integer $websiteId The website ID of the stock item to load
366
     * @param integer $stockId   The stock ID of the stock item to load
367
     *
368
     * @return array The stock item
369
     */
370
    public function loadStockItem($productId, $websiteId, $stockId)
371
    {
372
        return $this->getProductProcessor()->loadStockItem($productId, $websiteId, $stockId);
373
    }
374
375
    /**
376
     * Load's and return's the datetime attribute with the passed entity/attribute/store ID.
377
     *
378
     * @param integer $entityId    The entity ID of the attribute
379
     * @param integer $attributeId The attribute ID of the attribute
380
     * @param integer $storeId     The store ID of the attribute
381
     *
382
     * @return array|null The datetime attribute
383
     */
384
    public function loadProductDatetimeAttribute($entityId, $attributeId, $storeId)
385
    {
386
        return $this->getProductProcessor()->loadProductDatetimeAttribute($entityId, $attributeId, $storeId);
387
    }
388
389
    /**
390
     * Load's and return's the decimal attribute with the passed entity/attribute/store ID.
391
     *
392
     * @param integer $entityId    The entity ID of the attribute
393
     * @param integer $attributeId The attribute ID of the attribute
394
     * @param integer $storeId     The store ID of the attribute
395
     *
396
     * @return array|null The decimal attribute
397
     */
398
    public function loadProductDecimalAttribute($entityId, $attributeId, $storeId)
399
    {
400
        return $this->getProductProcessor()->loadProductDecimalAttribute($entityId, $attributeId, $storeId);
401
    }
402
403
    /**
404
     * Load's and return's the integer attribute with the passed entity/attribute/store ID.
405
     *
406
     * @param integer $entityId    The entity ID of the attribute
407
     * @param integer $attributeId The attribute ID of the attribute
408
     * @param integer $storeId     The store ID of the attribute
409
     *
410
     * @return array|null The integer attribute
411
     */
412
    public function loadProductIntAttribute($entityId, $attributeId, $storeId)
413
    {
414
        return $this->getProductProcessor()->loadProductIntAttribute($entityId, $attributeId, $storeId);
415
    }
416
417
    /**
418
     * Load's and return's the text attribute with the passed entity/attribute/store ID.
419
     *
420
     * @param integer $entityId    The entity ID of the attribute
421
     * @param integer $attributeId The attribute ID of the attribute
422
     * @param integer $storeId     The store ID of the attribute
423
     *
424
     * @return array|null The text attribute
425
     */
426
    public function loadProductTextAttribute($entityId, $attributeId, $storeId)
427
    {
428
        return $this->getProductProcessor()->loadProductTextAttribute($entityId, $attributeId, $storeId);
429
    }
430
431
    /**
432
     * Load's and return's the varchar attribute with the passed entity/attribute/store ID.
433
     *
434
     * @param integer $entityId    The entity ID of the attribute
435
     * @param integer $attributeId The attribute ID of the attribute
436
     * @param integer $storeId     The store ID of the attribute
437
     *
438
     * @return array|null The varchar attribute
439
     */
440
    public function loadProductVarcharAttribute($entityId, $attributeId, $storeId)
441
    {
442
        return $this->getProductProcessor()->loadProductVarcharAttribute($entityId, $attributeId, $storeId);
443
    }
444
445
    /**
446
     * Persist's the passed product data and return's the ID.
447
     *
448
     * @param array $product The product data to persist
449
     *
450
     * @return string The ID of the persisted entity
451
     */
452
    public function persistProduct($product)
453
    {
454
        return $this->getProductProcessor()->persistProduct($product);
455
    }
456
457
    /**
458
     * Persist's the passed product varchar attribute.
459
     *
460
     * @param array $attribute The attribute to persist
461
     *
462
     * @return void
463
     */
464
    public function persistProductVarcharAttribute($attribute)
465
    {
466
        $this->getProductProcessor()->persistProductVarcharAttribute($attribute);
467
    }
468
469
    /**
470
     * Persist's the passed product integer attribute.
471
     *
472
     * @param array $attribute The attribute to persist
473
     *
474
     * @return void
475
     */
476
    public function persistProductIntAttribute($attribute)
477
    {
478
        $this->getProductProcessor()->persistProductIntAttribute($attribute);
479
    }
480
481
    /**
482
     * Persist's the passed product decimal attribute.
483
     *
484
     * @param array $attribute The attribute to persist
485
     *
486
     * @return void
487
     */
488
    public function persistProductDecimalAttribute($attribute)
489
    {
490
        $this->getProductProcessor()->persistProductDecimalAttribute($attribute);
491
    }
492
493
    /**
494
     * Persist's the passed product datetime attribute.
495
     *
496
     * @param array $attribute The attribute to persist
497
     *
498
     * @return void
499
     */
500
    public function persistProductDatetimeAttribute($attribute)
501
    {
502
        $this->getProductProcessor()->persistProductDatetimeAttribute($attribute);
503
    }
504
505
    /**
506
     * Persist's the passed product text attribute.
507
     *
508
     * @param array $attribute The attribute to persist
509
     *
510
     * @return void
511
     */
512
    public function persistProductTextAttribute($attribute)
513
    {
514
        $this->getProductProcessor()->persistProductTextAttribute($attribute);
515
    }
516
517
    /**
518
     * Persist's the passed product website data and return's the ID.
519
     *
520
     * @param array $productWebsite The product website data to persist
521
     *
522
     * @return void
523
     */
524
    public function persistProductWebsite($productWebsite)
525
    {
526
        $this->getProductProcessor()->persistProductWebsite($productWebsite);
527
    }
528
529
    /**
530
     * Persist's the passed category product relation.
531
     *
532
     * @param array $categoryProduct The category product relation to persist
533
     *
534
     * @return void
535
     */
536
    public function persistCategoryProduct($categoryProduct)
537
    {
538
        $this->getProductProcessor()->persistCategoryProduct($categoryProduct);
539
    }
540
541
    /**
542
     * Persist's the passed stock item data and return's the ID.
543
     *
544
     * @param array $stockItem The stock item data to persist
545
     *
546
     * @return void
547
     */
548
    public function persistStockItem($stockItem)
549
    {
550
        $this->getProductProcessor()->persistStockItem($stockItem);
551
    }
552
553
    /**
554
     * Persist's the passed stock status data and return's the ID.
555
     *
556
     * @param array $stockStatus The stock status data to persist
557
     *
558
     * @return void
559
     */
560
    public function persistStockStatus($stockStatus)
561
    {
562
        $this->getProductProcessor()->persistStockStatus($stockStatus);
563
    }
564
565
    /**
566
     * Persist's the URL write with the passed data.
567
     *
568
     * @param array $row The URL rewrite to persist
569
     *
570
     * @return void
571
     */
572 1
    public function persistUrlRewrite($row)
573
    {
574 1
        $this->getProductProcessor()->persistUrlRewrite($row);
575 1
    }
576
577
    /**
578
     * Delete's the entity 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 deleteProduct($row, $name = null)
586
    {
587
        $this->getProductProcessor()->deleteProduct($row, $name);
588
    }
589
590
    /**
591
     * Delete's the URL rewrite(s) 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 1
    public function deleteUrlRewrite($row, $name = null)
599
    {
600 1
        $this->getProductProcessor()->deleteUrlRewrite($row, $name);
601 1
    }
602
603
    /**
604
     * Delete's the stock item(s) 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 deleteStockItem($row, $name = null)
612
    {
613
        $this->getProductProcessor()->deleteStockItem($row, $name);
614
    }
615
616
    /**
617
     * Delete's the stock status with the passed attributes.
618
     *
619
     * @param array       $row  The attributes of the entity to delete
620
     * @param string|null $name The name of the prepared statement that has to be executed
621
     *
622
     * @return void
623
     */
624
    public function deleteStockStatus($row, $name = null)
625
    {
626
        $this->getProductProcessor()->deleteStockStatus($row, $name);
627
    }
628
629
    /**
630
     * Delete's the product website relations with the passed attributes.
631
     *
632
     * @param array       $row  The attributes of the entity to delete
633
     * @param string|null $name The name of the prepared statement that has to be executed
634
     *
635
     * @return void
636
     */
637
    public function deleteProductWebsite($row, $name = null)
638
    {
639
        $this->getProductProcessor()->deleteProductWebsite($row, $name);
640
    }
641
642
    /**
643
     * Delete's the category product relations with the passed attributes.
644
     *
645
     * @param array       $row  The attributes of the entity to delete
646
     * @param string|null $name The name of the prepared statement that has to be executed
647
     *
648
     * @return void
649
     */
650
    public function deleteCategoryProduct($row, $name = null)
651
    {
652
        $this->getProductProcessor()->deleteCategoryProduct($row, $name);
653
    }
654
}
655