ScopeResolver.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\ConfigurationMismatchException;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. use Magento\Framework\Phrase;
  11. /**
  12. * Class ScopeResolver
  13. */
  14. class ScopeResolver
  15. {
  16. /**
  17. * @var ObjectManagerInterface
  18. */
  19. private $objectManager;
  20. /**
  21. * @var MetadataPool
  22. */
  23. private $metadataPool;
  24. /**
  25. * ScopeResolver constructor.
  26. * @param ObjectManagerInterface $objectManager
  27. * @param MetadataPool $metadataPool
  28. */
  29. public function __construct(
  30. ObjectManagerInterface $objectManager,
  31. MetadataPool $metadataPool
  32. ) {
  33. $this->objectManager = $objectManager;
  34. $this->metadataPool = $metadataPool;
  35. }
  36. /**
  37. * @param string $entityType
  38. * @param array|null $entityData
  39. * @return \Magento\Framework\Model\Entity\ScopeInterface[]
  40. * @throws ConfigurationMismatchException
  41. * @throws \Exception
  42. */
  43. public function getEntityContext($entityType, $entityData = [])
  44. {
  45. $entityContext = [];
  46. $metadata = $this->metadataPool->getMetadata($entityType);
  47. foreach ($metadata->getEntityContext() as $contextProviderClass) {
  48. $contextProvider = $this->objectManager->get($contextProviderClass);
  49. if (!$contextProvider instanceof ScopeProviderInterface) {
  50. throw new ConfigurationMismatchException(
  51. new Phrase(
  52. 'The configuration for the "%1" type is incorrect. '
  53. . 'Verify the type and configuration and try again.',
  54. [$entityType]
  55. )
  56. );
  57. }
  58. $entityContext[] = $contextProvider->getContext($entityType, $entityData);
  59. }
  60. return $entityContext;
  61. }
  62. }