1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bundle\MagentoBundle\ImportExport\Strategy; |
4
|
|
|
|
5
|
|
|
use Oro\Bundle\MagentoBundle\Entity\Customer; |
6
|
|
|
use Oro\Bundle\MagentoBundle\Entity\MagentoSoapTransport; |
7
|
|
|
use Oro\Bundle\MagentoBundle\Entity\Order; |
8
|
|
|
use Oro\Bundle\MagentoBundle\ImportExport\Converter\GuestCustomerDataConverter; |
9
|
|
|
use Oro\Bundle\MagentoBundle\Provider\Reader\ContextCartReader; |
10
|
|
|
use Oro\Bundle\MagentoBundle\Provider\Reader\ContextCustomerReader; |
11
|
|
|
|
12
|
|
|
class OrderWithExistingCustomerStrategy extends OrderStrategy |
13
|
|
|
{ |
14
|
|
|
const CONTEXT_ORDER_POST_PROCESS = 'postProcessOrders'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param Order $importingOrder |
18
|
|
|
* |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
public function process($importingOrder) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
if (!$this->isProcessingAllowed($importingOrder)) { |
24
|
|
|
$this->appendDataToContext(self::CONTEXT_ORDER_POST_PROCESS, $this->context->getValue('itemData')); |
25
|
|
|
|
26
|
|
|
return null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return parent::process($importingOrder); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Order $order |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
|
protected function isProcessingAllowed(Order $order) |
37
|
|
|
{ |
38
|
|
|
$isProcessingAllowed = true; |
39
|
|
|
$customer = $this->findExistingCustomer($order); |
40
|
|
|
$customerOriginId = $order->getCustomer()->getOriginId(); |
|
|
|
|
41
|
|
|
if (!$customer && $customerOriginId) { |
42
|
|
|
$this->appendDataToContext(ContextCustomerReader::CONTEXT_POST_PROCESS_CUSTOMERS, $customerOriginId); |
43
|
|
|
|
44
|
|
|
$isProcessingAllowed = false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Do not try to load cart if bridge does not installed |
48
|
|
|
/** @var MagentoSoapTransport $transport */ |
49
|
|
|
$channel = $this->databaseHelper->findOneByIdentity($order->getChannel()); |
50
|
|
|
$transport = $channel->getTransport(); |
51
|
|
|
if ($transport->getIsExtensionInstalled()) { |
52
|
|
|
$cart = $this->findExistingEntity($order->getCart()); |
|
|
|
|
53
|
|
|
$cartOriginId = $order->getCart()->getOriginId(); |
54
|
|
|
if (!$cart && $cartOriginId) { |
55
|
|
|
$this->appendDataToContext(ContextCartReader::CONTEXT_POST_PROCESS_CARTS, $cartOriginId); |
56
|
|
|
|
57
|
|
|
$isProcessingAllowed = false; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* If Order created with registered customer but registered customer was deleted in Magento |
63
|
|
|
* before it was synced order will not have connection to the customer |
64
|
|
|
* Customer for such orders should be processed as guest if Guest Customer synchronization is allowed |
65
|
|
|
*/ |
66
|
|
|
if (!$customer && !$customerOriginId && $transport->getGuestCustomerSync()) { |
67
|
|
|
$this->appendDataToContext( |
68
|
|
|
'postProcessGuestCustomers', |
69
|
|
|
GuestCustomerDataConverter::extractCustomersValues((array)$this->context->getValue('itemData')) |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$isProcessingAllowed = false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $isProcessingAllowed; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get existing registered customer or existing guest customer |
80
|
|
|
* If customer not found by Identifier |
81
|
|
|
* find existing customer using entity data for entities containing customer like Order and Cart |
82
|
|
|
* |
83
|
|
|
* @param Order $entity |
84
|
|
|
* |
85
|
|
|
* @return null|Customer |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
protected function findExistingCustomer(Order $entity) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$existingEntity = null; |
90
|
|
|
$customer = $entity->getCustomer(); |
91
|
|
|
|
92
|
|
|
if ($customer->getId() || $customer->getOriginId()) { |
93
|
|
|
$existingEntity = parent::findExistingEntity($customer); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
if (!$existingEntity) { |
96
|
|
|
$existingEntity = $this->findExistingCustomerByContext($entity); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $existingEntity; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get existing customer entity by Identifier or using entity data |
104
|
|
|
* |
105
|
|
|
* @param object $entity |
106
|
|
|
* @param array $searchContext |
107
|
|
|
* @return null|object |
108
|
|
|
*/ |
109
|
|
|
protected function findExistingEntity($entity, array $searchContext = []) |
110
|
|
|
{ |
111
|
|
View Code Duplication |
if ($entity instanceof Customer && !$entity->getOriginId() && $this->existingEntity) { |
|
|
|
|
112
|
|
|
return $this->findExistingCustomerByContext($this->existingEntity); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return parent::findExistingEntity($entity, $searchContext); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
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.