EntityUrl.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\UrlRewriteGraphQl\Model\Resolver;
  8. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  9. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  10. use Magento\Framework\GraphQl\Config\Element\Field;
  11. use Magento\Framework\GraphQl\Query\ResolverInterface;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. use Magento\UrlRewrite\Model\UrlFinderInterface;
  14. use Magento\UrlRewriteGraphQl\Model\Resolver\UrlRewrite\CustomUrlLocatorInterface;
  15. /**
  16. * UrlRewrite field resolver, used for GraphQL request processing.
  17. */
  18. class EntityUrl implements ResolverInterface
  19. {
  20. /**
  21. * @var UrlFinderInterface
  22. */
  23. private $urlFinder;
  24. /**
  25. * @var StoreManagerInterface
  26. */
  27. private $storeManager;
  28. /**
  29. * @var CustomUrlLocatorInterface
  30. */
  31. private $customUrlLocator;
  32. /**
  33. * @param UrlFinderInterface $urlFinder
  34. * @param StoreManagerInterface $storeManager
  35. * @param CustomUrlLocatorInterface $customUrlLocator
  36. */
  37. public function __construct(
  38. UrlFinderInterface $urlFinder,
  39. StoreManagerInterface $storeManager,
  40. CustomUrlLocatorInterface $customUrlLocator
  41. ) {
  42. $this->urlFinder = $urlFinder;
  43. $this->storeManager = $storeManager;
  44. $this->customUrlLocator = $customUrlLocator;
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function resolve(
  50. Field $field,
  51. $context,
  52. ResolveInfo $info,
  53. array $value = null,
  54. array $args = null
  55. ) {
  56. if (!isset($args['url']) || empty(trim($args['url']))) {
  57. throw new GraphQlInputException(__('"url" argument should be specified and not empty'));
  58. }
  59. $result = null;
  60. $url = $args['url'];
  61. if (substr($url, 0, 1) === '/' && $url !== '/') {
  62. $url = ltrim($url, '/');
  63. }
  64. $customUrl = $this->customUrlLocator->locateUrl($url);
  65. $url = $customUrl ?: $url;
  66. $urlRewrite = $this->findCanonicalUrl($url);
  67. if ($urlRewrite) {
  68. $result = [
  69. 'id' => $urlRewrite->getEntityId(),
  70. 'canonical_url' => $urlRewrite->getTargetPath(),
  71. 'type' => $this->sanitizeType($urlRewrite->getEntityType())
  72. ];
  73. }
  74. return $result;
  75. }
  76. /**
  77. * Find the canonical url passing through all redirects if any
  78. *
  79. * @param string $requestPath
  80. * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|null
  81. */
  82. private function findCanonicalUrl(string $requestPath) : ?\Magento\UrlRewrite\Service\V1\Data\UrlRewrite
  83. {
  84. $urlRewrite = $this->findUrlFromRequestPath($requestPath);
  85. if ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
  86. while ($urlRewrite && $urlRewrite->getRedirectType() > 0) {
  87. $urlRewrite = $this->findUrlFromRequestPath($urlRewrite->getTargetPath());
  88. }
  89. }
  90. if (!$urlRewrite) {
  91. $urlRewrite = $this->findUrlFromTargetPath($requestPath);
  92. }
  93. return $urlRewrite;
  94. }
  95. /**
  96. * Find a url from a request url on the current store
  97. *
  98. * @param string $requestPath
  99. * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|null
  100. */
  101. private function findUrlFromRequestPath(string $requestPath) : ?\Magento\UrlRewrite\Service\V1\Data\UrlRewrite
  102. {
  103. return $this->urlFinder->findOneByData(
  104. [
  105. 'request_path' => $requestPath,
  106. 'store_id' => $this->storeManager->getStore()->getId()
  107. ]
  108. );
  109. }
  110. /**
  111. * Find a url from a target url on the current store
  112. *
  113. * @param string $targetPath
  114. * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite|null
  115. */
  116. private function findUrlFromTargetPath(string $targetPath) : ?\Magento\UrlRewrite\Service\V1\Data\UrlRewrite
  117. {
  118. return $this->urlFinder->findOneByData(
  119. [
  120. 'target_path' => $targetPath,
  121. 'store_id' => $this->storeManager->getStore()->getId()
  122. ]
  123. );
  124. }
  125. /**
  126. * Sanitize the type to fit schema specifications
  127. *
  128. * @param string $type
  129. * @return string
  130. */
  131. private function sanitizeType(string $type) : string
  132. {
  133. return strtoupper(str_replace('-', '_', $type));
  134. }
  135. }