Completed
Pull Request — master (#7)
by Tim
04:42
created

BunchSubject::persistUrlRewrite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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 Goodby\CSV\Export\Standard\Exporter;
24
use Goodby\CSV\Export\Standard\ExporterConfig;
25
use TechDivision\Import\Subjects\AbstractSubject;
26
use TechDivision\Import\Utils\RegistryKeys;
27
use TechDivision\Import\Services\RegistryProcessor;
28
use TechDivision\Import\Product\Utils\MemberNames;
29
use TechDivision\Import\Product\Utils\VisibilityKeys;
30
use TechDivision\Import\Product\Services\ProductProcessorInterface;
31
32
/**
33
 * The subject implementation that handles the business logic to persist products.
34
 *
35
 * @author    Tim Wagner <[email protected]>
36
 * @copyright 2016 TechDivision GmbH <[email protected]>
37
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
38
 * @link      https://github.com/techdivision/import-product
39
 * @link      http://www.techdivision.com
40
 */
41
class BunchSubject extends AbstractSubject
42
{
43
44
    /**
45
     * The processor to read/write the necessary product data.
46
     *
47
     * @var \TechDivision\Import\Product\Services\ProductProcessorInterface
48
     */
49
    protected $productProcessor;
50
51
    /**
52
     * The mapping for the supported backend types (for the product entity) => persist methods.
53
     *
54
     * @var array
55
     */
56
    protected $backendTypes = array(
57
        'datetime' => 'persistProductDatetimeAttribute',
58
        'decimal'  => 'persistProductDecimalAttribute',
59
        'int'      => 'persistProductIntAttribute',
60
        'text'     => 'persistProductTextAttribute',
61
        'varchar'  => 'persistProductVarcharAttribute'
62
    );
63
64
    /**
65
     * Mappings for attribute code => CSV column header.
66
     *
67
     * @var array
68
     */
69
    protected $headerMappings = array(
70
        'status' => 'product_online',
71
        'tax_class_id' => 'tax_class_name',
72
        'price_type'  => 'bundle_price_type',
73
        'sku_type' => 'bundle_sku_type',
74
        'price_view' => 'bundle_price_view',
75
        'weight_type' => 'bundle_weight_type',
76
        'image' => 'base_image',
77
        'image_label' => 'base_image_label',
78
        'thumbnail' => 'thumbnail_image',
79
        'thumbnail_label' => 'thumbnail_image_label'
80
    );
81
82
    /**
83
     * Mappings for the table column => CSV column header.
84
     *
85
     * @var array
86
     */
87
    protected $headerStockMappings = array(
88
        'qty'                         => array('qty', 'float'),
89
        'min_qty'                     => array('out_of_stock_qty', 'float'),
90
        'use_config_min_qty'          => array('use_config_min_qty', 'int'),
91
        'is_qty_decimal'              => array('is_qty_decimal', 'int'),
92
        'backorders'                  => array('allow_backorders', 'int'),
93
        'use_config_backorders'       => array('use_config_backorders', 'int'),
94
        'min_sale_qty'                => array('min_cart_qty', 'float'),
95
        'use_config_min_sale_qty'     => array('use_config_min_sale_qty', 'int'),
96
        'max_sale_qty'                => array('max_cart_qty', 'float'),
97
        'use_config_max_sale_qty'     => array('use_config_max_sale_qty', 'int'),
98
        'is_in_stock'                 => array('is_in_stock', 'int'),
99
        'notify_stock_qty'            => array('notify_on_stock_below', 'float'),
100
        'use_config_notify_stock_qty' => array('use_config_notify_stock_qty', 'int'),
101
        'manage_stock'                => array('manage_stock', 'int'),
102
        'use_config_manage_stock'     => array('use_config_manage_stock', 'int'),
103
        'use_config_qty_increments'   => array('use_config_qty_increments', 'int'),
104
        'qty_increments'              => array('qty_increments', 'float'),
105
        'use_config_enable_qty_inc'   => array('use_config_enable_qty_inc', 'int'),
106
        'enable_qty_increments'       => array('enable_qty_increments', 'int'),
107
        'is_decimal_divided'          => array('is_decimal_divided', 'int'),
108
    );
109
110
    /**
111
     * The array with the available visibility keys.
112
     *
113
     * @var array
114
     */
115
    protected $availableVisibilities = array(
116
        'Not Visible Individually' => VisibilityKeys::VISIBILITY_NOT_VISIBLE,
117
        'Catalog'                  => VisibilityKeys::VISIBILITY_IN_CATALOG,
118
        'Search'                   => VisibilityKeys::VISIBILITY_IN_SEARCH,
119
        'Catalog, Search'          => VisibilityKeys::VISIBILITY_BOTH
120
    );
121
122
    /**
123
     * The array containing the data for product type configuration (configurables, bundles, etc).
124
     *
125
     * @var array
126
     */
127
    protected $artefacs = array();
128
129
    /**
130
     * The mapping for the SKUs to the created entity IDs.
131
     *
132
     * @var array
133
     */
134
    protected $skuEntityIdMapping = array();
135
136
    /**
137
     * The category IDs the product is related with.
138
     *
139
     * @var array
140
     */
141
    protected $productCategoryIds = array();
142
143
    /**
144
     * The UID of the file to be imported.
145
     *
146
     * @var string
147
     */
148
    protected $uid;
149
150
    /**
151
     * The available EAV attribute sets.
152
     *
153
     * @var array
154
     */
155
    protected $attributeSets = array();
156
157
    /**
158
     * The available stores.
159
     *
160
     * @var array
161
     */
162
    protected $stores = array();
163
164
    /**
165
     * The available store websites.
166
     *
167
     * @var array
168
     */
169
    protected $storeWebsites = array();
170
171
    /**
172
     * The available EAV attributes, grouped by their attribute set and the attribute set name as keys.
173
     *
174
     * @var array
175
     */
176
    protected $attributes = array();
177
178
    /**
179
     * The available tax classes.
180
     *
181
     * @var array
182
     */
183
    protected $taxClasses = array();
184
185
    /**
186
     * The available categories.
187
     *
188
     * @var array
189
     */
190
    protected $categories = array();
191
192
    /**
193
     * The available root categories.
194
     *
195
     * @var array
196
     */
197
    protected $rootCategories = array();
198
199
    /**
200
     * The attribute set of the product that has to be created.
201
     *
202
     * @var array
203
     */
204
    protected $attributeSet = array();
205
206
    /**
207
     * The ID of the product that has been created recently.
208
     *
209
     * @var integer
210
     */
211
    protected $lastEntityId;
212
213
    /**
214
     * The SKU of the product that has been created recently.
215
     *
216
     * @var string
217
     */
218
    protected $lastSku;
219
220
    /**
221
     * The store view code the create the product/attributes for.
222
     *
223
     * @var string
224
     */
225
    protected $storeViewCode;
226
227
    /**
228
     * Set's the product processor instance.
229
     *
230
     * @param \TechDivision\Import\Product\Services\ProductProcessorInterface $productProcessor The product processor instance
231
     *
232
     * @return void
233
     */
234 3
    public function setProductProcessor(ProductProcessorInterface $productProcessor)
235
    {
236 3
        $this->productProcessor = $productProcessor;
237 3
    }
238
239
    /**
240
     * Return's the product processor instance.
241
     *
242
     * @return \TechDivision\Import\Services\ProductProcessorInterface The product processor instance
243
     */
244 3
    public function getProductProcessor()
245
    {
246 3
        return $this->productProcessor;
247
    }
248
249
    /**
250
     * Set's the SKU of the last imported product.
251
     *
252
     * @param string $lastSku The SKU
253
     *
254
     * @return void
255
     */
256
    public function setLastSku($lastSku)
257
    {
258
        $this->lastSku = $lastSku;
259
    }
260
261
    /**
262
     * Return's the SKU of the last imported product.
263
     *
264
     * @return string|null The SKU
265
     */
266
    public function getLastSku()
267
    {
268
        return $this->lastSku;
269
    }
270
271
    /**
272
     * Set's the ID of the product that has been created recently.
273
     *
274
     * @param string $lastEntityId The entity ID
275
     *
276
     * @return void
277
     */
278
    public function setLastEntityId($lastEntityId)
279
    {
280
        $this->lastEntityId = $lastEntityId;
0 ignored issues
show
Documentation Bug introduced by
The property $lastEntityId was declared of type integer, but $lastEntityId is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
281
    }
282
283
    /**
284
     * Return's the ID of the product that has been created recently.
285
     *
286
     * @return string The entity Id
287
     */
288
    public function getLastEntityId()
289
    {
290
        return $this->lastEntityId;
291
    }
292
293
    /**
294
     * Set's the attribute set of the product that has to be created.
295
     *
296
     * @param array $attributeSet The attribute set
297
     *
298
     * @return void
299
     */
300
    public function setAttributeSet(array $attributeSet)
301
    {
302
        $this->attributeSet = $attributeSet;
303
    }
304
305
    /**
306
     * Return's the attribute set of the product that has to be created.
307
     *
308
     * @return array The attribute set
309
     */
310
    public function getAttributeSet()
311
    {
312
        return $this->attributeSet;
313
    }
314
315
    /**
316
     * Set's the store view code the create the product/attributes for.
317
     *
318
     * @param string $storeViewCode The store view code
319
     *
320
     * @return void
321
     */
322 3
    public function setStoreViewCode($storeViewCode)
323
    {
324 3
        $this->storeViewCode = $storeViewCode;
325 3
    }
326
327
    /**
328
     * Return's the store view code the create the product/attributes for.
329
     *
330
     * @return string The store view code
331
     */
332
    public function getStoreViewCode()
333
    {
334
        return $this->storeViewCode;
335
    }
336
337
    /**
338
     * Intializes the previously loaded global data for exactly one bunch.
339
     *
340
     * @return void
341
     * @see \Importer\Csv\Actions\ProductImportAction::prepare()
342
     */
343
    public function setUp()
344
    {
345
346
        // load the status of the actual import
347
        $status = $this->getRegistryProcessor()->getAttribute($this->serial);
348
349
        // load the attribute set we've prepared intially
350
        $this->attributeSets = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ATTRIBUTE_SETS];
351
352
        // load the store websites we've prepare initially
353
        $this->storeWebsites =  $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::STORE_WEBSITES];
354
355
        // load the EAV attributes we've prepared initially
356
        $this->attributes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::EAV_ATTRIBUTES];
357
358
        // load the stores we've initialized before
359
        $this->stores = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::STORES];
360
361
        // load the stores we've initialized before
362
        $this->taxClasses = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::TAX_CLASSES];
363
364
        // load the categories we've initialized before
365
        $this->categories = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::CATEGORIES];
366
367
        // load the root categories we've initialized before
368
        $this->rootCategories = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ROOT_CATEGORIES];
369
370
        // prepare the callbacks
371
        parent::setUp();
372
    }
373
374
    /**
375
     * Clean up the global data after importing the bunch.
376
     *
377
     * @return void
378
     */
379
    public function tearDown()
380
    {
381
382
        // export the artefacts
383
        $this->exportArtefacts();
384
385
        // load the registry processor
386
        $registryProcessor = $this->getRegistryProcessor();
387
388
        // update the status up the actual import with the found variations, bundles, SKU => entity ID mapping and the imported files
389
        $registryProcessor->mergeAttributesRecursive($this->serial, array(RegistryKeys::SKU_ENTITY_ID_MAPPING => $this->skuEntityIdMapping));
390
        $registryProcessor->mergeAttributesRecursive($this->serial, array(RegistryKeys::FILES                 => array($this->uid => array(RegistryKeys::STATUS => 1))));
391
    }
392
393
    /**
394
     * Export's the artefacts to CSV files.
395
     *
396
     * @return void
397
     */
398
    public function exportArtefacts()
399
    {
400
401
        // load the target directory and the actual timestamp
402
        $targetDir = $this->getConfiguration()->getTargetDir();
403
        $timestamp = date('Ymd-His');
404
405
        // initialize the counter
406
        $counter = 0;
407
408
        // iterate over the artefacts and export them
409
        foreach ($this->getArtefacts() as $artefactType => $artefacts) {
410
            foreach ($artefacts as $entityArtefacts) {
411
                // initialize the the exporter
412
                $exporter = new Exporter(new ExporterConfig());
413
414
                // initialize the bunch
415
                $bunch = array();
416
417
                // set the bunch header and append the artefact data
418
                $bunch[] = array_keys(reset(reset($entityArtefacts)));
0 ignored issues
show
Bug introduced by
reset($entityArtefacts) cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
419
420
                // export the artefacts
421
                foreach ($entityArtefacts as $entityArtefact) {
422
                    $bunch = array_merge($bunch, $entityArtefact);
423
                }
424
425
                // export the artefact (bunch)
426
                $exporter->export(sprintf('%s/%s-%s_%d.csv', $targetDir, $artefactType, $timestamp, $counter++), $bunch);
427
            }
428
        }
429
    }
430
431
    /**
432
     * Cast's the passed value based on the backend type information.
433
     *
434
     * @param string $backendType The backend type to cast to
435
     * @param mixed  $value       The value to be casted
436
     *
437
     * @return mixed The casted value
438
     */
439
    public function castValueByBackendType($backendType, $value)
440
    {
441
442
        // cast the value to a valid timestamp
443
        if ($backendType === 'datetime') {
444
            return \DateTime::createFromFormat($this->getSourceDateFormat(), $value)->format('Y-m-d H:i:s');
445
        }
446
447
        // cast the value to a float value
448
        if ($backendType === 'float') {
449
            return (float) $value;
450
        }
451
452
        // cast the value to an integer
453
        if ($backendType === 'int') {
454
            return (int) $value;
455
        }
456
457
        // we don't need to cast strings
458
        return $value;
459
    }
460
461
    /**
462
     * Return's the mappings for the table column => CSV column header.
463
     *
464
     * @return array The header stock mappings
465
     */
466
    public function getHeaderStockMappings()
467
    {
468
        return $this->headerStockMappings;
469
    }
470
471
    /**
472
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
473
     *
474
     * @return array The mapping for the supported backend types
475
     */
476
    public function getBackendTypes()
477
    {
478
        return $this->backendTypes;
479
    }
480
481
    /**
482
     * Return's the attributes for the attribute set of the product that has to be created.
483
     *
484
     * @return array The attributes
485
     * @throws \Exception Is thrown if the attributes for the actual attribute set are not available
486
     */
487
    public function getAttributes()
488
    {
489
490
        // load the attribute set of the product that has to be created.
491
        $attributeSet = $this->getAttributeSet();
492
493
        // query whether or not, the requested EAV attributes are available
494
        if (isset($this->attributes[$attributeSetName = $attributeSet[MemberNames::ATTRIBUTE_SET_NAME]])) {
495
            return $this->attributes[$attributeSetName];
496
        }
497
498
        // throw an exception, if not
499
        throw new \Exception(sprintf('Found invalid attribute set name %s', $attributeSetName));
500
    }
501
502
    /**
503
     * Return's the artefacts for post-processing.
504
     *
505
     * @return array The artefacts
506
     */
507
    public function getArtefacts()
508
    {
509
        return $this->artefacs;
510
    }
511
512
    /**
513
     * Return's the store ID of the actual row.
514
     *
515
     * @return integer The ID of the actual store
516
     * @throws \Exception Is thrown, if the store with the actual code is not available
517
     */
518 View Code Duplication
    public function getRowStoreId()
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...
519
    {
520
521
        // load the store view code the create the product/attributes for
522
        $storeViewCode = $this->getStoreViewCode();
523
524
        // query whether or not, the requested store is available
525
        if (isset($this->stores[$storeViewCode])) {
526
            return (integer) $this->stores[$storeViewCode][MemberNames::STORE_ID];
527
        }
528
529
        // throw an exception, if not
530
        throw new \Exception(sprintf('Found invalid store view code %s', $storeViewCode));
531
    }
532
533
    /**
534
     * Return's the tax class ID for the passed tax class name.
535
     *
536
     * @param string $taxClassName The tax class name to return the ID for
537
     *
538
     * @return integer The tax class ID
539
     * @throws \Exception Is thrown, if the tax class with the requested name is not available
540
     */
541 View Code Duplication
    public function getTaxClassIdByTaxClassName($taxClassName)
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...
542
    {
543
544
        // query whether or not, the requested tax class is available
545
        if (isset($this->taxClasses[$taxClassName])) {
546
            return (integer) $this->taxClasses[$taxClassName][MemberNames::CLASS_ID];
547
        }
548
549
        // throw an exception, if not
550
        throw new \Exception(sprintf('Found invalid tax class name %s', $taxClassName));
551
    }
552
553
    /**
554
     * Return's the store website for the passed code.
555
     *
556
     * @param string $code The code of the store website to return the ID for
557
     *
558
     * @return integer The store website ID
559
     * @throws \Exception Is thrown, if the store website with the requested code is not available
560
     */
561 View Code Duplication
    public function getStoreWebsiteIdByCode($code)
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...
562
    {
563
564
        // query whether or not, the requested store website is available
565
        if (isset($this->storeWebsites[$code])) {
566
            return (integer) $this->storeWebsites[$code][MemberNames::WEBSITE_ID];
567
        }
568
569
        // throw an exception, if not
570
        throw new \Exception(sprintf('Found invalid website code %s', $code));
571
    }
572
573
    /**
574
     * Return's the visibility key for the passed visibility string.
575
     *
576
     * @param string $visibility The visibility string the return the key for
577
     *
578
     * @return integer The requested visibility key
579
     * @throws \Exception Is thrown, if the requested visibility is not available
580
     */
581
    public function getVisibilityIdByValue($visibility)
582
    {
583
584
        // query whether or not, the requested visibility is available
585
        if (isset($this->availableVisibilities[$visibility])) {
586
            return $this->availableVisibilities[$visibility];
587
        }
588
589
        // throw an exception, if not
590
        throw new \Exception(sprintf('Found invalid visibility %s', $visibility));
591
    }
592
593
    /**
594
     * Return's the attribute set with the passed attribute set name.
595
     *
596
     * @param string $attributeSetName The name of the requested attribute set
597
     *
598
     * @return array The attribute set data
599
     * @throws \Exception Is thrown, if the attribute set with the passed name is not available
600
     */
601
    public function getAttributeSetByAttributeSetName($attributeSetName)
602
    {
603
        // query whether or not, the requested attribute set is available
604
        if (isset($this->attributeSets[$attributeSetName])) {
605
            return $this->attributeSets[$attributeSetName];
606
        }
607
608
        // throw an exception, if not
609
        throw new \Exception(sprintf('Found invalid attribute set name %s', $attributeSetName));
610
    }
611
612
    /**
613
     * Return's the category with the passed path.
614
     *
615
     * @param string $path The path of the category to return
616
     *
617
     * @return array The category
618
     * @throws \Exception Is thrown, if the requested category is not available
619
     */
620
    public function getCategoryByPath($path)
621
    {
622
623
        // query whether or not the category with the passed path exists
624
        if (isset($this->categories[$path])) {
625
            return $this->categories[$path];
626
        }
627
628
        // throw an exception, if not
629
        throw new \Exception(sprintf('Found invalid category path %s', $path));
630
    }
631
632
    /**
633
     * Map the passed attribute code, if a header mapping exists and return the
634
     * mapped mapping.
635
     *
636
     * @param string $attributeCode The attribute code to map
637
     *
638
     * @return string The mapped attribute code, or the original one
639
     */
640
    public function mapAttributeCodeByHeaderMapping($attributeCode)
641
    {
642
643
        // query weather or not we've a mapping, if yes, map the attribute code
644
        if (isset($this->headerMappings[$attributeCode])) {
645
            $attributeCode = $this->headerMappings[$attributeCode];
646
        }
647
648
        // return the (mapped) attribute code
649
        return $attributeCode;
650
    }
651
652
    /**
653
     * Add the passed product type artefacts to the product with the
654
     * last entity ID.
655
     *
656
     * @param string $type      The artefact type, e. g. configurable
657
     * @param array  $artefacts The product type artefacts
658
     *
659
     * @return void
660
     * @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId()
661
     */
662
    public function addArtefacts($type, array $artefacts)
663
    {
664
665
        // query whether or not, any artefacts are available
666
        if (sizeof($artefacts) === 0) {
667
            return;
668
        }
669
670
        // append the artefacts to the stack
671
        $this->artefacs[$type][$this->getLastEntityId()][] = $artefacts;
672
    }
673
674
    /**
675
     * Return's the attribute option value with the passed value and store ID.
676
     *
677
     * @param mixed   $value   The option value
678
     * @param integer $storeId The ID of the store
679
     *
680
     * @return array|boolean The attribute option value instance
681
     */
682
    public function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId)
683
    {
684
        return $this->getProductProcessor()->getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId);
685
    }
686
687
    /**
688
     * Return's the URL rewrites for the passed URL entity type and ID.
689
     *
690
     * @param string  $entityType The entity type to load the URL rewrites for
691
     * @param integer $entityId   The entity ID to laod the rewrites for
692
     *
693
     * @return array The URL rewrites
694
     */
695 1
    public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId)
696
    {
697 1
        return $this->getProductProcessor()->getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId);
698
    }
699
700
    /**
701
     * Add the passed SKU => entity ID mapping.
702
     *
703
     * @param string $sku The SKU
704
     *
705
     * @return void
706
     * @uses \Import\Csv\Actions\ProductImportBunchAction::getLastEntityId()
707
     */
708
    public function addSkuEntityIdMapping($sku)
709
    {
710
        $this->skuEntityIdMapping[$sku] = $this->getLastEntityId();
711
    }
712
713
    /**
714
     * Add the passed category ID to the product's category list.
715
     *
716
     * @param integer $categoryId The category ID to add
717
     *
718
     * @return void
719
     */
720
    public function addProductCategoryId($categoryId)
721
    {
722
        $this->productCategoryIds[$this->getLastEntityId()][$categoryId] = $this->getLastEntityId();
723
    }
724
725
    /**
726
     * Return's the list with category IDs the product is related with.
727
     *
728
     * @return array The product's category IDs
729
     */
730
    public function getProductCategoryIds()
731
    {
732
        return $this->productCategoryIds[$this->getLastEntityId()];
733
    }
734
735
    /**
736
     * Return's the category with the passed ID.
737
     *
738
     * @param integer $categoryId The ID of the category to return
739
     *
740
     * @return array The category data
741
     * @throws \Exception Is thrown, if the category is not available
742
     */
743
    public function getCategory($categoryId)
744
    {
745
746
        // try to load the category with the passed ID
747
        foreach ($this->categories as $category) {
748
            if ($category[MemberNames::ENTITY_ID] == $categoryId) {
749
                return $category;
750
            }
751
        }
752
753
        // throw an exception if the category is NOT available
754
        throw new \Exception(sprintf('Can\'t load category with ID %d', $categoryId));
755
    }
756
757
    /**
758
     * Return's the root category for the actual view store.
759
     *
760
     * @return array The store's root category
761
     * @throws \Exception Is thrown if the root category for the passed store code is NOT available
762
     */
763 View Code Duplication
    public function getRootCategory()
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...
764
    {
765
766
        // load the actual store view code
767
        $storeViewCode = $this->getStoreViewCode();
768
769
        // query weather or not we've a root category or not
770
        if (isset($this->rootCategories[$storeViewCode])) {
771
            return $this->rootCategories[$storeViewCode];
772
        }
773
774
        // throw an exception if the root category is NOT available
775
        throw new \Exception(sprintf('Root category for %s is not available', $storeViewCode));
776
    }
777
778
    /**
779
     * Persist's the passed product data and return's the ID.
780
     *
781
     * @param array $product The product data to persist
782
     *
783
     * @return string The ID of the persisted entity
784
     */
785
    public function persistProduct($product)
786
    {
787
        return $this->getProductProcessor()->persistProduct($product);
788
    }
789
790
    /**
791
     * Persist's the passed product varchar attribute.
792
     *
793
     * @param array $attribute The attribute to persist
794
     *
795
     * @return void
796
     */
797
    public function persistProductVarcharAttribute($attribute)
798
    {
799
        $this->getProductProcessor()->persistProductVarcharAttribute($attribute);
800
    }
801
802
    /**
803
     * Persist's the passed product integer attribute.
804
     *
805
     * @param array $attribute The attribute to persist
806
     *
807
     * @return void
808
     */
809
    public function persistProductIntAttribute($attribute)
810
    {
811
        $this->getProductProcessor()->persistProductIntAttribute($attribute);
812
    }
813
814
    /**
815
     * Persist's the passed product decimal attribute.
816
     *
817
     * @param array $attribute The attribute to persist
818
     *
819
     * @return void
820
     */
821
    public function persistProductDecimalAttribute($attribute)
822
    {
823
        $this->getProductProcessor()->persistProductDecimalAttribute($attribute);
824
    }
825
826
    /**
827
     * Persist's the passed product datetime attribute.
828
     *
829
     * @param array $attribute The attribute to persist
830
     *
831
     * @return void
832
     */
833
    public function persistProductDatetimeAttribute($attribute)
834
    {
835
        $this->getProductProcessor()->persistProductDatetimeAttribute($attribute);
836
    }
837
838
    /**
839
     * Persist's the passed product text attribute.
840
     *
841
     * @param array $attribute The attribute to persist
842
     *
843
     * @return void
844
     */
845
    public function persistProductTextAttribute($attribute)
846
    {
847
        $this->getProductProcessor()->persistProductTextAttribute($attribute);
848
    }
849
850
    /**
851
     * Persist's the passed product website data and return's the ID.
852
     *
853
     * @param array $productWebsite The product website data to persist
854
     *
855
     * @return void
856
     */
857
    public function persistProductWebsite($productWebsite)
858
    {
859
        $this->getProductProcessor()->persistProductWebsite($productWebsite);
860
    }
861
862
    /**
863
     * Persist's the passed product category data and return's the ID.
864
     *
865
     * @param array $productCategory The product category data to persist
866
     *
867
     * @return void
868
     */
869
    public function persistProductCategory($productCategory)
870
    {
871
        $this->getProductProcessor()->persistProductCategory($productCategory);
872
    }
873
874
    /**
875
     * Persist's the passed stock item data and return's the ID.
876
     *
877
     * @param array $stockItem The stock item data to persist
878
     *
879
     * @return void
880
     */
881
    public function persistStockItem($stockItem)
882
    {
883
        $this->getProductProcessor()->persistStockItem($stockItem);
884
    }
885
886
    /**
887
     * Persist's the passed stock status data and return's the ID.
888
     *
889
     * @param array $stockStatus The stock status data to persist
890
     *
891
     * @return void
892
     */
893
    public function persistStockStatus($stockStatus)
894
    {
895
        $this->getProductProcessor()->persistStockStatus($stockStatus);
896
    }
897
898
    /**
899
     * Persist's the URL write with the passed data.
900
     *
901
     * @param array $row The URL rewrite to persist
902
     *
903
     * @return void
904
     */
905 1
    public function persistUrlRewrite($row)
906
    {
907 1
        $this->getProductProcessor()->persistUrlRewrite($row);
908 1
    }
909
910
    /**
911
     * Remove's the entity with the passed attributes.
912
     *
913
     * @param array       $row  The attributes of the entity to remove
914
     * @param string|null $name The name of the prepared statement that has to be executed
915
     *
916
     * @return void
917
     */
918
    public function removeProduct($row, $name = null)
919
    {
920
        $this->getProductProcessor()->removeProduct($row, $name);
921
    }
922
923
    /**
924
     * Delete's the URL rewrite(s) with the passed attributes.
925
     *
926
     * @param array       $row  The attributes of the entity to remove
927
     * @param string|null $name The name of the prepared statement that has to be executed
928
     *
929
     * @return void
930
     */
931 1
    public function removeUrlRewrite($row, $name = null)
932
    {
933 1
        $this->getProductProcessor()->removeUrlRewrite($row, $name);
934 1
    }
935
936
    /**
937
     * Delete's the stock item(s) with the passed attributes.
938
     *
939
     * @param array       $row  The attributes of the entity to remove
940
     * @param string|null $name The name of the prepared statement that has to be executed
941
     *
942
     * @return void
943
     */
944
    public function removeStockItem($row, $name = null)
945
    {
946
        $this->getProductProcessor()->removeStockItem($row, $name);
947
    }
948
949
    /**
950
     * Delete's the stock status with the passed attributes.
951
     *
952
     * @param array       $row  The attributes of the entity to remove
953
     * @param string|null $name The name of the prepared statement that has to be executed
954
     *
955
     * @return void
956
     */
957
    public function removeStockStatus($row, $name = null)
958
    {
959
        $this->getProductProcessor()->removeStockStatus($row, $name);
960
    }
961
962
    /**
963
     * Delete's the product website relations with the passed attributes.
964
     *
965
     * @param array       $row  The attributes of the entity to remove
966
     * @param string|null $name The name of the prepared statement that has to be executed
967
     *
968
     * @return void
969
     */
970
    public function removeProductWebsite($row, $name = null)
971
    {
972
        $this->getProductProcessor()->removeProductWebsite($row, $name);
973
    }
974
975
    /**
976
     * Delete's the product category relations with the passed attributes.
977
     *
978
     * @param array       $row  The attributes of the entity to remove
979
     * @param string|null $name The name of the prepared statement that has to be executed
980
     *
981
     * @return void
982
     */
983
    public function removeProductCategory($row, $name = null)
984
    {
985
        $this->getProductProcessor()->removeProductCategory($row, $name);
986
    }
987
}
988