CustomerGroupDimensionProvider.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Customer\Model\Indexer;
  8. use Magento\Customer\Model\ResourceModel\Group\CollectionFactory as CustomerGroupCollectionFactory;
  9. use Magento\Framework\Indexer\DimensionFactory;
  10. use Magento\Framework\Indexer\DimensionProviderInterface;
  11. /**
  12. * Class CustomerGroupDimensionProvider
  13. */
  14. class CustomerGroupDimensionProvider implements DimensionProviderInterface
  15. {
  16. /**
  17. * Name for customer group dimension for multidimensional indexer
  18. * 'cg' - stands for 'customer_group'
  19. */
  20. const DIMENSION_NAME = 'cg';
  21. /**
  22. * @var CustomerGroupCollectionFactory
  23. */
  24. private $collectionFactory;
  25. /**
  26. * @var \SplFixedArray
  27. */
  28. private $customerGroupsDataIterator;
  29. /**
  30. * @var DimensionFactory
  31. */
  32. private $dimensionFactory;
  33. /**
  34. * @param CustomerGroupCollectionFactory $collectionFactory
  35. * @param DimensionFactory $dimensionFactory
  36. */
  37. public function __construct(CustomerGroupCollectionFactory $collectionFactory, DimensionFactory $dimensionFactory)
  38. {
  39. $this->dimensionFactory = $dimensionFactory;
  40. $this->collectionFactory = $collectionFactory;
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function getIterator(): \Traversable
  46. {
  47. foreach ($this->getCustomerGroups() as $customerGroup) {
  48. yield $this->dimensionFactory->create(self::DIMENSION_NAME, (string)$customerGroup);
  49. }
  50. }
  51. /**
  52. * Get Customer Groups
  53. *
  54. * @return array
  55. */
  56. private function getCustomerGroups(): array
  57. {
  58. if ($this->customerGroupsDataIterator === null) {
  59. $customerGroups = $this->collectionFactory->create()->getAllIds();
  60. $this->customerGroupsDataIterator = is_array($customerGroups) ? $customerGroups : [];
  61. }
  62. return $this->customerGroupsDataIterator;
  63. }
  64. }