|
| 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\Address; |
| 16 | +use OroCRM\Bundle\MagentoBundle\Entity\Customer; |
| 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 array */ |
| 29 | + protected $baseAddressProperties = [ |
| 30 | + 'label', |
| 31 | + 'street', |
| 32 | + 'street2', |
| 33 | + 'city', |
| 34 | + 'postalCode', |
| 35 | + 'country', |
| 36 | + 'organization', |
| 37 | + 'region', |
| 38 | + 'regionText', |
| 39 | + 'namePrefix', |
| 40 | + 'firstName', |
| 41 | + 'middleName', |
| 42 | + 'lastName', |
| 43 | + 'nameSuffix' |
| 44 | + ]; |
| 45 | + |
| 46 | + /** |
| 47 | + * @param EntityManager $em |
| 48 | + */ |
| 49 | + public function __construct(EntityManager $em) |
| 50 | + { |
| 51 | + $this->em = $em; |
| 52 | + $this->accessor = PropertyAccess::createPropertyAccessor(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param int[]|null $customersIds |
| 57 | + * @param int[]|null $integrationIds |
| 58 | + * @param int $batchSize |
| 59 | + */ |
| 60 | + public function copyToContact($customersIds = null, $integrationIds = null, $batchSize = 25) |
| 61 | + { |
| 62 | + $i = 0; |
| 63 | + $this->logger->info(sprintf('Start process')); |
| 64 | + $repository = $this->em->getRepository('OroCRMMagentoBundle:Customer'); |
| 65 | + |
| 66 | + $iterator = $repository->getIteratorByIdsAndIntegrationIds($customersIds, $integrationIds); |
| 67 | + $iterator->setBufferSize($batchSize); |
| 68 | + $customerCount = $iterator->count(); |
| 69 | + |
| 70 | + $iterator->setPageCallback(function () use (&$i, $customerCount) { |
| 71 | + $this->em->flush(); |
| 72 | + $this->em->flush(); |
| 73 | + $this->logger->info(sprintf('Processed %s customers from %s', $i, $customerCount)); |
| 74 | + }); |
| 75 | + |
| 76 | + /** @var Customer $customer */ |
| 77 | + foreach ($iterator as $customer) { |
| 78 | + $i++; |
| 79 | + $contact = $customer->getContact(); |
| 80 | + if ($contact) { |
| 81 | + $addresses = $customer->getAddresses(); |
| 82 | + if ($addresses->count() > 0) { |
| 83 | + foreach ($addresses as $address) { |
| 84 | + $newContactAddress = $this->convertToContactAddress($address); |
| 85 | + if (!$this->contactHasAddress($contact, $newContactAddress)) { |
| 86 | + $contact->addAddress($newContactAddress); |
| 87 | + $message = 'Customer address with id=%s was copied in contact with id=%s'; |
| 88 | + $this->logger->info(sprintf($message, $address->getId(), $contact->getId())); |
| 89 | + } |
| 90 | + } |
| 91 | + $this->em->persist($contact); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + $this->em->flush(); |
| 97 | + $this->logger->info(sprintf('Finish process')); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param Contact $contact |
| 102 | + * @param ContactAddress $contactAddress |
| 103 | + * |
| 104 | + * @return bool |
| 105 | + */ |
| 106 | + protected function contactHasAddress(Contact $contact, ContactAddress $contactAddress) |
| 107 | + { |
| 108 | + $addresses = $contact->getAddresses(); |
| 109 | + foreach ($addresses as $address) { |
| 110 | + if ($this->isEqualAddresses($address, $contactAddress)) { |
| 111 | + return true; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @param ContactAddress $address1 |
| 120 | + * @param ContactAddress $address2 |
| 121 | + * |
| 122 | + * @return bool |
| 123 | + */ |
| 124 | + protected function isEqualAddresses(ContactAddress $address1, ContactAddress $address2) |
| 125 | + { |
| 126 | + $countEqualProperty = 0; |
| 127 | + foreach ($this->baseAddressProperties as $property) { |
| 128 | + if ($this->accessor->getValue($address1, $property) === $this->accessor->getValue($address2, $property)) { |
| 129 | + $countEqualProperty++; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return $countEqualProperty === count($this->baseAddressProperties); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @param Address $customerAddress |
| 138 | + * |
| 139 | + * @return ContactAddress |
| 140 | + */ |
| 141 | + protected function convertToContactAddress(Address $customerAddress) |
| 142 | + { |
| 143 | + $properties = $this->baseAddressProperties; |
| 144 | + $properties[] = 'types'; |
| 145 | + $properties[] = 'primary'; |
| 146 | + |
| 147 | + $contactAddress = new ContactAddress(); |
| 148 | + foreach ($properties as $property) { |
| 149 | + $this->accessor->setValue( |
| 150 | + $contactAddress, |
| 151 | + $property, |
| 152 | + $this->accessor->getValue($customerAddress, $property) |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + return $contactAddress; |
| 157 | + } |
| 158 | +} |
0 commit comments