PageRenderTime 71ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Oro/Bundle/MagentoBundle/ImportExport/Writer/ProxyEntityWriter.php

https://bitbucket.org/sebastian-bogado/crm
PHP | 146 lines | 99 code | 18 blank | 29 comment | 14 complexity | b7428444af71569e269ce6f0a2f49d47 MD5 | raw file
  1. <?php
  2. namespace Oro\Bundle\MagentoBundle\ImportExport\Writer;
  3. use Psr\Log\NullLogger;
  4. use Psr\Log\LoggerAwareTrait;
  5. use Psr\Log\LoggerAwareInterface;
  6. use Akeneo\Bundle\BatchBundle\Entity\StepExecution;
  7. use Akeneo\Bundle\BatchBundle\Item\ItemWriterInterface;
  8. use Akeneo\Bundle\BatchBundle\Step\StepExecutionAwareInterface;
  9. use Oro\Bundle\BatchBundle\Step\StepExecutionRestoreInterface;
  10. use Oro\Bundle\ImportExportBundle\Field\DatabaseHelper;
  11. use Oro\Bundle\MagentoBundle\Entity\Cart;
  12. use Oro\Bundle\MagentoBundle\Entity\CreditMemo;
  13. use Oro\Bundle\MagentoBundle\Entity\Order;
  14. use Oro\Bundle\MagentoBundle\Entity\Customer;
  15. use Oro\Bundle\MagentoBundle\Entity\NewsletterSubscriber;
  16. class ProxyEntityWriter implements
  17. ItemWriterInterface,
  18. StepExecutionAwareInterface,
  19. StepExecutionRestoreInterface,
  20. LoggerAwareInterface
  21. {
  22. use LoggerAwareTrait;
  23. /** @var ItemWriterInterface */
  24. protected $writer;
  25. /** @var DatabaseHelper */
  26. protected $databaseHelper;
  27. /** @var StepExecution|null */
  28. protected $previousStepExecution;
  29. /**
  30. * @param ItemWriterInterface $writer
  31. * @param DatabaseHelper $databaseHelper
  32. */
  33. public function __construct(ItemWriterInterface $writer, DatabaseHelper $databaseHelper)
  34. {
  35. $this->writer = $writer;
  36. $this->databaseHelper = $databaseHelper;
  37. $this->logger = new NullLogger();
  38. }
  39. /**
  40. * {@inheritdoc}
  41. *
  42. * Prepare items for PersistentBatchWriter, filters for duplicates and takes only latest versions
  43. */
  44. public function write(array $items)
  45. {
  46. $uniqueItems = [];
  47. $uniqueKeys = [];
  48. foreach ($items as $item) {
  49. if ($item instanceof Customer) {
  50. //GuestCustomerStrategy checks both email and channel
  51. if ($item->isGuest()) {
  52. $channel = $item->getChannel();
  53. $identifier = strtolower($item->getEmail());
  54. //set unique identifier: email and channel id
  55. if ($channel) {
  56. $identifier.=$channel->getId();
  57. }
  58. $identifier = md5($identifier);
  59. } else {
  60. $identifier = $item->getOriginId();
  61. }
  62. $this->handleIdentifier($uniqueItems, $item, $identifier);
  63. } elseif ($item instanceof Cart) {
  64. $this->handleIdentifier($uniqueItems, $item, $item->getOriginId());
  65. } elseif ($item instanceof Order) {
  66. $this->handleIdentifier($uniqueItems, $item, $item->getIncrementId());
  67. } elseif ($item instanceof CreditMemo) {
  68. $this->handleIdentifier($uniqueItems, $item, $item->getOriginId());
  69. } elseif ($item instanceof NewsletterSubscriber) {
  70. $identifier = $item->getCustomer() ? $item->getCustomer()->getId() : 0;
  71. if ($identifier !== 0 && in_array($identifier, $uniqueKeys)) {
  72. $this->logSkipped($item->getOriginId());
  73. } else {
  74. $uniqueKeys[] = $identifier;
  75. $uniqueItems[] = $item;
  76. }
  77. } else {
  78. $uniqueItems[] = $item;
  79. }
  80. }
  81. $this->writer->write($uniqueItems);
  82. // force entity cache clear if clear is skipped
  83. $this->databaseHelper->onClear();
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function setStepExecution(StepExecution $stepExecution)
  89. {
  90. if ($this->writer instanceof StepExecutionAwareInterface) {
  91. $this->writer->setStepExecution($stepExecution);
  92. }
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function restoreStepExecution()
  98. {
  99. if ($this->writer instanceof StepExecutionRestoreInterface) {
  100. $this->writer->restoreStepExecution();
  101. }
  102. }
  103. /**
  104. * @param array $uniqueItems
  105. * @param object $item
  106. * @param string|null $identifier
  107. */
  108. protected function handleIdentifier(array &$uniqueItems, $item, $identifier = null)
  109. {
  110. if ($identifier && array_key_exists($identifier, $uniqueItems)) {
  111. $this->logSkipped($identifier);
  112. }
  113. if ($identifier) {
  114. $uniqueItems[$identifier] = $item;
  115. } else {
  116. $uniqueItems[spl_object_hash($item)] = $item;
  117. }
  118. }
  119. /**
  120. * @param int|string $identifier
  121. */
  122. protected function logSkipped($identifier)
  123. {
  124. $this->logger->info(
  125. sprintf('[origin_id=%s] Item skipped because of newer version found', (string)$identifier)
  126. );
  127. }
  128. }