Completed
Pull Request — master (#37)
by Tim
03:54
created

ImportProcessor::getTaxClassRepository()   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 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Services\ImportProcessor
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Services;
22
23
use TechDivision\Import\Utils\RegistryKeys;
24
use TechDivision\Import\Utils\MemberNames;
25
26
/**
27
 * Processor implementation to load global data.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
class ImportProcessor implements ImportProcessorInterface
36
{
37
38
    /**
39
     * A PDO connection initialized with the values from the Doctrine EntityManager.
40
     *
41
     * @var \PDO
42
     */
43
    protected $connection;
44
45
    /**
46
     * The category assembler instance.
47
     *
48
     * @var \TechDivision\Import\Assembler\CategoryAssembler
49
     */
50
    protected $categoryAssembler;
51
52
    /**
53
     * The repository to access categories.
54
     *
55
     * @var \TechDivision\Import\Repositories\CategoryRepository
56
     */
57
    protected $categoryRepository;
58
59
    /**
60
     * The repository to access category varchar values.
61
     *
62
     * @var \TechDivision\Import\Repositories\CategoryVarcharRepository
63
     */
64
    protected $categoryVarcharRepository;
65
66
    /**
67
     * The repository to access EAV attributes.
68
     *
69
     * @var \TechDivision\Import\Repositories\EavAttributeRepository
70
     */
71
    protected $eavAttributeRepository;
72
73
    /**
74
     * The repository to access EAV attribute set.
75
     *
76
     * @var \TechDivision\Import\Repositories\EavAttributeSetRepository
77
     */
78
    protected $eavAttributeSetRepository;
79
80
    /**
81
     * The repository to access EAV entity types.
82
     *
83
     * @var \TechDivision\Import\Repositories\EavEntityTypeRepository
84
     */
85
    protected $eavEntityTypeRepository;
86
87
    /**
88
     * The repository to access stores.
89
     *
90
     * @var \TechDivision\Import\Repositories\StoreRepository
91
     */
92
    protected $storeRepository;
93
94
    /**
95
     * The repository to access store websites.
96
     *
97
     * @var \TechDivision\Import\Repositories\StoreWebsiteRepository
98
     */
99
    protected $storeWebsiteRepository;
100
101
    /**
102
     * The repository to access tax classes.
103
     *
104
     * @var \TechDivision\Import\Repositories\TaxClassRepository
105
     */
106
    protected $taxClassRepository;
107
108
    /**
109
     * The repository to access link types.
110
     *
111
     * @var \TechDivision\Import\Repositories\LinkTypeRepository
112
     */
113
    protected $linkTypeRepository;
114
115
    /**
116
     * The repository to access link attributes.
117
     *
118
     * @var \TechDivision\Import\Repositories\LinkAttributeRepository
119
     */
120
    protected $linkAttributeRepository;
121
122
    /**
123
     * The repository to access the configuration.
124
     *
125
     * @var \TechDivision\Import\Repositories\CoreConfigDataRepository
126
     */
127
    protected $coreConfigDataRepository;
128
129
    /**
130
     * Set's the passed connection.
131
     *
132
     * @param \PDO $connection The connection to set
133
     *
134
     * @return void
135
     */
136
    public function setConnection(\PDO $connection)
137
    {
138
        $this->connection = $connection;
139
    }
140
141
    /**
142
     * Return's the connection.
143
     *
144
     * @return \PDO The connection instance
145
     */
146
    public function getConnection()
147
    {
148
        return $this->connection;
149
    }
150
151
    /**
152
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
153
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
154
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
155
     * to autocommit mode.
156
     *
157
     * @return boolean Returns TRUE on success or FALSE on failure
158
     * @link http://php.net/manual/en/pdo.begintransaction.php
159
     */
160
    public function beginTransaction()
161
    {
162
        return $this->connection->beginTransaction();
163
    }
164
165
    /**
166
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
167
     * ProductProcessor::beginTransaction() starts a new transaction.
168
     *
169
     * @return boolean Returns TRUE on success or FALSE on failure
170
     * @link http://php.net/manual/en/pdo.commit.php
171
     */
172
    public function commit()
173
    {
174
        return $this->connection->commit();
175
    }
176
177
    /**
178
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
179
     *
180
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
181
     * rolled back the transaction.
182
     *
183
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
184
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
185
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
186
     *
187
     * @return boolean Returns TRUE on success or FALSE on failure
188
     * @link http://php.net/manual/en/pdo.rollback.php
189
     */
190
    public function rollBack()
191
    {
192
        return $this->connection->rollBack();
193
    }
194
195
    /**
196
     * Set's the category assembler.
197
     *
198
     * @param \TechDivision\Import\Assembler\CategoryAssembler $categoryAssembler The category assembler
199
     *
200
     * @return void
201
     */
202
    public function setCategoryAssembler($categoryAssembler)
203
    {
204
        $this->categoryAssembler = $categoryAssembler;
205
    }
206
207
    /**
208
     * Return's the category assembler.
209
     *
210
     * @return \TechDivision\Import\Assembler\CategoryAssembler The category assembler instance
211
     */
212
    public function getCategoryAssembler()
213
    {
214
        return $this->categoryAssembler;
215
    }
216
217
    /**
218
     * Set's the repository to access categories.
219
     *
220
     * @param \TechDivision\Import\Repositories\CategoryRepository $categoryRepository The repository to access categories
221
     *
222
     * @return void
223
     */
224
    public function setCategoryRepository($categoryRepository)
225
    {
226
        $this->categoryRepository = $categoryRepository;
227
    }
228
229
    /**
230
     * Return's the repository to access categories.
231
     *
232
     * @return \TechDivision\Import\Repositories\CategoryRepository The repository instance
233
     */
234
    public function getCategoryRepository()
235
    {
236
        return $this->categoryRepository;
237
    }
238
239
    /**
240
     * Return's the repository to access category varchar values.
241
     *
242
     * @param \TechDivision\Import\Repositories\CategoryVarcharRepository $categoryVarcharRepository The repository instance
243
     *
244
     * @return void
245
     */
246
    public function setCategoryVarcharRepository($categoryVarcharRepository)
247
    {
248
        $this->categoryVarcharRepository = $categoryVarcharRepository;
249
    }
250
251
    /**
252
     * Return's the repository to access category varchar values.
253
     *
254
     * @return \TechDivision\Import\Repositories\CategoryVarcharRepository The repository instance
255
     */
256
    public function getCategoryVarcharRepository()
257
    {
258
        return $this->categoryVarcharRepository;
259
    }
260
261
    /**
262
     * Set's the repository to access EAV attributes.
263
     *
264
     * @param \TechDivision\Import\Repositories\EavAttributeRepository $eavAttributeRepository The repository to access EAV attributes
265
     *
266
     * @return void
267
     */
268
    public function setEavAttributeRepository($eavAttributeRepository)
269
    {
270
        $this->eavAttributeRepository = $eavAttributeRepository;
271
    }
272
273
    /**
274
     * Return's the repository to access EAV attributes.
275
     *
276
     * @return \TechDivision\Import\Repositories\EavAttributeRepository The repository instance
277
     */
278
    public function getEavAttributeRepository()
279
    {
280
        return $this->eavAttributeRepository;
281
    }
282
283
    /**
284
     * Set's the repository to access EAV attribute sets.
285
     *
286
     * @param \TechDivision\Import\Repositories\EavAttributeSetRepository $eavAttributeSetRepository The repository the access EAV attribute sets
287
     *
288
     * @return void
289
     */
290
    public function setEavAttributeSetRepository($eavAttributeSetRepository)
291
    {
292
        $this->eavAttributeSetRepository = $eavAttributeSetRepository;
293
    }
294
295
    /**
296
     * Return's the repository to access EAV attribute sets.
297
     *
298
     * @return \TechDivision\Import\Repositories\EavAttributeSetRepository The repository instance
299
     */
300
    public function getEavAttributeSetRepository()
301
    {
302
        return $this->eavAttributeSetRepository;
303
    }
304
305
    /**
306
     * Return's the repository to access EAV entity types.
307
     *
308
     * @return \TechDivision\Import\Repositories\EavEntityTypeRepository The repository instance
309
     */
310
    public function getEavEntityTypeRepository()
311
    {
312
        return $this->eavEntityTypeRepository;
313
    }
314
315
    /**
316
     * Set's the repository to access EAV entity types.
317
     *
318
     * @param \TechDivision\Import\Repositories\EavEntityTypeRepository $eavEntityTypeRepository The repository the access EAV entity types
319
     *
320
     * @return void
321
     */
322
    public function setEavEntityTypeRepository($eavEntityTypeRepository)
323
    {
324
        $this->eavEntityTypeRepository = $eavEntityTypeRepository;
325
    }
326
327
    /**
328
     * Set's the repository to access stores.
329
     *
330
     * @param \TechDivision\Import\Repositories\StoreRepository $storeRepository The repository the access stores
331
     *
332
     * @return void
333
     */
334
    public function setStoreRepository($storeRepository)
335
    {
336
        $this->storeRepository = $storeRepository;
337
    }
338
339
    /**
340
     * Return's the repository to access stores.
341
     *
342
     * @return \TechDivision\Import\Repositories\StoreRepository The repository instance
343
     */
344
    public function getStoreRepository()
345
    {
346
        return $this->storeRepository;
347
    }
348
349
    /**
350
     * Set's the repository to access store websites.
351
     *
352
     * @param \TechDivision\Import\Repositories\StoreWebsiteRepository $storeWebsiteRepository The repository the access store websites
353
     *
354
     * @return void
355
     */
356
    public function setStoreWebsiteRepository($storeWebsiteRepository)
357
    {
358
        $this->storeWebsiteRepository = $storeWebsiteRepository;
359
    }
360
361
    /**
362
     * Return's the repository to access store websites.
363
     *
364
     * @return \TechDivision\Import\Repositories\StoreWebsiteRepository The repository instance
365
     */
366
    public function getStoreWebsiteRepository()
367
    {
368
        return $this->storeWebsiteRepository;
369
    }
370
371
    /**
372
     * Set's the repository to access tax classes.
373
     *
374
     * @param \TechDivision\Import\Repositories\TaxClassRepository $taxClassRepository The repository the access stores
375
     *
376
     * @return void
377
     */
378
    public function setTaxClassRepository($taxClassRepository)
379
    {
380
        $this->taxClassRepository = $taxClassRepository;
381
    }
382
383
    /**
384
     * Return's the repository to access tax classes.
385
     *
386
     * @return \TechDivision\Import\Repositories\TaxClassRepository The repository instance
387
     */
388
    public function getTaxClassRepository()
389
    {
390
        return $this->taxClassRepository;
391
    }
392
393
    /**
394
     * Set's the repository to access link types.
395
     *
396
     * @param \TechDivision\Import\Repositories\LinkTypeRepository $linkTypeRepository The repository to access link types
397
     *
398
     * @return void
399
     */
400
    public function setLinkTypeRepository($linkTypeRepository)
401
    {
402
        $this->linkTypeRepository = $linkTypeRepository;
403
    }
404
405
    /**
406
     * Return's the repository to access link types.
407
     *
408
     * @return \TechDivision\Import\Repositories\LinkTypeRepository The repository instance
409
     */
410
    public function getLinkTypeRepository()
411
    {
412
        return $this->linkTypeRepository;
413
    }
414
415
    /**
416
     * Set's the repository to access link attributes.
417
     *
418
     * @param \TechDivision\Import\Repositories\LinkTypeRepository $linkAttributeRepository The repository to access link attributes
419
     *
420
     * @return void
421
     */
422
    public function setLinkAttributeRepository($linkAttributeRepository)
423
    {
424
        $this->linkAttributeRepository = $linkAttributeRepository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $linkAttributeRepository of type object<TechDivision\Impo...ies\LinkTypeRepository> is incompatible with the declared type object<TechDivision\Impo...inkAttributeRepository> of property $linkAttributeRepository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
425
    }
426
427
    /**
428
     * Return's the repository to access link attributes.
429
     *
430
     * @return \TechDivision\Import\Repositories\LinkTypeRepository The repository instance
431
     */
432
    public function getLinkAttributeRepository()
433
    {
434
        return $this->linkAttributeRepository;
435
    }
436
437
    /**
438
     * Set's the repository to access the Magento 2 configuration.
439
     *
440
     * @param \TechDivision\Import\Repositories\CoreConfigDataRepository $coreConfigDataRepository The repository to access the Magento 2 configuration
441
     *
442
     * @return void
443
     */
444
    public function setCoreConfigDataRepository($coreConfigDataRepository)
445
    {
446
        $this->coreConfigDataRepository = $coreConfigDataRepository;
447
    }
448
449
    /**
450
     * Return's the repository to access the Magento 2 configuration.
451
     *
452
     * @return \TechDivision\Import\Repositories\CoreConfigDataRepository The repository instance
453
     */
454
    public function getCoreConfigDataRepository()
455
    {
456
        return $this->coreConfigDataRepository;
457
    }
458
459
    /**
460
     * Return's the EAV attribute set with the passed ID.
461
     *
462
     * @param integer $id The ID of the EAV attribute set to load
463
     *
464
     * @return array The EAV attribute set
465
     */
466
    public function getEavAttributeSet($id)
467
    {
468
        return $this->getEavAttributeSetRepository()->load($id);
469
    }
470
471
    /**
472
     * Return's the attribute sets for the passed entity type ID.
473
     *
474
     * @param mixed $entityTypeId The entity type ID to return the attribute sets for
475
     *
476
     * @return array|boolean The attribute sets for the passed entity type ID
477
     */
478
    public function getEavAttributeSetsByEntityTypeId($entityTypeId)
479
    {
480
        return $this->getEavAttributeSetRepository()->findAllByEntityTypeId($entityTypeId);
481
    }
482
483
    /**
484
     * Return's an array with the EAV attributes for the passed entity type ID and attribute set name.
485
     *
486
     * @param integer $entityTypeId     The entity type ID of the EAV attributes to return
487
     * @param string  $attributeSetName The attribute set name of the EAV attributes to return
488
     *
489
     * @return array The
490
     */
491
    public function getEavAttributesByEntityTypeIdAndAttributeSetName($entityTypeId, $attributeSetName)
492
    {
493
        return $this->getEavAttributeRepository()->findAllByEntityTypeIdAndAttributeSetName($entityTypeId, $attributeSetName);
494
    }
495
496
    /**
497
     * Return's an array with the available EAV attributes for the passed option value and store ID.
498
     *
499
     * @param string $optionValue The option value of the EAV attributes
500
     * @param string $storeId     The store ID of the EAV attribues
501
     *
502
     * @return array The array with all available EAV attributes
503
     */
504
    public function getEavAttributesByOptionValueAndStoreId($optionValue, $storeId)
505
    {
506
        return $this->getEavAttributeRepository()->findAllByOptionValueAndStoreId($optionValue, $storeId);
507
    }
508
509
    /**
510
     * Return's the first EAV attribute for the passed option value and store ID.
511
     *
512
     * @param string $optionValue The option value of the EAV attributes
513
     * @param string $storeId     The store ID of the EAV attribues
514
     *
515
     * @return array The array with the EAV attribute
516
     */
517
    public function getEavAttributeByOptionValueAndStoreId($optionValue, $storeId)
518
    {
519
        return $this->getEavAttributeRepository()->findOneByOptionValueAndStoreId($optionValue, $storeId);
520
    }
521
522
    /**
523
     * Return's an array with the available EAV attributes for the passed is user defined flag.
524
     *
525
     * @param integer $isUserDefined The flag itself
526
     *
527
     * @return array The array with the EAV attributes matching the passed flag
528
     */
529
    public function getEavAttributeByIsUserDefined($isUserDefined = 1)
530
    {
531
        return $this->getEavAttributeRepository()->findAllByIsUserDefined($isUserDefined);
532
    }
533
534
    /**
535
     * Return's an array with all available EAV entity types with the entity type code as key.
536
     *
537
     * @return array The available link types
538
     */
539
    public function getEavEntityTypes()
540
    {
541
        return $this->getEavEntityTypeRepository()->findAll();
542
    }
543
544
    /**
545
     * Return's an array with the available stores.
546
     *
547
     * @return array The array with the available stores
548
     */
549
    public function getStores()
550
    {
551
        return $this->getStoreRepository()->findAll();
552
    }
553
554
    /**
555
     * Return's the default store.
556
     *
557
     * @return array The default store
558
     */
559
    public function getDefaultStore()
560
    {
561
        return $this->getStoreRepository()->findOneByDefault();
562
    }
563
564
    /**
565
     * Return's an array with the available store websites.
566
     *
567
     * @return array The array with the available store websites
568
     */
569
    public function getStoreWebsites()
570
    {
571
        return $this->getStoreWebsiteRepository()->findAll();
572
    }
573
574
    /**
575
     * Return's an array with the available tax classes.
576
     *
577
     * @return array The array with the available tax classes
578
     */
579
    public function getTaxClasses()
580
    {
581
        return $this->getTaxClassRepository()->findAll();
582
    }
583
584
    /**
585
     * Return's an array with all available categories.
586
     *
587
     * @return array The available categories
588
     */
589
    public function getCategories()
590
    {
591
        return $this->getCategoryRepository()->findAll();
592
    }
593
594
    /**
595
     * Return's an array with the root categories with the store code as key.
596
     *
597
     * @return array The root categories
598
     */
599
    public function getRootCategories()
600
    {
601
        return $this->getCategoryRepository()->findAllRootCategories();
602
    }
603
604
    /**
605
     * Returns the category varchar values for the categories with
606
     * the passed with the passed entity IDs.
607
     *
608
     * @param array $entityIds The array with the category IDs
609
     *
610
     * @return mixed The category varchar values
611
     */
612
    public function getCategoryVarcharsByEntityIds(array $entityIds)
613
    {
614
        return $this->getCategoryVarcharRepository()->findAllByEntityIds($entityIds);
615
    }
616
617
    /**
618
     * Return's an array with all available link types.
619
     *
620
     * @return array The available link types
621
     */
622
    public function getLinkTypes()
623
    {
624
        return $this->getLinkTypeRepository()->findAll();
625
    }
626
627
    /**
628
     * Return's an array with all available link attributes.
629
     *
630
     * @return array The available link attributes
631
     */
632
    public function getLinkAttributes()
633
    {
634
        return $this->getLinkAttributeRepository()->findAll();
635
    }
636
637
    /**
638
     * Return's an array with the Magento 2 configuration.
639
     *
640
     * @return array The Magento 2 configuration
641
     */
642
    public function getCoreConfigData()
643
    {
644
        return $this->getCoreConfigDataRepository()->findAll();
645
    }
646
647
    /**
648
     * Returns the array with the global data necessary for the
649
     * import process.
650
     *
651
     * @return array The array with the global data
652
     */
653
    public function getGlobalData()
654
    {
655
656
        // initialize the array for the global data
657
        $globalData = array();
658
659
        // initialize the global data
660
        $globalData[RegistryKeys::STORES] = $this->getStores();
661
        $globalData[RegistryKeys::LINK_TYPES] = $this->getLinkTypes();
662
        $globalData[RegistryKeys::TAX_CLASSES] = $this->getTaxClasses();
663
        $globalData[RegistryKeys::DEFAULT_STORE] = $this->getDefaultStore();
664
        $globalData[RegistryKeys::STORE_WEBSITES] = $this->getStoreWebsites();
665
        $globalData[RegistryKeys::LINK_ATTRIBUTES] = $this->getLinkAttributes();
666
        $globalData[RegistryKeys::ROOT_CATEGORIES] = $this->getRootCategories();
667
        $globalData[RegistryKeys::CORE_CONFIG_DATA] = $this->getCoreConfigData();
668
        $globalData[RegistryKeys::ENTITY_TYPES] = $eavEntityTypes = $this->getEavEntityTypes();
669
670
        // prepare the attribute sets
671
        $eavAttributes = array();
672
        $eavAttributeSets = array();
673
        foreach ($eavEntityTypes as $eavEntityTypeCode => $eavEntityType) {
674
            // load the attribute sets for the entity type
675
            $attributeSets = $this->getEavAttributeSetsByEntityTypeId($entityTypeId = $eavEntityType[MemberNames::ENTITY_TYPE_ID]);
676
            // append the attribute sets to the array
677
            $eavAttributeSets[$eavEntityTypeCode] = $attributeSets;
678
679
            // iterate over the attribute sets and initialize the attributes
680
            foreach ($attributeSets as $attributeSet) {
0 ignored issues
show
Bug introduced by
The expression $attributeSets of type array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
681
                // load the attribute set name
682
                $eavAttributeSetName = $attributeSet[MemberNames::ATTRIBUTE_SET_NAME];
683
                // load the attributes for the attribute set
684
                $eavAttributes[$eavEntityTypeCode][$eavAttributeSetName] = $this->getEavAttributesByEntityTypeIdAndAttributeSetName(
685
                    $entityTypeId,
686
                    $eavAttributeSetName
687
                );
688
            }
689
        }
690
691
        // initialize the arrays with the EAV attributes and attribute sets
692
        $globalData[RegistryKeys::EAV_ATTRIBUTES] = $eavAttributes;
693
        $globalData[RegistryKeys::ATTRIBUTE_SETS] = $eavAttributeSets;
694
695
        // initialize the array with the avaliable categories
696
        $globalData[RegistryKeys::CATEGORIES] = $this->categoryAssembler->getCategoriesWithResolvedPath();
697
698
        // return the array
699
        return $globalData;
700
    }
701
}
702