SaveHandlerFactory.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Indexer;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Framework\Indexer\SaveHandler\IndexerInterface as SaveHandlerInterface;
  9. /**
  10. * @api Instantiate save handler when implementing custom Indexer\Action
  11. * @since 100.0.2
  12. */
  13. class SaveHandlerFactory
  14. {
  15. /**
  16. * @var ObjectManagerInterface
  17. */
  18. protected $objectManager;
  19. /**
  20. * @param ObjectManagerInterface $objectManager
  21. */
  22. public function __construct(ObjectManagerInterface $objectManager)
  23. {
  24. $this->objectManager = $objectManager;
  25. }
  26. /**
  27. * Get handler class instance
  28. *
  29. * @param string $saveHandlerClass
  30. * @param array $arguments
  31. * @throws \InvalidArgumentException
  32. * @return IndexerInterface
  33. */
  34. public function create($saveHandlerClass, $arguments = [])
  35. {
  36. $handler = $this->objectManager->create($saveHandlerClass, $arguments);
  37. if (!$handler instanceof SaveHandlerInterface) {
  38. throw new \InvalidArgumentException(
  39. $saveHandlerClass . ' doesn\'t implement \Magento\Framework\IndexerInterface'
  40. );
  41. }
  42. return $handler;
  43. }
  44. }