IntervalFactory.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Search\Dynamic;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Framework\Search\EngineResolverInterface;
  9. /**
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class IntervalFactory
  14. {
  15. /**
  16. * @var string
  17. */
  18. private $interval;
  19. /**
  20. * @var ObjectManagerInterface
  21. */
  22. private $objectManager;
  23. /**
  24. * @param ObjectManagerInterface $objectManager
  25. * @param EngineResolverInterface $engineResolver
  26. * @param string[] $intervals
  27. */
  28. public function __construct(
  29. ObjectManagerInterface $objectManager,
  30. EngineResolverInterface $engineResolver,
  31. $intervals
  32. ) {
  33. $this->objectManager = $objectManager;
  34. $configValue = $engineResolver->getCurrentSearchEngine();
  35. if (isset($intervals[$configValue])) {
  36. $this->interval = $intervals[$configValue];
  37. } else {
  38. throw new \LogicException("Interval not found by config {$configValue}");
  39. }
  40. }
  41. /**
  42. * Create interval
  43. *
  44. * @param array $data
  45. * @return IntervalInterface
  46. */
  47. public function create(array $data = [])
  48. {
  49. $interval = $this->objectManager->create($this->interval, $data);
  50. if (!$interval instanceof IntervalInterface) {
  51. throw new \LogicException(
  52. 'Interval not instance of interface ' . IntervalInterface::class
  53. );
  54. }
  55. return $interval;
  56. }
  57. }