Country.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\DirectoryGraphQl\Model\Resolver;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
  10. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  11. use Magento\Framework\GraphQl\Config\Element\Field;
  12. use Magento\Framework\GraphQl\Query\ResolverInterface;
  13. use Magento\Framework\Reflection\DataObjectProcessor;
  14. use Magento\Directory\Api\CountryInformationAcquirerInterface;
  15. use Magento\Directory\Api\Data\CountryInformationInterface;
  16. /**
  17. * Country field resolver, used for GraphQL request processing.
  18. */
  19. class Country implements ResolverInterface
  20. {
  21. /**
  22. * @var DataObjectProcessor
  23. */
  24. private $dataProcessor;
  25. /**
  26. * @var CountryInformationAcquirerInterface
  27. */
  28. private $countryInformationAcquirer;
  29. /**
  30. * @param DataObjectProcessor $dataProcessor
  31. * @param CountryInformationAcquirerInterface $countryInformationAcquirer
  32. */
  33. public function __construct(
  34. DataObjectProcessor $dataProcessor,
  35. CountryInformationAcquirerInterface $countryInformationAcquirer
  36. ) {
  37. $this->dataProcessor = $dataProcessor;
  38. $this->countryInformationAcquirer = $countryInformationAcquirer;
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public function resolve(
  44. Field $field,
  45. $context,
  46. ResolveInfo $info,
  47. array $value = null,
  48. array $args = null
  49. ) {
  50. try {
  51. $country = $this->countryInformationAcquirer->getCountryInfo($args['id']);
  52. } catch (NoSuchEntityException $exception) {
  53. throw new GraphQlNoSuchEntityException(__($exception->getMessage()), $exception);
  54. }
  55. return $this->dataProcessor->buildOutputDataArray(
  56. $country,
  57. CountryInformationInterface::class
  58. );
  59. }
  60. }