Completed
Push — master ( 326155...e3b03f )
by Tim
10s
created

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