Completed
Pull Request — master (#35)
by Tim
03:16
created

ImportProcessor::getEavAttributeRepository()   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 repository to access categories.
47
     *
48
     * @var \TechDivision\Import\Repositories\CategoryRepository
49
     */
50
    protected $categoryRepository;
51
52
    /**
53
     * The repository to access category varchar values.
54
     *
55
     * @var \TechDivision\Import\Repositories\CategoryVarcharRepository
56
     */
57
    protected $categoryVarcharRepository;
58
59
    /**
60
     * The repository to access EAV attributes.
61
     *
62
     * @var \TechDivision\Import\Repositories\EavAttributeRepository
63
     */
64
    protected $eavAttributeRepository;
65
66
    /**
67
     * The repository to access EAV attribute set.
68
     *
69
     * @var \TechDivision\Import\Repositories\EavAttributeSetRepository
70
     */
71
    protected $eavAttributeSetRepository;
72
73
    /**
74
     * The repository to access stores.
75
     *
76
     * @var \TechDivision\Import\Repositories\StoreRepository
77
     */
78
    protected $storeRepository;
79
80
    /**
81
     * The repository to access store websites.
82
     *
83
     * @var \TechDivision\Import\Repositories\StoreWebsiteRepository
84
     */
85
    protected $storeWebsiteRepository;
86
87
    /**
88
     * The repository to access tax classes.
89
     *
90
     * @var \TechDivision\Import\Repositories\TaxClassRepository
91
     */
92
    protected $taxClassRepository;
93
94
    /**
95
     * The repository to access link types.
96
     *
97
     * @var \TechDivision\Import\Repositories\LinkTypeRepository
98
     */
99
    protected $linkTypeRepository;
100
101
    /**
102
     * The repository to access link attributes.
103
     *
104
     * @var \TechDivision\Import\Repositories\LinkAttributeRepository
105
     */
106
    protected $linkAttributeRepository;
107
108
    /**
109
     * The repository to access the configuration.
110
     *
111
     * @var \TechDivision\Import\Repositories\CoreConfigDataRepository
112
     */
113
    protected $coreConfigDataRepository;
114
115
    /**
116
     * Set's the passed connection.
117
     *
118
     * @param \PDO $connection The connection to set
119
     *
120
     * @return void
121
     */
122
    public function setConnection(\PDO $connection)
123
    {
124
        $this->connection = $connection;
125
    }
126
127
    /**
128
     * Return's the connection.
129
     *
130
     * @return \PDO The connection instance
131
     */
132
    public function getConnection()
133
    {
134
        return $this->connection;
135
    }
136
137
    /**
138
     * Turns off autocommit mode. While autocommit mode is turned off, changes made to the database via the PDO
139
     * object instance are not committed until you end the transaction by calling ProductProcessor::commit().
140
     * Calling ProductProcessor::rollBack() will roll back all changes to the database and return the connection
141
     * to autocommit mode.
142
     *
143
     * @return boolean Returns TRUE on success or FALSE on failure
144
     * @link http://php.net/manual/en/pdo.begintransaction.php
145
     */
146
    public function beginTransaction()
147
    {
148
        return $this->connection->beginTransaction();
149
    }
150
151
    /**
152
     * Commits a transaction, returning the database connection to autocommit mode until the next call to
153
     * ProductProcessor::beginTransaction() starts a new transaction.
154
     *
155
     * @return boolean Returns TRUE on success or FALSE on failure
156
     * @link http://php.net/manual/en/pdo.commit.php
157
     */
158
    public function commit()
159
    {
160
        return $this->connection->commit();
161
    }
162
163
    /**
164
     * Rolls back the current transaction, as initiated by ProductProcessor::beginTransaction().
165
     *
166
     * If the database was set to autocommit mode, this function will restore autocommit mode after it has
167
     * rolled back the transaction.
168
     *
169
     * Some databases, including MySQL, automatically issue an implicit COMMIT when a database definition
170
     * language (DDL) statement such as DROP TABLE or CREATE TABLE is issued within a transaction. The implicit
171
     * COMMIT will prevent you from rolling back any other changes within the transaction boundary.
172
     *
173
     * @return boolean Returns TRUE on success or FALSE on failure
174
     * @link http://php.net/manual/en/pdo.rollback.php
175
     */
176
    public function rollBack()
177
    {
178
        return $this->connection->rollBack();
179
    }
180
181
    /**
182
     * Set's the repository to access categories.
183
     *
184
     * @param \TechDivision\Import\Repositories\CategoryRepository $categoryRepository The repository to access categories
185
     *
186
     * @return void
187
     */
188
    public function setCategoryRepository($categoryRepository)
189
    {
190
        $this->categoryRepository = $categoryRepository;
191
    }
192
193
    /**
194
     * Return's the repository to access categories.
195
     *
196
     * @return \TechDivision\Import\Repositories\CategoryRepository The repository instance
197
     */
198
    public function getCategoryRepository()
199
    {
200
        return $this->categoryRepository;
201
    }
202
203
    /**
204
     * Return's the repository to access category varchar values.
205
     *
206
     * @param \TechDivision\Import\Repositories\CategoryVarcharRepository $categoryVarcharRepository The repository instance
207
     *
208
     * @return void
209
     */
210
    public function setCategoryVarcharRepository($categoryVarcharRepository)
211
    {
212
        $this->categoryVarcharRepository = $categoryVarcharRepository;
213
    }
214
215
    /**
216
     * Return's the repository to access category varchar values.
217
     *
218
     * @return \TechDivision\Import\Repositories\CategoryVarcharRepository The repository instance
219
     */
220
    public function getCategoryVarcharRepository()
221
    {
222
        return $this->categoryVarcharRepository;
223
    }
224
225
    /**
226
     * Set's the repository to access EAV attributes.
227
     *
228
     * @param \TechDivision\Import\Repositories\EavAttributeRepository $eavAttributeRepository The repository to access EAV attributes
229
     *
230
     * @return void
231
     */
232
    public function setEavAttributeRepository($eavAttributeRepository)
233
    {
234
        $this->eavAttributeRepository = $eavAttributeRepository;
235
    }
236
237
    /**
238
     * Return's the repository to access EAV attributes.
239
     *
240
     * @return \TechDivision\Import\Repositories\EavAttributeRepository The repository instance
241
     */
242
    public function getEavAttributeRepository()
243
    {
244
        return $this->eavAttributeRepository;
245
    }
246
247
    /**
248
     * Set's the repository to access EAV attribute sets.
249
     *
250
     * @param \TechDivision\Import\Repositories\EavAttributeSetRepository $eavAttributeSetRepository The repository the access EAV attribute sets
251
     *
252
     * @return void
253
     */
254
    public function setEavAttributeSetRepository($eavAttributeSetRepository)
255
    {
256
        $this->eavAttributeSetRepository = $eavAttributeSetRepository;
257
    }
258
259
    /**
260
     * Return's the repository to access EAV attribute sets.
261
     *
262
     * @return \TechDivision\Import\Repositories\EavAttributeSetRepository The repository instance
263
     */
264
    public function getEavAttributeSetRepository()
265
    {
266
        return $this->eavAttributeSetRepository;
267
    }
268
269
    /**
270
     * Set's the repository to access stores.
271
     *
272
     * @param \TechDivision\Import\Repositories\StoreRepository $storeRepository The repository the access stores
273
     *
274
     * @return void
275
     */
276
    public function setStoreRepository($storeRepository)
277
    {
278
        $this->storeRepository = $storeRepository;
279
    }
280
281
    /**
282
     * Return's the repository to access stores.
283
     *
284
     * @return \TechDivision\Import\Repositories\StoreRepository The repository instance
285
     */
286
    public function getStoreRepository()
287
    {
288
        return $this->storeRepository;
289
    }
290
291
    /**
292
     * Set's the repository to access store websites.
293
     *
294
     * @param \TechDivision\Import\Repositories\StoreWebsiteRepository $storeWebsiteRepository The repository the access store websites
295
     *
296
     * @return void
297
     */
298
    public function setStoreWebsiteRepository($storeWebsiteRepository)
299
    {
300
        $this->storeWebsiteRepository = $storeWebsiteRepository;
301
    }
302
303
    /**
304
     * Return's the repository to access store websites.
305
     *
306
     * @return \TechDivision\Import\Repositories\StoreWebsiteRepository The repository instance
307
     */
308
    public function getStoreWebsiteRepository()
309
    {
310
        return $this->storeWebsiteRepository;
311
    }
312
313
    /**
314
     * Set's the repository to access tax classes.
315
     *
316
     * @param \TechDivision\Import\Repositories\TaxClassRepository $taxClassRepository The repository the access stores
317
     *
318
     * @return void
319
     */
320
    public function setTaxClassRepository($taxClassRepository)
321
    {
322
        $this->taxClassRepository = $taxClassRepository;
323
    }
324
325
    /**
326
     * Return's the repository to access tax classes.
327
     *
328
     * @return \TechDivision\Import\Repositories\TaxClassRepository The repository instance
329
     */
330
    public function getTaxClassRepository()
331
    {
332
        return $this->taxClassRepository;
333
    }
334
335
    /**
336
     * Set's the repository to access link types.
337
     *
338
     * @param \TechDivision\Import\Repositories\LinkTypeRepository $linkTypeRepository The repository to access link types
339
     *
340
     * @return void
341
     */
342
    public function setLinkTypeRepository($linkTypeRepository)
343
    {
344
        $this->linkTypeRepository = $linkTypeRepository;
345
    }
346
347
    /**
348
     * Return's the repository to access link types.
349
     *
350
     * @return \TechDivision\Import\Repositories\LinkTypeRepository The repository instance
351
     */
352
    public function getLinkTypeRepository()
353
    {
354
        return $this->linkTypeRepository;
355
    }
356
357
    /**
358
     * Set's the repository to access link attributes.
359
     *
360
     * @param \TechDivision\Import\Repositories\LinkTypeRepository $linkAttributeRepository The repository to access link attributes
361
     *
362
     * @return void
363
     */
364
    public function setLinkAttributeRepository($linkAttributeRepository)
365
    {
366
        $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...
367
    }
368
369
    /**
370
     * Return's the repository to access link attributes.
371
     *
372
     * @return \TechDivision\Import\Repositories\LinkTypeRepository The repository instance
373
     */
374
    public function getLinkAttributeRepository()
375
    {
376
        return $this->linkAttributeRepository;
377
    }
378
379
    /**
380
     * Set's the repository to access the Magento 2 configuration.
381
     *
382
     * @param \TechDivision\Import\Repositories\CoreConfigDataRepository $coreConfigDataRepository The repository to access the Magento 2 configuration
383
     *
384
     * @return void
385
     */
386
    public function setCoreConfigDataRepository($coreConfigDataRepository)
387
    {
388
        $this->coreConfigDataRepository = $coreConfigDataRepository;
389
    }
390
391
    /**
392
     * Return's the repository to access the Magento 2 configuration.
393
     *
394
     * @return \TechDivision\Import\Repositories\CoreConfigDataRepository The repository instance
395
     */
396
    public function getCoreConfigDataRepository()
397
    {
398
        return $this->coreConfigDataRepository;
399
    }
400
401
    /**
402
     * Return's the EAV attribute set with the passed ID.
403
     *
404
     * @param integer $id The ID of the EAV attribute set to load
405
     *
406
     * @return array The EAV attribute set
407
     */
408
    public function getEavAttributeSet($id)
409
    {
410
        return $this->getEavAttributeSetRepository()->load($id);
411
    }
412
413
    /**
414
     * Return's the attribute sets for the passed entity type ID.
415
     *
416
     * @param mixed $entityTypeId The entity type ID to return the attribute sets for
417
     *
418
     * @return array|boolean The attribute sets for the passed entity type ID
419
     */
420
    public function getEavAttributeSetsByEntityTypeId($entityTypeId)
421
    {
422
        return $this->getEavAttributeSetRepository()->findAllByEntityTypeId($entityTypeId);
423
    }
424
425
    /**
426
     * Return's an array with the EAV attributes for the passed entity type ID and attribute set name.
427
     *
428
     * @param integer $entityTypeId     The entity type ID of the EAV attributes to return
429
     * @param string  $attributeSetName The attribute set name of the EAV attributes to return
430
     *
431
     * @return array The
432
     */
433
    public function getEavAttributesByEntityTypeIdAndAttributeSetName($entityTypeId, $attributeSetName)
434
    {
435
        return $this->getEavAttributeRepository()->findAllByEntityTypeIdAndAttributeSetName($entityTypeId, $attributeSetName);
436
    }
437
438
    /**
439
     * Return's an array with the available EAV attributes for the passed option value and store ID.
440
     *
441
     * @param string $optionValue The option value of the EAV attributes
442
     * @param string $storeId     The store ID of the EAV attribues
443
     *
444
     * @return array The array with all available EAV attributes
445
     */
446
    public function getEavAttributesByOptionValueAndStoreId($optionValue, $storeId)
447
    {
448
        return $this->getEavAttributeRepository()->findAllByOptionValueAndStoreId($optionValue, $storeId);
449
    }
450
451
    /**
452
     * Return's the first EAV attribute for the passed option value and store ID.
453
     *
454
     * @param string $optionValue The option value of the EAV attributes
455
     * @param string $storeId     The store ID of the EAV attribues
456
     *
457
     * @return array The array with the EAV attribute
458
     */
459
    public function getEavAttributeByOptionValueAndStoreId($optionValue, $storeId)
460
    {
461
        return $this->getEavAttributeRepository()->findOneByOptionValueAndStoreId($optionValue, $storeId);
462
    }
463
464
    /**
465
     * Return's an array with the available EAV attributes for the passed is user defined flag.
466
     *
467
     * @param integer $isUserDefined The flag itself
468
     *
469
     * @return array The array with the EAV attributes matching the passed flag
470
     */
471
    public function getEavAttributeByIsUserDefined($isUserDefined = 1)
472
    {
473
        return $this->getEavAttributeRepository()->findAllByIsUserDefined($isUserDefined);
474
    }
475
476
    /**
477
     * Return's an array with the available stores.
478
     *
479
     * @return array The array with the available stores
480
     */
481
    public function getStores()
482
    {
483
        return $this->getStoreRepository()->findAll();
484
    }
485
486
    /**
487
     * Return's the default store.
488
     *
489
     * @return array The default store
490
     */
491
    public function getDefaultStore()
492
    {
493
        return $this->getStoreRepository()->findOneByDefault();
494
    }
495
496
    /**
497
     * Return's an array with the available store websites.
498
     *
499
     * @return array The array with the available store websites
500
     */
501
    public function getStoreWebsites()
502
    {
503
        return $this->getStoreWebsiteRepository()->findAll();
504
    }
505
506
    /**
507
     * Return's an array with the available tax classes.
508
     *
509
     * @return array The array with the available tax classes
510
     */
511
    public function getTaxClasses()
512
    {
513
        return $this->getTaxClassRepository()->findAll();
514
    }
515
516
    /**
517
     * Return's an array with all available categories.
518
     *
519
     * @return array The available categories
520
     */
521
    public function getCategories()
522
    {
523
        return $this->getCategoryRepository()->findAll();
524
    }
525
526
    /**
527
     * Return's an array with the root categories with the store code as key.
528
     *
529
     * @return array The root categories
530
     */
531
    public function getRootCategories()
532
    {
533
        return $this->getCategoryRepository()->findAllRootCategories();
534
    }
535
536
    /**
537
     * Returns the category varchar values for the categories with
538
     * the passed with the passed entity IDs.
539
     *
540
     * @param array $entityIds The array with the category IDs
541
     *
542
     * @return mixed The category varchar values
543
     */
544
    public function getCategoryVarcharsByEntityIds(array $entityIds)
545
    {
546
        return $this->getCategoryVarcharRepository()->findAllByEntityIds($entityIds);
547
    }
548
549
    /**
550
     * Return's an array with all available link types.
551
     *
552
     * @return array The available link types
553
     */
554
    public function getLinkTypes()
555
    {
556
        return $this->getLinkTypeRepository()->findAll();
557
    }
558
559
    /**
560
     * Return's an array with all available link attributes.
561
     *
562
     * @return array The available link attributes
563
     */
564
    public function getLinkAttributes()
565
    {
566
        return $this->getLinkAttributeRepository()->findAll();
567
    }
568
569
    /**
570
     * Return's an array with the Magento 2 configuration.
571
     *
572
     * @return array The Magento 2 configuration
573
     */
574
    public function getCoreConfigData()
575
    {
576
        return $this->getCoreConfigDataRepository()->findAll();
577
    }
578
579
    /**
580
     * Returns the array with the global data necessary for the
581
     * import process.
582
     *
583
     * @return array The array with the global data
584
     */
585
    public function getGlobalData()
586
    {
587
588
        // initialize the array for the global data
589
        $globalData = array();
590
591
        // initialize the global data
592
        $globalData[RegistryKeys::STORES] = $this->getStores();
593
        $globalData[RegistryKeys::LINK_TYPES] = $this->getLinkTypes();
594
        $globalData[RegistryKeys::TAX_CLASSES] = $this->getTaxClasses();
595
        $globalData[RegistryKeys::DEFAULT_STORE] = $this->getDefaultStore();
596
        $globalData[RegistryKeys::STORE_WEBSITES] = $this->getStoreWebsites();
597
        $globalData[RegistryKeys::LINK_ATTRIBUTES] = $this->getLinkAttributes();
598
        $globalData[RegistryKeys::ROOT_CATEGORIES] = $this->getRootCategories();
599
        $globalData[RegistryKeys::CORE_CONFIG_DATA] = $this->getCoreConfigData();
600
        $globalData[RegistryKeys::ATTRIBUTE_SETS] = $eavAttributeSets = $this->getEavAttributeSetsByEntityTypeId(4);
601
602
        // prepare the categories
603
        $categories = array();
604
        foreach ($this->getCategories() as $category) {
605
            // expload the entity IDs from the category path
606
            $entityIds = explode('/', $category[MemberNames::PATH]);
607
608
            // cut-off the root category
609
            array_shift($entityIds);
610
611
            // continue with the next category if no entity IDs are available
612
            if (sizeof($entityIds) === 0) {
613
                continue;
614
            }
615
616
            // initialize the array for the path elements
617
            $path = array();
618
            foreach ($this->getCategoryVarcharsByEntityIds($entityIds) as $cat) {
619
                $path[] = $cat[MemberNames::VALUE];
620
            }
621
622
            // append the catogory with the string path as key
623
            $categories[implode('/', $path)] = $category;
624
        }
625
626
        // initialize the array with the categories
627
        $globalData[RegistryKeys::CATEGORIES] = $categories;
628
629
        // prepare an array with the EAV attributes grouped by their attribute set name as keys
630
        $eavAttributes = array();
631
        foreach (array_keys($eavAttributeSets) as $eavAttributeSetName) {
632
            $eavAttributes[$eavAttributeSetName] = $this->getEavAttributesByEntityTypeIdAndAttributeSetName(4, $eavAttributeSetName);
633
        }
634
635
        // initialize the array with the EAV attributes
636
        $globalData[RegistryKeys::EAV_ATTRIBUTES] = $eavAttributes;
637
638
        // return the array
639
        return $globalData;
640
    }
641
}
642