RepositoryFactory.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Model\Entity;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Framework\Exception\NotFoundException;
  9. /**
  10. * Class RepositoryFactory
  11. */
  12. class RepositoryFactory
  13. {
  14. /**
  15. * List of entity types and their repositories
  16. *
  17. * @var array
  18. */
  19. protected $entities;
  20. /**
  21. * Object Manager
  22. *
  23. * @var ObjectManagerInterface
  24. */
  25. protected $objectManager;
  26. /**
  27. * RepositoryFactory constructor.
  28. *
  29. * @param ObjectManagerInterface $objectManager
  30. * @param array $entities
  31. */
  32. public function __construct(
  33. ObjectManagerInterface $objectManager,
  34. array $entities = []
  35. ) {
  36. $this->objectManager = $objectManager;
  37. $this->entities = $entities;
  38. }
  39. /**
  40. * @param string $entityType
  41. * @return object
  42. * @throws NotFoundException
  43. */
  44. public function create($entityType)
  45. {
  46. if (!isset($this->entities[$entityType])) {
  47. $message =
  48. sprintf('The repository for the "%s" entity type isn\'t declared. Verify and try again.', $entityType);
  49. throw new NotFoundException(new \Magento\Framework\Phrase($message));
  50. }
  51. return $this->objectManager->get($this->entities[$entityType]);
  52. }
  53. }