ConfigElementFactory.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\GraphQl\Config;
  8. /**
  9. * This factory allows instantiation of all supported GraphQL config element objects.
  10. *
  11. * It automatically detects the type of the object to be instantiated based on the provided config data.
  12. */
  13. class ConfigElementFactory implements ConfigElementFactoryInterface
  14. {
  15. /**
  16. * @var ConfigElementFactoryInterface[]
  17. */
  18. private $factoryMapByConfigElementType;
  19. /**
  20. * @param ConfigElementFactoryInterface[] $factoryMapByConfigElementType
  21. */
  22. public function __construct(
  23. array $factoryMapByConfigElementType
  24. ) {
  25. $this->factoryMapByConfigElementType = $factoryMapByConfigElementType;
  26. }
  27. /**
  28. * Instantiate config element based on its type specified in $data
  29. *
  30. * @param array $data
  31. * @return ConfigElementInterface
  32. */
  33. public function createFromConfigData(array $data): ConfigElementInterface
  34. {
  35. if (!isset($this->factoryMapByConfigElementType[$data['type']])) {
  36. throw new \LogicException(
  37. sprintf('Factory is not configured for config element of "%s" type', $data['type'])
  38. );
  39. }
  40. return $this->factoryMapByConfigElementType[$data['type']]->createFromConfigData($data);
  41. }
  42. }