DependencyInfoProvider.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Framework\Indexer\Config;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\Indexer\ConfigInterface;
  10. use Magento\Framework\Phrase;
  11. /**
  12. * @inheritdoc
  13. */
  14. class DependencyInfoProvider implements DependencyInfoProviderInterface
  15. {
  16. /**
  17. * @var ConfigInterface
  18. */
  19. private $config;
  20. /**
  21. * @param ConfigInterface $config
  22. */
  23. public function __construct(ConfigInterface $config)
  24. {
  25. $this->config = $config;
  26. }
  27. /**
  28. * @inheritdoc
  29. */
  30. public function getIndexerIdsToRunBefore(string $indexerId): array
  31. {
  32. return $this->getIndexerDataWithValidation($indexerId)['dependencies'];
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function getIndexerIdsToRunAfter(string $indexerId): array
  38. {
  39. /** check indexer existence */
  40. $this->getIndexerDataWithValidation($indexerId);
  41. $result = [];
  42. foreach ($this->config->getIndexers() as $id => $indexerData) {
  43. if (array_search($indexerId, $indexerData['dependencies']) !== false) {
  44. $result[] = $id;
  45. }
  46. }
  47. return $result;
  48. }
  49. /**
  50. * Return the indexer data from the configuration.
  51. *
  52. * @param string $indexerId
  53. * @return array
  54. */
  55. private function getIndexerData(string $indexerId): array
  56. {
  57. return $this->config->getIndexer($indexerId);
  58. }
  59. /**
  60. * Return the indexer data from the configuration and validate this data.
  61. *
  62. * @param string $indexerId
  63. * @return array
  64. * @throws NoSuchEntityException In case when the indexer with the specified Id does not exist.
  65. */
  66. private function getIndexerDataWithValidation(string $indexerId): array
  67. {
  68. $indexerData = $this->getIndexerData($indexerId);
  69. if (!isset($indexerData['indexer_id']) || $indexerData['indexer_id'] != $indexerId) {
  70. throw new NoSuchEntityException(
  71. new Phrase("%1 indexer does not exist.", [$indexerId])
  72. );
  73. }
  74. return $indexerData;
  75. }
  76. }