1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\ImportExport\Strategy; |
4
|
|
|
|
5
|
|
|
use Oro\Bundle\AddressBundle\Entity\Region; |
6
|
|
|
|
7
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Address; |
8
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Customer; |
9
|
|
|
use OroCRM\Bundle\MagentoBundle\Provider\Reader\ContextCustomerReader; |
10
|
|
|
|
11
|
|
|
class CustomerStrategy extends AbstractImportStrategy |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Address[] |
15
|
|
|
*/ |
16
|
|
|
protected $importingAddresses = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $addressRegions = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Customer $entity |
25
|
|
|
* @return Customer |
26
|
|
|
*/ |
27
|
|
|
protected function beforeProcessEntity($entity) |
28
|
|
|
{ |
29
|
|
|
$this->importingAddresses = []; |
30
|
|
|
$this->addressRegions = []; |
31
|
|
|
$importingAddresses = $entity->getAddresses(); |
32
|
|
|
if ($importingAddresses) { |
33
|
|
|
foreach ($importingAddresses as $address) { |
34
|
|
|
if ($address->getSyncState() !== Address::SYNC_TO_MAGENTO) { |
35
|
|
|
$originId = $address->getOriginId(); |
36
|
|
|
$this->importingAddresses[$originId] = $address; |
37
|
|
|
|
38
|
|
|
if ($address->getRegion()) { |
39
|
|
|
$this->addressRegions[$originId] = $address->getRegion()->getCombinedCode(); |
40
|
|
|
} else { |
41
|
|
|
$this->addressRegions[$originId] = null; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return parent::beforeProcessEntity($entity); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Customer $entity |
52
|
|
|
* @return Customer |
53
|
|
|
*/ |
54
|
|
|
protected function afterProcessEntity($entity) |
55
|
|
|
{ |
56
|
|
|
$this->processAddresses($entity); |
57
|
|
|
|
58
|
|
|
$now = new \DateTime('now', new \DateTimeZone('UTC')); |
59
|
|
|
if (!$entity->getImportedAt()) { |
60
|
|
|
$entity->setImportedAt($now); |
61
|
|
|
} |
62
|
|
|
$entity->setSyncedAt($now); |
63
|
|
|
|
64
|
|
|
$this->appendDataToContext(ContextCustomerReader::CONTEXT_POST_PROCESS_CUSTOMERS, $entity->getOriginId()); |
65
|
|
|
|
66
|
|
|
return parent::afterProcessEntity($entity); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Customer $entity |
71
|
|
|
*/ |
72
|
|
|
protected function processAddresses(Customer $entity) |
73
|
|
|
{ |
74
|
|
|
if (!$entity->getAddresses()->isEmpty()) { |
75
|
|
|
/** @var Address $address */ |
76
|
|
|
foreach ($entity->getAddresses() as $address) { |
77
|
|
|
if ($address->getSyncState() !== Address::SYNC_TO_MAGENTO) { |
78
|
|
|
$originId = $address->getOriginId(); |
79
|
|
|
if (array_key_exists($originId, $this->importingAddresses)) { |
80
|
|
|
$remoteAddress = $this->importingAddresses[$originId]; |
81
|
|
|
$this->addressHelper->mergeAddressTypes($address, $remoteAddress); |
82
|
|
|
|
83
|
|
|
if (!empty($this->addressRegions[$originId]) && $address->getCountry()) { |
84
|
|
|
$this->addressHelper->updateRegionByMagentoRegionId( |
85
|
|
|
$address, |
86
|
|
|
$address->getCountry()->getIso2Code(), |
87
|
|
|
$this->addressRegions[$originId] |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
$address->setOwner($entity); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
protected function findExistingEntity($entity, array $searchContext = []) |
101
|
|
|
{ |
102
|
|
|
$existingEntity = null; |
|
|
|
|
103
|
|
|
|
104
|
|
|
if ($entity instanceof Customer) { |
105
|
|
|
$website = $this->databaseHelper->findOneBy( |
106
|
|
|
'OroCRM\Bundle\MagentoBundle\Entity\Website', |
107
|
|
|
[ |
108
|
|
|
'originId' => $entity->getWebsite()->getOriginId(), |
109
|
|
|
'channel' => $entity->getChannel() |
110
|
|
|
] |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
if ($website) { |
114
|
|
|
$searchContext['website'] = $website; |
115
|
|
|
} |
116
|
|
|
/** @var Customer $existingEntity */ |
117
|
|
|
$existingEntity = parent::findExistingEntity($entity, $searchContext); |
118
|
|
|
|
119
|
|
|
if (!$existingEntity) { |
120
|
|
|
$existingEntity = $this->databaseHelper->findOneBy( |
121
|
|
|
'OroCRM\Bundle\MagentoBundle\Entity\Customer', |
122
|
|
|
[ |
123
|
|
|
'email' => $entity->getEmail(), |
124
|
|
|
'channel' => $entity->getChannel(), |
125
|
|
|
'website' => $website |
126
|
|
|
] |
127
|
|
|
); |
128
|
|
|
if ($existingEntity && $existingEntity->getId()) { |
129
|
|
|
if ($existingEntity->isGuest()) { |
130
|
|
|
$existingEntity->setGuest(false); |
131
|
|
|
$existingEntity->setIsActive(true); |
132
|
|
|
} |
133
|
|
|
if ($entity->getOriginId()) { |
134
|
|
|
$existingEntity->setOriginId($entity->getOriginId()); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} elseif ($entity instanceof Region) { |
139
|
|
|
/** @var \OroCRM\Bundle\MagentoBundle\Entity\Region $existingEntity */ |
140
|
|
|
$existingEntity = $this->findRegionEntity($entity, $entity->getCombinedCode()); |
141
|
|
|
} else { |
142
|
|
|
/** @var Customer $existingEntity */ |
143
|
|
|
$existingEntity = parent::findExistingEntity($entity, $searchContext); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $existingEntity; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.