Completed
Push — master ( b9383d...d4bc8a )
by Tim
9s
created

BunchSubject::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
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 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 string
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;
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($this->getExportConfig());
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
     * Initialize and return the exporter configuration.
433
     *
434
     * @return \Goodby\CSV\Export\Standard\ExporterConfig The exporter configuration
435
     */
436
    protected function getExportConfig()
437
    {
438
439
        // initialize the lexer configuration
440
        $config = new ExporterConfig();
441
442
        // query whether or not a delimiter character has been configured
443
        if ($delimiter = $this->getConfiguration()->getDelimiter()) {
444
            $config->setDelimiter($delimiter);
445
        }
446
447
        // query whether or not a custom escape character has been configured
448
        if ($escape = $this->getConfiguration()->getEscape()) {
449
            $config->setEscape($escape);
450
        }
451
452
        // query whether or not a custom enclosure character has been configured
453
        if ($enclosure = $this->getConfiguration()->getEnclosure()) {
454
            $config->setEnclosure($enclosure);
455
        }
456
457
        // query whether or not a custom source charset has been configured
458
        if ($fromCharset = $this->getConfiguration()->getFromCharset()) {
459
            $config->setFromCharset($fromCharset);
460
        }
461
462
        // query whether or not a custom target charset has been configured
463
        if ($toCharset = $this->getConfiguration()->getToCharset()) {
464
            $config->setToCharset($toCharset);
465
        }
466
467
        // query whether or not a custom file mode has been configured
468
        if ($fileMode = $this->getConfiguration()->getFileMode()) {
469
            $config->setFileMode($fileMode);
470
        }
471
472
        // return the lexer configuratio
473
        return $config;
474
    }
475
476
    /**
477
     * Cast's the passed value based on the backend type information.
478
     *
479
     * @param string $backendType The backend type to cast to
480
     * @param mixed  $value       The value to be casted
481
     *
482
     * @return mixed The casted value
483
     */
484
    public function castValueByBackendType($backendType, $value)
485
    {
486
487
        // cast the value to a valid timestamp
488
        if ($backendType === 'datetime') {
489
            return \DateTime::createFromFormat($this->getSourceDateFormat(), $value)->format('Y-m-d H:i:s');
490
        }
491
492
        // cast the value to a float value
493
        if ($backendType === 'float') {
494
            return (float) $value;
495
        }
496
497
        // cast the value to an integer
498
        if ($backendType === 'int') {
499
            return (int) $value;
500
        }
501
502
        // we don't need to cast strings
503
        return $value;
504
    }
505
506
    /**
507
     * Return's the mappings for the table column => CSV column header.
508
     *
509
     * @return array The header stock mappings
510
     */
511
    public function getHeaderStockMappings()
512
    {
513
        return $this->headerStockMappings;
514
    }
515
516
    /**
517
     * Return's mapping for the supported backend types (for the product entity) => persist methods.
518
     *
519
     * @return array The mapping for the supported backend types
520
     */
521
    public function getBackendTypes()
522
    {
523
        return $this->backendTypes;
524
    }
525
526
    /**
527
     * Return's the attributes for the attribute set of the product that has to be created.
528
     *
529
     * @return array The attributes
530
     * @throws \Exception Is thrown if the attributes for the actual attribute set are not available
531
     */
532
    public function getAttributes()
533
    {
534
535
        // load the attribute set of the product that has to be created.
536
        $attributeSet = $this->getAttributeSet();
537
538
        // query whether or not, the requested EAV attributes are available
539
        if (isset($this->attributes[$attributeSetName = $attributeSet[MemberNames::ATTRIBUTE_SET_NAME]])) {
540
            return $this->attributes[$attributeSetName];
541
        }
542
543
        // throw an exception, if not
544
        throw new \Exception(sprintf('Found invalid attribute set name %s', $attributeSetName));
545
    }
546
547
    /**
548
     * Return's the artefacts for post-processing.
549
     *
550
     * @return array The artefacts
551
     */
552
    public function getArtefacts()
553
    {
554
        return $this->artefacs;
555
    }
556
557
    /**
558
     * Return's the store ID of the actual row.
559
     *
560
     * @return integer The ID of the actual store
561
     * @throws \Exception Is thrown, if the store with the actual code is not available
562
     */
563 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...
564
    {
565
566
        // load the store view code the create the product/attributes for
567
        $storeViewCode = $this->getStoreViewCode();
568
569
        // query whether or not, the requested store is available
570
        if (isset($this->stores[$storeViewCode])) {
571
            return (integer) $this->stores[$storeViewCode][MemberNames::STORE_ID];
572
        }
573
574
        // throw an exception, if not
575
        throw new \Exception(sprintf('Found invalid store view code %s', $storeViewCode));
576
    }
577
578
    /**
579
     * Return's the tax class ID for the passed tax class name.
580
     *
581
     * @param string $taxClassName The tax class name to return the ID for
582
     *
583
     * @return integer The tax class ID
584
     * @throws \Exception Is thrown, if the tax class with the requested name is not available
585
     */
586 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...
587
    {
588
589
        // query whether or not, the requested tax class is available
590
        if (isset($this->taxClasses[$taxClassName])) {
591
            return (integer) $this->taxClasses[$taxClassName][MemberNames::CLASS_ID];
592
        }
593
594
        // throw an exception, if not
595
        throw new \Exception(sprintf('Found invalid tax class name %s', $taxClassName));
596
    }
597
598
    /**
599
     * Return's the store website for the passed code.
600
     *
601
     * @param string $code The code of the store website to return the ID for
602
     *
603
     * @return integer The store website ID
604
     * @throws \Exception Is thrown, if the store website with the requested code is not available
605
     */
606 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...
607
    {
608
609
        // query whether or not, the requested store website is available
610
        if (isset($this->storeWebsites[$code])) {
611
            return (integer) $this->storeWebsites[$code][MemberNames::WEBSITE_ID];
612
        }
613
614
        // throw an exception, if not
615
        throw new \Exception(sprintf('Found invalid website code %s', $code));
616
    }
617
618
    /**
619
     * Return's the visibility key for the passed visibility string.
620
     *
621
     * @param string $visibility The visibility string the return the key for
622
     *
623
     * @return integer The requested visibility key
624
     * @throws \Exception Is thrown, if the requested visibility is not available
625
     */
626
    public function getVisibilityIdByValue($visibility)
627
    {
628
629
        // query whether or not, the requested visibility is available
630
        if (isset($this->availableVisibilities[$visibility])) {
631
            return $this->availableVisibilities[$visibility];
632
        }
633
634
        // throw an exception, if not
635
        throw new \Exception(sprintf('Found invalid visibility %s', $visibility));
636
    }
637
638
    /**
639
     * Return's the attribute set with the passed attribute set name.
640
     *
641
     * @param string $attributeSetName The name of the requested attribute set
642
     *
643
     * @return array The attribute set data
644
     * @throws \Exception Is thrown, if the attribute set with the passed name is not available
645
     */
646
    public function getAttributeSetByAttributeSetName($attributeSetName)
647
    {
648
        // query whether or not, the requested attribute set is available
649
        if (isset($this->attributeSets[$attributeSetName])) {
650
            return $this->attributeSets[$attributeSetName];
651
        }
652
653
        // throw an exception, if not
654
        throw new \Exception(sprintf('Found invalid attribute set name %s', $attributeSetName));
655
    }
656
657
    /**
658
     * Return's the category with the passed path.
659
     *
660
     * @param string $path The path of the category to return
661
     *
662
     * @return array The category
663
     * @throws \Exception Is thrown, if the requested category is not available
664
     */
665
    public function getCategoryByPath($path)
666
    {
667
668
        // query whether or not the category with the passed path exists
669
        if (isset($this->categories[$path])) {
670
            return $this->categories[$path];
671
        }
672
673
        // throw an exception, if not
674
        throw new \Exception(sprintf('Found invalid category path %s', $path));
675
    }
676
677
    /**
678
     * Map the passed attribute code, if a header mapping exists and return the
679
     * mapped mapping.
680
     *
681
     * @param string $attributeCode The attribute code to map
682
     *
683
     * @return string The mapped attribute code, or the original one
684
     */
685
    public function mapAttributeCodeByHeaderMapping($attributeCode)
686
    {
687
688
        // query weather or not we've a mapping, if yes, map the attribute code
689
        if (isset($this->headerMappings[$attributeCode])) {
690
            $attributeCode = $this->headerMappings[$attributeCode];
691
        }
692
693
        // return the (mapped) attribute code
694
        return $attributeCode;
695
    }
696
697
    /**
698
     * Add the passed product type artefacts to the product with the
699
     * last entity ID.
700
     *
701
     * @param string $type      The artefact type, e. g. configurable
702
     * @param array  $artefacts The product type artefacts
703
     *
704
     * @return void
705
     * @uses \TechDivision\Import\Product\Subjects\BunchSubject::getLastEntityId()
706
     */
707
    public function addArtefacts($type, array $artefacts)
708
    {
709
710
        // query whether or not, any artefacts are available
711
        if (sizeof($artefacts) === 0) {
712
            return;
713
        }
714
715
        // append the artefacts to the stack
716
        $this->artefacs[$type][$this->getLastEntityId()][] = $artefacts;
717
    }
718
719
    /**
720
     * Return's the attribute option value with the passed value and store ID.
721
     *
722
     * @param mixed   $value   The option value
723
     * @param integer $storeId The ID of the store
724
     *
725
     * @return array|boolean The attribute option value instance
726
     */
727
    public function getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId)
728
    {
729
        return $this->getProductProcessor()->getEavAttributeOptionValueByOptionValueAndStoreId($value, $storeId);
730
    }
731
732
    /**
733
     * Return's the URL rewrites for the passed URL entity type and ID.
734
     *
735
     * @param string  $entityType The entity type to load the URL rewrites for
736
     * @param integer $entityId   The entity ID to laod the rewrites for
737
     *
738
     * @return array The URL rewrites
739
     */
740 1
    public function getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId)
741
    {
742 1
        return $this->getProductProcessor()->getUrlRewritesByEntityTypeAndEntityId($entityType, $entityId);
743
    }
744
745
    /**
746
     * Add the passed SKU => entity ID mapping.
747
     *
748
     * @param string $sku The SKU
749
     *
750
     * @return void
751
     * @uses \Import\Csv\Actions\ProductImportBunchAction::getLastEntityId()
752
     */
753
    public function addSkuEntityIdMapping($sku)
754
    {
755
        $this->skuEntityIdMapping[$sku] = $this->getLastEntityId();
756
    }
757
758
    /**
759
     * Add the passed category ID to the product's category list.
760
     *
761
     * @param integer $categoryId The category ID to add
762
     *
763
     * @return void
764
     */
765
    public function addProductCategoryId($categoryId)
766
    {
767
        $this->productCategoryIds[$this->getLastEntityId()][$categoryId] = $this->getLastEntityId();
768
    }
769
770
    /**
771
     * Return's the list with category IDs the product is related with.
772
     *
773
     * @return array The product's category IDs
774
     */
775
    public function getProductCategoryIds()
776
    {
777
        return $this->productCategoryIds[$this->getLastEntityId()];
778
    }
779
780
    /**
781
     * Return's the category with the passed ID.
782
     *
783
     * @param integer $categoryId The ID of the category to return
784
     *
785
     * @return array The category data
786
     * @throws \Exception Is thrown, if the category is not available
787
     */
788
    public function getCategory($categoryId)
789
    {
790
791
        // try to load the category with the passed ID
792
        foreach ($this->categories as $category) {
793
            if ($category[MemberNames::ENTITY_ID] == $categoryId) {
794
                return $category;
795
            }
796
        }
797
798
        // throw an exception if the category is NOT available
799
        throw new \Exception(sprintf('Can\'t load category with ID %d', $categoryId));
800
    }
801
802
    /**
803
     * Return's the root category for the actual view store.
804
     *
805
     * @return array The store's root category
806
     * @throws \Exception Is thrown if the root category for the passed store code is NOT available
807
     */
808 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...
809
    {
810
811
        // load the actual store view code
812
        $storeViewCode = $this->getStoreViewCode();
813
814
        // query weather or not we've a root category or not
815
        if (isset($this->rootCategories[$storeViewCode])) {
816
            return $this->rootCategories[$storeViewCode];
817
        }
818
819
        // throw an exception if the root category is NOT available
820
        throw new \Exception(sprintf('Root category for %s is not available', $storeViewCode));
821
    }
822
823
    /**
824
     * Persist's the passed product data and return's the ID.
825
     *
826
     * @param array $product The product data to persist
827
     *
828
     * @return string The ID of the persisted entity
829
     */
830
    public function persistProduct($product)
831
    {
832
        return $this->getProductProcessor()->persistProduct($product);
833
    }
834
835
    /**
836
     * Persist's the passed product varchar attribute.
837
     *
838
     * @param array $attribute The attribute to persist
839
     *
840
     * @return void
841
     */
842
    public function persistProductVarcharAttribute($attribute)
843
    {
844
        $this->getProductProcessor()->persistProductVarcharAttribute($attribute);
845
    }
846
847
    /**
848
     * Persist's the passed product integer attribute.
849
     *
850
     * @param array $attribute The attribute to persist
851
     *
852
     * @return void
853
     */
854
    public function persistProductIntAttribute($attribute)
855
    {
856
        $this->getProductProcessor()->persistProductIntAttribute($attribute);
857
    }
858
859
    /**
860
     * Persist's the passed product decimal attribute.
861
     *
862
     * @param array $attribute The attribute to persist
863
     *
864
     * @return void
865
     */
866
    public function persistProductDecimalAttribute($attribute)
867
    {
868
        $this->getProductProcessor()->persistProductDecimalAttribute($attribute);
869
    }
870
871
    /**
872
     * Persist's the passed product datetime attribute.
873
     *
874
     * @param array $attribute The attribute to persist
875
     *
876
     * @return void
877
     */
878
    public function persistProductDatetimeAttribute($attribute)
879
    {
880
        $this->getProductProcessor()->persistProductDatetimeAttribute($attribute);
881
    }
882
883
    /**
884
     * Persist's the passed product text attribute.
885
     *
886
     * @param array $attribute The attribute to persist
887
     *
888
     * @return void
889
     */
890
    public function persistProductTextAttribute($attribute)
891
    {
892
        $this->getProductProcessor()->persistProductTextAttribute($attribute);
893
    }
894
895
    /**
896
     * Persist's the passed product website data and return's the ID.
897
     *
898
     * @param array $productWebsite The product website data to persist
899
     *
900
     * @return void
901
     */
902
    public function persistProductWebsite($productWebsite)
903
    {
904
        $this->getProductProcessor()->persistProductWebsite($productWebsite);
905
    }
906
907
    /**
908
     * Persist's the passed product category data and return's the ID.
909
     *
910
     * @param array $productCategory The product category data to persist
911
     *
912
     * @return void
913
     */
914
    public function persistProductCategory($productCategory)
915
    {
916
        $this->getProductProcessor()->persistProductCategory($productCategory);
917
    }
918
919
    /**
920
     * Persist's the passed stock item data and return's the ID.
921
     *
922
     * @param array $stockItem The stock item data to persist
923
     *
924
     * @return void
925
     */
926
    public function persistStockItem($stockItem)
927
    {
928
        $this->getProductProcessor()->persistStockItem($stockItem);
929
    }
930
931
    /**
932
     * Persist's the passed stock status data and return's the ID.
933
     *
934
     * @param array $stockStatus The stock status data to persist
935
     *
936
     * @return void
937
     */
938
    public function persistStockStatus($stockStatus)
939
    {
940
        $this->getProductProcessor()->persistStockStatus($stockStatus);
941
    }
942
943
    /**
944
     * Persist's the URL write with the passed data.
945
     *
946
     * @param array $row The URL rewrite to persist
947
     *
948
     * @return void
949
     */
950 1
    public function persistUrlRewrite($row)
951
    {
952 1
        $this->getProductProcessor()->persistUrlRewrite($row);
953 1
    }
954
955
    /**
956
     * Remove's the entity with the passed attributes.
957
     *
958
     * @param array       $row  The attributes of the entity to remove
959
     * @param string|null $name The name of the prepared statement that has to be executed
960
     *
961
     * @return void
962
     */
963
    public function removeProduct($row, $name = null)
964
    {
965
        $this->getProductProcessor()->removeProduct($row, $name);
966
    }
967
968
    /**
969
     * Delete's the URL rewrite(s) with the passed attributes.
970
     *
971
     * @param array       $row  The attributes of the entity to remove
972
     * @param string|null $name The name of the prepared statement that has to be executed
973
     *
974
     * @return void
975
     */
976 1
    public function removeUrlRewrite($row, $name = null)
977
    {
978 1
        $this->getProductProcessor()->removeUrlRewrite($row, $name);
979 1
    }
980
981
    /**
982
     * Delete's the stock item(s) with the passed attributes.
983
     *
984
     * @param array       $row  The attributes of the entity to remove
985
     * @param string|null $name The name of the prepared statement that has to be executed
986
     *
987
     * @return void
988
     */
989
    public function removeStockItem($row, $name = null)
990
    {
991
        $this->getProductProcessor()->removeStockItem($row, $name);
992
    }
993
994
    /**
995
     * Delete's the stock status with the passed attributes.
996
     *
997
     * @param array       $row  The attributes of the entity to remove
998
     * @param string|null $name The name of the prepared statement that has to be executed
999
     *
1000
     * @return void
1001
     */
1002
    public function removeStockStatus($row, $name = null)
1003
    {
1004
        $this->getProductProcessor()->removeStockStatus($row, $name);
1005
    }
1006
1007
    /**
1008
     * Delete's the product website relations with the passed attributes.
1009
     *
1010
     * @param array       $row  The attributes of the entity to remove
1011
     * @param string|null $name The name of the prepared statement that has to be executed
1012
     *
1013
     * @return void
1014
     */
1015
    public function removeProductWebsite($row, $name = null)
1016
    {
1017
        $this->getProductProcessor()->removeProductWebsite($row, $name);
1018
    }
1019
1020
    /**
1021
     * Delete's the product category relations with the passed attributes.
1022
     *
1023
     * @param array       $row  The attributes of the entity to remove
1024
     * @param string|null $name The name of the prepared statement that has to be executed
1025
     *
1026
     * @return void
1027
     */
1028
    public function removeProductCategory($row, $name = null)
1029
    {
1030
        $this->getProductProcessor()->removeProductCategory($row, $name);
1031
    }
1032
}
1033