UrlRewriteFinder.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogUrlRewrite\Model\Map;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\UrlRewrite\Model\UrlFinderInterface;
  9. use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
  10. use Magento\UrlRewrite\Service\V1\Data\UrlRewriteFactory;
  11. /**
  12. * Finds specific queried url rewrites identified by specific fields
  13. *
  14. * A group of identifiers specifies a query consumed by the client to retrieve existing url rewrites from the database
  15. * Clients will query a map of DatabaseMapInterface type through this class resulting into a set of url rewrites results
  16. * Each map type will fallback to a UrlFinderInterface by identifiers for unmapped values
  17. */
  18. class UrlRewriteFinder
  19. {
  20. const ENTITY_TYPE_CATEGORY = 'category';
  21. const ENTITY_TYPE_PRODUCT = 'product';
  22. /**
  23. * @var \Magento\CatalogUrlRewrite\Model\Map\DatabaseMapPool
  24. */
  25. private $databaseMapPool;
  26. /**
  27. * @var \Magento\UrlRewrite\Model\UrlFinderInterface
  28. */
  29. private $urlFinder;
  30. /**
  31. * @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite
  32. */
  33. private $urlRewritePrototype;
  34. /**
  35. * @var array
  36. */
  37. private $urlRewriteClassNames = [];
  38. /**
  39. * @param DatabaseMapPool $databaseMapPool
  40. * @param UrlFinderInterface $urlFinder
  41. * @param UrlRewriteFactory $urlRewriteFactory
  42. * @param string[] $urlRewriteClassNames
  43. */
  44. public function __construct(
  45. DatabaseMapPool $databaseMapPool,
  46. UrlFinderInterface $urlFinder,
  47. UrlRewriteFactory $urlRewriteFactory,
  48. array $urlRewriteClassNames = []
  49. ) {
  50. $this->databaseMapPool = $databaseMapPool;
  51. $this->urlFinder = $urlFinder;
  52. $this->urlRewriteClassNames = $urlRewriteClassNames;
  53. $this->urlRewritePrototype = $urlRewriteFactory->create();
  54. }
  55. /**
  56. * Retrieves existing url rewrites filtered by identifiers from prebuild database maps
  57. * This method will fall-back to by using UrlFinderInterface when map type is not found in configured list
  58. *
  59. * @param int $entityId
  60. * @param int $storeId
  61. * @param string $entityType
  62. * @param int|null $rootCategoryId
  63. * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
  64. */
  65. public function findAllByData($entityId, $storeId, $entityType, $rootCategoryId = null)
  66. {
  67. if ($rootCategoryId
  68. && is_numeric($entityId)
  69. && is_numeric($storeId)
  70. && is_string($entityType)
  71. && isset($this->urlRewriteClassNames[$entityType])
  72. ) {
  73. $map = $this->databaseMapPool->getDataMap($this->urlRewriteClassNames[$entityType], $rootCategoryId);
  74. if ($map) {
  75. $key = $storeId . '_' . $entityId;
  76. return $this->arrayToUrlRewriteObject($map->getData($rootCategoryId, $key));
  77. }
  78. }
  79. return $this->urlFinder->findAllByData(
  80. [
  81. UrlRewrite::STORE_ID => $storeId,
  82. UrlRewrite::ENTITY_ID => $entityId,
  83. UrlRewrite::ENTITY_TYPE => $entityType
  84. ]
  85. );
  86. }
  87. /**
  88. * Transfers an array values to url rewrite object values
  89. *
  90. * @param array $data
  91. * @return UrlRewrite[]
  92. */
  93. private function arrayToUrlRewriteObject(array $data)
  94. {
  95. foreach ($data as $key => $array) {
  96. $data[$key] = $this->createUrlRewrite($array);
  97. }
  98. return $data;
  99. }
  100. /**
  101. * Creates url rewrite object and sets $data to its properties by key->value
  102. *
  103. * @param array $data
  104. * @return UrlRewrite
  105. */
  106. private function createUrlRewrite(array $data)
  107. {
  108. $dataObject = clone $this->urlRewritePrototype;
  109. $dataObject->setUrlRewriteId($data['url_rewrite_id']);
  110. $dataObject->setEntityType($data['entity_type']);
  111. $dataObject->setEntityId($data['entity_id']);
  112. $dataObject->setRequestPath($data['request_path']);
  113. $dataObject->setTargetPath($data['target_path']);
  114. $dataObject->setRedirectType($data['redirect_type']);
  115. $dataObject->setStoreId($data['store_id']);
  116. $dataObject->setDescription($data['description']);
  117. $dataObject->setIsAutogenerated($data['is_autogenerated']);
  118. $dataObject->setMetadata($data['metadata']);
  119. return $dataObject;
  120. }
  121. }