Source.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Indexer;
  7. use Magento\Customer\Model\ResourceModel\Customer\Indexer\CollectionFactory;
  8. use Magento\Customer\Model\ResourceModel\Customer\Indexer\Collection;
  9. use Magento\Framework\App\ResourceConnection\SourceProviderInterface;
  10. use Traversable;
  11. /**
  12. * Customers data batch generator for customer_grid indexer
  13. */
  14. class Source implements \IteratorAggregate, \Countable, SourceProviderInterface
  15. {
  16. /**
  17. * @var Collection
  18. */
  19. private $customerCollection;
  20. /**
  21. * @var int
  22. */
  23. private $batchSize;
  24. /**
  25. * @param CollectionFactory $collectionFactory
  26. * @param int $batchSize
  27. */
  28. public function __construct(
  29. CollectionFactory $collectionFactory,
  30. $batchSize = 10000
  31. ) {
  32. $this->customerCollection = $collectionFactory->create();
  33. $this->batchSize = $batchSize;
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function getMainTable()
  39. {
  40. return $this->customerCollection->getMainTable();
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function getIdFieldName()
  46. {
  47. return $this->customerCollection->getIdFieldName();
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. public function addFieldToSelect($fieldName, $alias = null)
  53. {
  54. $this->customerCollection->addFieldToSelect($fieldName, $alias);
  55. return $this;
  56. }
  57. /**
  58. * @inheritdoc
  59. */
  60. public function getSelect()
  61. {
  62. return $this->customerCollection->getSelect();
  63. }
  64. /**
  65. * @inheritdoc
  66. */
  67. public function addFieldToFilter($attribute, $condition = null)
  68. {
  69. $this->customerCollection->addFieldToFilter($attribute, $condition);
  70. return $this;
  71. }
  72. /**
  73. * @inheritdoc
  74. */
  75. public function count()
  76. {
  77. return $this->customerCollection->getSize();
  78. }
  79. /**
  80. * Retrieve an iterator
  81. *
  82. * @return Traversable
  83. */
  84. public function getIterator()
  85. {
  86. $this->customerCollection->setPageSize($this->batchSize);
  87. $lastPage = $this->customerCollection->getLastPageNumber();
  88. $pageNumber = 0;
  89. do {
  90. $this->customerCollection->clear();
  91. $this->customerCollection->setCurPage($pageNumber);
  92. foreach ($this->customerCollection->getItems() as $key => $value) {
  93. yield $key => $value;
  94. }
  95. $pageNumber++;
  96. } while ($pageNumber <= $lastPage);
  97. }
  98. /**
  99. * Joins Attribute
  100. *
  101. * @param string $alias alias for the joined attribute
  102. * @param string|\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
  103. * @param string $bind attribute of the main entity to link with joined $filter
  104. * @param string|null $filter primary key for the joined entity (entity_id default)
  105. * @param string $joinType inner|left
  106. * @param int|null $storeId
  107. * @return void
  108. * @throws \Magento\Framework\Exception\LocalizedException
  109. * @see Collection::joinAttribute()
  110. */
  111. public function joinAttribute(
  112. string $alias,
  113. $attribute,
  114. string $bind,
  115. ?string $filter = null,
  116. string $joinType = 'inner',
  117. ?int $storeId = null
  118. ): void {
  119. $this->customerCollection->joinAttribute($alias, $attribute, $bind, $filter, $joinType, $storeId);
  120. }
  121. }