Completed
Push — master ( 685540...ab50ec )
by
unknown
10:30
created

DefaultMagentoImportStrategy::findExistingEntity()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
c 4
b 3
f 0
dl 0
loc 38
rs 4.9091
cc 9
eloc 22
nc 12
nop 2
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\ImportExport\Strategy;
4
5
use Oro\Bundle\ImportExportBundle\Strategy\Import\ConfigurableAddOrReplaceStrategy;
6
use Doctrine\Common\Util\ClassUtils;
7
8
class DefaultMagentoImportStrategy extends ConfigurableAddOrReplaceStrategy
9
{
10
    const TARGET_TRAIT = 'OroCRM\Bundle\MagentoBundle\Entity\IntegrationEntityTrait';
11
12
    /**
13
     * {@inheritdoc}
14
     */
15
    protected function updateContextCounters($entity)
16
    {
17
        // increment context counter
18
        $identifier = $this->databaseHelper->getIdentifier($entity);
19
        if ($identifier) {
20
            $this->context->incrementUpdateCount();
21
        } else {
22
            $this->context->incrementAddCount();
23
        }
24
25
        return $entity;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function findExistingEntity($entity, array $searchContext = [])
32
    {
33
        $entityName = ClassUtils::getClass($entity);
34
        $identifier = $this->databaseHelper->getIdentifier($entity);
35
        $existingEntity = null;
36
37
        // find by identifier
38
        if ($identifier) {
39
            $existingEntity = $this->databaseHelper->find($entityName, $identifier);
40
        }
41
42
        // find by identity fields
43
        if (!$existingEntity
44
            && (!$searchContext || $this->databaseHelper->getIdentifier(current($searchContext)))
0 ignored issues
show
Bug Best Practice introduced by
The expression $searchContext of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
45
        ) {
46
            $identityValues = $searchContext;
47
48
            $usedTraits = class_uses($entityName);
49
            if ($this->context->hasOption('channel') && in_array(self::TARGET_TRAIT, $usedTraits)) {
50
                $channel = $this->databaseHelper->findOneBy(
51
                    'Oro\Bundle\IntegrationBundle\Entity\Channel',
52
                    ['id' => $this->context->getOption('channel')]
53
                );
54
                $identityValues['channel'] = $channel;
55
            }
56
57
            $identityValues += $this->fieldHelper->getIdentityValues($entity);
58
            $existingEntity = $this->findEntityByIdentityValues($entityName, $identityValues);
59
        }
60
61
        if ($existingEntity && !$identifier) {
62
            $identifier = $this->databaseHelper->getIdentifier($existingEntity);
63
            $identifierName = $this->databaseHelper->getIdentifierFieldName($entity);
64
            $this->fieldHelper->setObjectValue($entity, $identifierName, $identifier);
65
        }
66
67
        return $existingEntity;
68
    }
69
}
70