1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\Manager; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerAwareInterface; |
8
|
|
|
use Psr\Log\LoggerAwareTrait; |
9
|
|
|
|
10
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
11
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
12
|
|
|
|
13
|
|
|
use OroCRM\Bundle\ContactBundle\Entity\Contact; |
14
|
|
|
use OroCRM\Bundle\ContactBundle\Entity\ContactAddress; |
15
|
|
|
use OroCRM\Bundle\MagentoBundle\Entity\Customer; |
16
|
|
|
use OroCRM\Bundle\MagentoBundle\Manager\CustomerAddress\ConvertAddressToContactAdress; |
17
|
|
|
|
18
|
|
|
class CustomerAddressManager implements LoggerAwareInterface |
19
|
|
|
{ |
20
|
|
|
use LoggerAwareTrait; |
21
|
|
|
|
22
|
|
|
/** @var EntityManager */ |
23
|
|
|
protected $em; |
24
|
|
|
|
25
|
|
|
/** @var PropertyAccessor */ |
26
|
|
|
protected $accessor; |
27
|
|
|
|
28
|
|
|
/** @var ConvertAddressToContactAdress */ |
29
|
|
|
protected $convertAddressToContactAddress; |
30
|
|
|
|
31
|
|
|
/** @var array */ |
32
|
|
|
protected $baseAddressProperties = [ |
33
|
|
|
'label', |
34
|
|
|
'street', |
35
|
|
|
'street2', |
36
|
|
|
'city', |
37
|
|
|
'postalCode', |
38
|
|
|
'country', |
39
|
|
|
'organization', |
40
|
|
|
'region', |
41
|
|
|
'regionText', |
42
|
|
|
'namePrefix', |
43
|
|
|
'firstName', |
44
|
|
|
'middleName', |
45
|
|
|
'lastName', |
46
|
|
|
'nameSuffix' |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param EntityManager $em |
51
|
|
|
* @param ConvertAddressToContactAdress $convertAddressToContactAdress |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
EntityManager $em, |
|
|
|
|
55
|
|
|
ConvertAddressToContactAdress $convertAddressToContactAdress |
56
|
|
|
) { |
57
|
|
|
$this->em = $em; |
58
|
|
|
$this->convertAddressToContactAddress = $convertAddressToContactAdress; |
59
|
|
|
$this->accessor = PropertyAccess::createPropertyAccessor(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param int[]|null $customersIds |
64
|
|
|
* @param int[]|null $integrationIds |
65
|
|
|
* @param int $batchSize |
66
|
|
|
*/ |
67
|
|
|
public function copyToContact($customersIds = null, $integrationIds = null, $batchSize = 25) |
68
|
|
|
{ |
69
|
|
|
$i = 0; |
70
|
|
|
$this->logger->info(sprintf('Start process')); |
71
|
|
|
$repository = $this->em->getRepository('OroCRMMagentoBundle:Customer'); |
72
|
|
|
|
73
|
|
|
$iterator = $repository->getIteratorByIdsAndIntegrationIds($customersIds, $integrationIds); |
74
|
|
|
$iterator->setBufferSize($batchSize); |
75
|
|
|
$customerCount = $iterator->count(); |
76
|
|
|
|
77
|
|
|
$iterator->setPageCallback(function () use (&$i, $customerCount) { |
78
|
|
|
$this->em->flush(); |
79
|
|
|
$this->logger->info(sprintf('Processed %s customers from %s', $i, $customerCount)); |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
/** @var Customer $customer */ |
83
|
|
|
foreach ($iterator as $customer) { |
84
|
|
|
$i++; |
85
|
|
|
$contact = $customer->getContact(); |
86
|
|
|
if ($contact) { |
87
|
|
|
$addresses = $customer->getAddresses(); |
88
|
|
|
if ($addresses->count() > 0) { |
89
|
|
|
foreach ($addresses as $address) { |
90
|
|
|
$newContactAddress = $this->convertAddressToContactAddress->convert($address); |
91
|
|
|
if (!$this->contactHasAddress($contact, $newContactAddress)) { |
92
|
|
|
$contact->addAddress($newContactAddress); |
93
|
|
|
$message = 'Customer address with id=%s was copied in contact with id=%s'; |
94
|
|
|
$this->logger->info(sprintf($message, $address->getId(), $contact->getId())); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
$this->em->persist($contact); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->em->flush(); |
103
|
|
|
$this->logger->info(sprintf('Finish process')); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Contact $contact |
108
|
|
|
* @param ContactAddress $contactAddress |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
protected function contactHasAddress(Contact $contact, ContactAddress $contactAddress) |
113
|
|
|
{ |
114
|
|
|
$addresses = $contact->getAddresses(); |
115
|
|
|
foreach ($addresses as $address) { |
116
|
|
|
if ($this->isEqualAddresses($address, $contactAddress)) { |
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param ContactAddress $address1 |
126
|
|
|
* @param ContactAddress $address2 |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
protected function isEqualAddresses(ContactAddress $address1, ContactAddress $address2) |
131
|
|
|
{ |
132
|
|
|
$countEqualProperty = 0; |
133
|
|
|
foreach ($this->baseAddressProperties as $property) { |
134
|
|
|
if ($this->accessor->getValue($address1, $property) === $this->accessor->getValue($address2, $property)) { |
135
|
|
|
$countEqualProperty++; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $countEqualProperty === count($this->baseAddressProperties); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.