Collection.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Indexer\Model\Indexer;
  7. use Magento\Framework\Indexer\IndexerInterface;
  8. class Collection extends \Magento\Framework\Data\Collection
  9. {
  10. /**
  11. * Item object class name
  12. *
  13. * @var string
  14. */
  15. protected $_itemObjectClass = IndexerInterface::class;
  16. /**
  17. * Collection items
  18. *
  19. * @var IndexerInterface[]
  20. */
  21. protected $_items = [];
  22. /**
  23. * @var \Magento\Framework\Indexer\ConfigInterface
  24. */
  25. protected $config;
  26. /**
  27. * @var \Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory
  28. */
  29. protected $statesFactory;
  30. /**
  31. * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
  32. * @param \Magento\Framework\Indexer\ConfigInterface $config
  33. * @param \Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory $statesFactory
  34. */
  35. public function __construct(
  36. \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
  37. \Magento\Framework\Indexer\ConfigInterface $config,
  38. \Magento\Indexer\Model\ResourceModel\Indexer\State\CollectionFactory $statesFactory
  39. ) {
  40. $this->config = $config;
  41. $this->statesFactory = $statesFactory;
  42. parent::__construct($entityFactory);
  43. }
  44. /**
  45. * Load data
  46. *
  47. * @param bool $printQuery
  48. * @param bool $logQuery
  49. * @return \Magento\Indexer\Model\Indexer\Collection
  50. *
  51. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  52. */
  53. public function loadData($printQuery = false, $logQuery = false)
  54. {
  55. if (!$this->isLoaded()) {
  56. $states = $this->statesFactory->create();
  57. foreach (array_keys($this->config->getIndexers()) as $indexerId) {
  58. /** @var IndexerInterface $indexer */
  59. $indexer = $this->getNewEmptyItem();
  60. $indexer->load($indexerId);
  61. foreach ($states->getItems() as $state) {
  62. /** @var \Magento\Indexer\Model\Indexer\State $state */
  63. if ($state->getIndexerId() == $indexerId) {
  64. $indexer->setState($state);
  65. break;
  66. }
  67. }
  68. $this->_addItem($indexer);
  69. }
  70. $this->_setIsLoaded(true);
  71. }
  72. return $this;
  73. }
  74. /**
  75. * {@inheritdoc} Prevents handle collection items as DataObject class instances.
  76. */
  77. public function getAllIds()
  78. {
  79. $ids = [];
  80. foreach ($this->getItems() as $item) {
  81. $ids[] = $item->getId();
  82. }
  83. return $ids;
  84. }
  85. /**
  86. * @inheritdoc
  87. * @return IndexerInterface[]
  88. */
  89. public function getItems()
  90. {
  91. return parent::getItems();
  92. }
  93. /**
  94. * {@inheritdoc} Prevents handle collection items as DataObject class instances.
  95. * @deprecated 100.2.0 Should not be used in the current implementation.
  96. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  97. */
  98. public function getColumnValues($colName)
  99. {
  100. return [];
  101. }
  102. /**
  103. * {@inheritdoc} Prevents handle collection items as DataObject class instances.
  104. * @deprecated 100.2.0 Should not be used in the current implementation.
  105. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  106. */
  107. public function getItemsByColumnValue($column, $value)
  108. {
  109. return [];
  110. }
  111. /**
  112. * {@inheritdoc} Prevents handle collection items as DataObject class instances.
  113. * @deprecated 100.2.0 Should not be used in the current implementation.
  114. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  115. */
  116. public function getItemByColumnValue($column, $value)
  117. {
  118. return null;
  119. }
  120. /**
  121. * {@inheritdoc} Prevents handle collection items as DataObject class instances.
  122. * @deprecated 100.2.0 Should not be used in the current implementation.
  123. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  124. */
  125. public function setDataToAll($key, $value = null)
  126. {
  127. return $this;
  128. }
  129. /**
  130. * {@inheritdoc} Prevents handle collection items as DataObject class instances.
  131. * @deprecated 100.2.0 Should not be used in the current implementation.
  132. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  133. */
  134. public function setItemObjectClass($className)
  135. {
  136. return $this;
  137. }
  138. /**
  139. * {@inheritdoc} Prevents handle collection items as DataObject class instances.
  140. * @deprecated 100.2.0 Should not be used in the current implementation.
  141. */
  142. public function toXml()
  143. {
  144. return '';
  145. }
  146. /**
  147. * {@inheritdoc} Prevents handle collection items as DataObject class instances.
  148. * @deprecated 100.2.0 Should not be used in the current implementation.
  149. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  150. */
  151. public function toArray($arrRequiredFields = [])
  152. {
  153. return [];
  154. }
  155. /**
  156. * {@inheritdoc} Prevents handle collection items as DataObject class instances.
  157. * @deprecated 100.2.0 Should not be used in the current implementation.
  158. */
  159. public function toOptionArray()
  160. {
  161. return [];
  162. }
  163. /**
  164. * {@inheritdoc} Prevents handle collection items as DataObject class instances.
  165. * @deprecated 100.2.0 Should not be used in the current implementation.
  166. */
  167. public function toOptionHash()
  168. {
  169. return [];
  170. }
  171. /**
  172. * {@inheritdoc} Prevents handle collection items as DataObject class instances.
  173. * @deprecated 100.2.0 Should not be used in the current implementation.
  174. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  175. */
  176. protected function _toOptionArray($valueField = 'id', $labelField = 'name', $additional = [])
  177. {
  178. return [];
  179. }
  180. /**
  181. * {@inheritdoc} Prevents handle collection items as DataObject class instances.
  182. * @deprecated 100.2.0 Should not be used in the current implementation.
  183. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  184. */
  185. protected function _toOptionHash($valueField = 'id', $labelField = 'name')
  186. {
  187. return [];
  188. }
  189. }