1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\ImportExport\Strategy; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
6
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
7
|
|
|
|
8
|
|
|
use Oro\Bundle\ImportExportBundle\Strategy\Import\ConfigurableAddOrReplaceStrategy; |
9
|
|
|
use Oro\Bundle\AddressBundle\Entity\Region; |
10
|
|
|
|
11
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Customer; |
12
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Region as MagentoRegion; |
13
|
|
|
|
14
|
|
|
class DefaultMagentoImportStrategy extends ConfigurableAddOrReplaceStrategy |
15
|
|
|
{ |
16
|
|
|
/** @var PropertyAccessor */ |
17
|
|
|
protected $propertyAccessor; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param PropertyAccessor $propertyAccessor |
21
|
|
|
*/ |
22
|
|
|
public function setPropertyAccessor(PropertyAccessor $propertyAccessor) |
23
|
|
|
{ |
24
|
|
|
$this->propertyAccessor = $propertyAccessor; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
protected function updateContextCounters($entity) |
31
|
|
|
{ |
32
|
|
|
// increment context counter |
33
|
|
|
$identifier = $this->databaseHelper->getIdentifier($entity); |
34
|
|
|
if ($identifier) { |
35
|
|
|
$this->context->incrementUpdateCount(); |
36
|
|
|
} else { |
37
|
|
|
$this->context->incrementAddCount(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $entity; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Specify channel as identity field |
45
|
|
|
* |
46
|
|
|
* For local entities created from not existing in Magento entities (as guest customer - without originId) |
47
|
|
|
* should be specified additional identities in appropriate strategy |
48
|
|
|
* |
49
|
|
|
* @param string $entityName |
50
|
|
|
* @param array $identityValues |
51
|
|
|
* @return null|object |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
protected function findEntityByIdentityValues($entityName, array $identityValues) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if (is_a($entityName, 'OroCRM\Bundle\MagentoBundle\Entity\IntegrationAwareInterface', true)) { |
56
|
|
|
$identityValues['channel'] = $this->context->getOption('channel'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return parent::findEntityByIdentityValues($entityName, $identityValues); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Combine channel with identity values for entity search on local new entities storage |
64
|
|
|
* |
65
|
|
|
* For local entities created from not existing in Magento entities (as guest customer - without originId) |
66
|
|
|
* should be configured special identity fields or search context in appropriate strategy |
67
|
|
|
* |
68
|
|
|
* @param $entity |
69
|
|
|
* @param $entityClass |
70
|
|
|
* @param array $searchContext |
71
|
|
|
* |
72
|
|
|
* @return array|null |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
protected function combineIdentityValues($entity, $entityClass, array $searchContext) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
if (is_a($entityClass, 'OroCRM\Bundle\MagentoBundle\Entity\IntegrationAwareInterface', true)) { |
77
|
|
|
$searchContext['channel'] = $this->context->getOption('channel'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return parent::combineIdentityValues($entity, $entityClass, $searchContext); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return PropertyAccessor |
85
|
|
|
*/ |
86
|
|
|
protected function getPropertyAccessor() |
87
|
|
|
{ |
88
|
|
|
if (!$this->propertyAccessor) { |
89
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
90
|
|
|
} |
91
|
|
|
return $this->propertyAccessor; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get existing registered customer or existing guest customer |
96
|
|
|
* Find existing customer using entity data for entities containing customer like Order and Cart |
97
|
|
|
* |
98
|
|
|
* @param object $entity |
99
|
|
|
* |
100
|
|
|
* @return null|Customer |
101
|
|
|
*/ |
102
|
|
|
protected function findExistingCustomerByContext($entity) |
103
|
|
|
{ |
104
|
|
|
/** @var Customer|null $existingEntity */ |
105
|
|
|
$searchContext = $this->getEntityCustomerSearchContext($entity); |
106
|
|
|
$existingEntity = $this->databaseHelper->findOneBy( |
107
|
|
|
'OroCRM\Bundle\MagentoBundle\Entity\Customer', |
108
|
|
|
$searchContext |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return $existingEntity; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get search context for entity customer |
116
|
|
|
* |
117
|
|
|
* @param object $entity |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
protected function getEntityCustomerSearchContext($entity) |
122
|
|
|
{ |
123
|
|
|
$customer = $entity->getCustomer(); |
124
|
|
|
if ($customer instanceof Customer) { |
125
|
|
|
$searchContext = $this->getCustomerSearchContext($customer); |
126
|
|
|
} else { |
127
|
|
|
$searchContext = [ |
128
|
|
|
'channel' => $entity->getChannel() |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $searchContext; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get customer search context by channel and website if exists |
137
|
|
|
* |
138
|
|
|
* @param Customer $customer |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
protected function getCustomerSearchContext(Customer $customer) |
143
|
|
|
{ |
144
|
|
|
$searchContext = [ |
145
|
|
|
'channel' => $customer->getChannel() |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
if ($customer->getWebsite()) { |
149
|
|
|
$website = $this->databaseHelper->findOneBy( |
150
|
|
|
'OroCRM\Bundle\MagentoBundle\Entity\Website', |
151
|
|
|
[ |
152
|
|
|
'originId' => $customer->getWebsite()->getOriginId(), |
153
|
|
|
'channel' => $customer->getChannel() |
154
|
|
|
] |
155
|
|
|
); |
156
|
|
|
if ($website) { |
157
|
|
|
$searchContext['website'] = $website; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $searchContext; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Find existing Magento Region entity |
166
|
|
|
* Find by Code if parameter $regionId not passed |
167
|
|
|
* |
168
|
|
|
* @param Region $entity |
169
|
|
|
* @param string|null $regionId |
170
|
|
|
* |
171
|
|
|
* @return null|MagentoRegion |
172
|
|
|
*/ |
173
|
|
|
protected function findRegionEntity(Region $entity, $regionId = null) |
174
|
|
|
{ |
175
|
|
|
$existingEntity = null; |
176
|
|
|
|
177
|
|
|
if (!$regionId) { |
|
|
|
|
178
|
|
|
$regionId = $entity->getCode(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** @var Region $magentoRegion */ |
182
|
|
|
$magentoRegion = $this->databaseHelper->findOneBy( |
183
|
|
|
'OroCRM\Bundle\MagentoBundle\Entity\Region', |
184
|
|
|
[ |
185
|
|
|
'regionId' => $regionId |
186
|
|
|
] |
187
|
|
|
); |
188
|
|
|
if ($magentoRegion) { |
189
|
|
|
$existingEntity = $this->databaseHelper->findOneBy( |
190
|
|
|
'Oro\Bundle\AddressBundle\Entity\Region', |
191
|
|
|
[ |
192
|
|
|
'combinedCode' => $magentoRegion->getCombinedCode() |
193
|
|
|
] |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $existingEntity; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.