ScopeProxy.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Model\Indexer\Scope;
  7. use Magento\Framework\Search\Request\Dimension;
  8. /**
  9. * Implementation of IndexScopeResolverInterface which resolves index scope dynamically depending on current scope state
  10. */
  11. class ScopeProxy implements \Magento\Framework\Search\Request\IndexScopeResolverInterface
  12. {
  13. /**
  14. * Object Manager instance
  15. *
  16. * @var \Magento\Framework\ObjectManagerInterface
  17. */
  18. private $objectManager;
  19. /**
  20. * @var array
  21. */
  22. private $states = [];
  23. /**
  24. * @var State
  25. */
  26. private $scopeState;
  27. /**
  28. * Factory constructor
  29. *
  30. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  31. * @param State $scopeState
  32. * @param array $states
  33. */
  34. public function __construct(
  35. \Magento\Framework\ObjectManagerInterface $objectManager,
  36. State $scopeState,
  37. array $states
  38. ) {
  39. $this->objectManager = $objectManager;
  40. $this->scopeState = $scopeState;
  41. $this->states = $states;
  42. }
  43. /**
  44. * Creates class instance with specified parameters
  45. *
  46. * @param string $state
  47. * @return \Magento\Framework\Search\Request\IndexScopeResolverInterface
  48. * @throws UnknownStateException
  49. */
  50. private function create($state)
  51. {
  52. if (!array_key_exists($state, $this->states)) {
  53. throw new UnknownStateException(__("Requested resolver for unknown indexer state: $state"));
  54. }
  55. return $this->objectManager->create($this->states[$state]);
  56. }
  57. /**
  58. * @inheritdoc
  59. */
  60. public function resolve($index, array $dimensions)
  61. {
  62. return $this->create($this->scopeState->getState())->resolve($index, $dimensions);
  63. }
  64. }