-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'remotes/dev/2.0' into 2.0
- Loading branch information
Showing
16 changed files
with
889 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/Oro/Bundle/MagentoBundle/Command/CopyCustomerAddressesToContactCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Oro\Bundle\MagentoBundle\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\DependencyInjection\ContainerAwareInterface; | ||
use Symfony\Component\DependencyInjection\ContainerAwareTrait; | ||
|
||
use Oro\Bundle\MagentoBundle\Manager\CustomerAddressManager; | ||
use Oro\Component\Log\OutputLogger; | ||
|
||
class CopyCustomerAddressesToContactCommand extends Command implements ContainerAwareInterface | ||
{ | ||
use ContainerAwareTrait; | ||
|
||
const BATCH_SIZE = 25; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure() | ||
{ | ||
$this | ||
->setName('oro:magento:copy-data-to-contact:addresses') | ||
->addOption( | ||
'id', | ||
null, | ||
InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY, | ||
'If option exists then customer addresses will be copied to contact for given magento customer by id' | ||
) | ||
->addOption( | ||
'integration-id', | ||
null, | ||
InputOption::VALUE_OPTIONAL|InputOption::VALUE_IS_ARRAY, | ||
'If option exists then customer addresses will be copied to contact | ||
for given magento customers by integration_id' | ||
) | ||
->addOption( | ||
'batch-size', | ||
null, | ||
InputOption::VALUE_OPTIONAL, | ||
'Number of customers in batch. The default value is 25.' | ||
) | ||
->setDescription('Make copy addresses of magento customers to the contact'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$logger = new OutputLogger($output); | ||
$logger->info('Executing command started.'); | ||
|
||
$integrationIds = $input->getOption('integration-id'); | ||
$ids = $input->getOption('id'); | ||
$batchSize = $input->getOption('batch-size') ? $input->getOption('batch-size') : self::BATCH_SIZE; | ||
|
||
$logger->info('Parameters:'); | ||
if ($integrationIds) { | ||
foreach ($integrationIds as $item) { | ||
$logger->info(sprintf('--integration-id=%s', $item)); | ||
} | ||
} | ||
if ($ids) { | ||
foreach ($integrationIds as $item) { | ||
$logger->info(sprintf('--id=%s', $item)); | ||
} | ||
} | ||
if ($batchSize) { | ||
$logger->info(sprintf('--batch-size=%s', $batchSize)); | ||
$logger->info(''); | ||
} | ||
|
||
/** @var CustomerAddressManager $customerAddressManager */ | ||
$customerAddressManager = $this->container->get('oro_magento.manager.customer_address_manager'); | ||
$customerAddressManager->setLogger($logger); | ||
$customerAddressManager->copyToContact($ids, $integrationIds, $batchSize); | ||
$logger->info('Executing command finished.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
src/Oro/Bundle/MagentoBundle/Manager/CustomerAddressManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
<?php | ||
|
||
namespace Oro\Bundle\MagentoBundle\Manager; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
|
||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerAwareTrait; | ||
|
||
use Symfony\Component\PropertyAccess\PropertyAccess; | ||
use Symfony\Component\PropertyAccess\PropertyAccessor; | ||
|
||
use Oro\Bundle\ContactBundle\Entity\Contact; | ||
use Oro\Bundle\ContactBundle\Entity\ContactAddress; | ||
use Oro\Bundle\MagentoBundle\Entity\Address; | ||
use Oro\Bundle\MagentoBundle\Entity\Customer; | ||
|
||
class CustomerAddressManager implements LoggerAwareInterface | ||
{ | ||
use LoggerAwareTrait; | ||
|
||
/** @var EntityManager */ | ||
protected $em; | ||
|
||
/** @var PropertyAccessor */ | ||
protected $accessor; | ||
|
||
protected $baseAddressProperties = [ | ||
'label', | ||
'street', | ||
'street2', | ||
'city', | ||
'postalCode', | ||
'country', | ||
'organization', | ||
'region', | ||
'regionText', | ||
'namePrefix', | ||
'firstName', | ||
'middleName', | ||
'lastName', | ||
'nameSuffix' | ||
]; | ||
|
||
/** | ||
* @param EntityManager $em | ||
*/ | ||
public function __construct(EntityManager $em) | ||
{ | ||
$this->em = $em; | ||
$this->accessor = PropertyAccess::createPropertyAccessor(); | ||
} | ||
|
||
/** | ||
* @param int[]|null $customersIds | ||
* @param int[]|null $integrationIds | ||
* @param int $batchSize | ||
*/ | ||
public function copyToContact($customersIds = null, $integrationIds = null, $batchSize = 25) | ||
{ | ||
$i = 0; | ||
$this->logger->info(sprintf('Start process')); | ||
$repository = $this->em->getRepository('OroMagentoBundle:Customer'); | ||
|
||
$iterator = $repository->getIteratorByIdsAndIntegrationIds($customersIds, $integrationIds); | ||
$iterator->setBufferSize($batchSize); | ||
$customerCount = $iterator->count(); | ||
|
||
$iterator->setPageCallback(function () use (&$i, $customerCount) { | ||
$this->em->flush(); | ||
$this->em->flush(); | ||
$this->logger->info(sprintf('Processed %s customers from %s', $i, $customerCount)); | ||
}); | ||
|
||
/** @var Customer $customer */ | ||
foreach ($iterator as $customer) { | ||
$i++; | ||
$contact = $customer->getContact(); | ||
if ($contact) { | ||
$addresses = $customer->getAddresses(); | ||
|
||
if ($addresses->count() > 0) { | ||
foreach ($addresses as $address) { | ||
$newContactAddress = $this->convertToContactAddress($address); | ||
if (!$this->contactHasAddress($contact, $newContactAddress)) { | ||
$contact->addAddress($newContactAddress); | ||
$message = 'Customer address with id=%s was copied in contact with id=%s'; | ||
$this->logger->info(sprintf($message, $address->getId(), $contact->getId())); | ||
} | ||
} | ||
$this->em->persist($contact); | ||
} | ||
} | ||
} | ||
|
||
$this->em->flush(); | ||
$this->logger->info(sprintf('Finish process')); | ||
} | ||
|
||
/** | ||
* @param Contact $contact | ||
* @param ContactAddress $contactAddress | ||
* | ||
* @return bool | ||
*/ | ||
protected function contactHasAddress(Contact $contact, ContactAddress $contactAddress) | ||
{ | ||
$addresses = $contact->getAddresses(); | ||
|
||
foreach ($addresses as $address) { | ||
if ($this->isEqualAddresses($address, $contactAddress)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param ContactAddress $address1 | ||
* @param ContactAddress $address2 | ||
* | ||
* @return bool | ||
*/ | ||
protected function isEqualAddresses(ContactAddress $address1, ContactAddress $address2) | ||
{ | ||
$countEqualProperty = 0; | ||
foreach ($this->baseAddressProperties as $property) { | ||
if ($this->accessor->getValue($address1, $property) === $this->accessor->getValue($address2, $property)) { | ||
$countEqualProperty++; | ||
} | ||
} | ||
|
||
return $countEqualProperty === count($this->baseAddressProperties); | ||
} | ||
|
||
/** | ||
* @param Address $customerAddress | ||
* | ||
* @return ContactAddress | ||
*/ | ||
protected function convertToContactAddress(Address $customerAddress) | ||
{ | ||
$properties = $this->baseAddressProperties; | ||
$properties[] = 'types'; | ||
$properties[] = 'primary'; | ||
|
||
$contactAddress = new ContactAddress(); | ||
foreach ($properties as $property) { | ||
$this->accessor->setValue( | ||
$contactAddress, | ||
$property, | ||
$this->accessor->getValue($customerAddress, $property) | ||
); | ||
} | ||
|
||
return $contactAddress; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.