|
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))) |
|
|
|
|
|
|
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
|
|
|
|
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.