src/Module/Api/UrlProvider.php line 62

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Module\Api;
  4. use App\Exception\ResourceNotFoundException;
  5. use App\Provider\ProductProvider;
  6. use App\Store\StoreContext;
  7. use Denios\SharedConstant\Storage\Prefix;
  8. use Elasticsearch\Client;
  9. use Exception;
  10. use Locale;
  11. /**
  12. * Provides methods to retrieve the url data in the action
  13. *
  14. * @package App\Module\Api
  15. */
  16. class UrlProvider
  17. {
  18. /**
  19. * @var Client
  20. */
  21. private Client $esClient;
  22. /**
  23. * @var ProductProvider
  24. */
  25. private ProductProvider $productProvider;
  26. /**
  27. * @var StoreContext
  28. */
  29. private StoreContext $storeContext;
  30. /**
  31. *
  32. * @param ProductProvider $productProvider
  33. * @param Client $esClient
  34. * @param StoreContext $storeContext
  35. */
  36. public function __construct(ProductProvider $productProvider, Client $esClient, StoreContext $storeContext)
  37. {
  38. $this->productProvider = $productProvider;
  39. $this->esClient = $esClient;
  40. $this->storeContext = $storeContext;
  41. }
  42. /**
  43. * Return the full url for a given sku.
  44. *
  45. * @param int $sku
  46. *
  47. * @throws Exception
  48. * @throws ResourceNotFoundException
  49. *
  50. * @return string
  51. */
  52. public function getProductUrlBySku(int $sku): string
  53. {
  54. $product = $this->productProvider->getProductBySku((string) $sku);
  55. if (!$product) {
  56. throw new ResourceNotFoundException(sprintf('Could not find a product for SKU %s', $sku));
  57. }
  58. if (isset($product->links->canonical)) {
  59. return $product->links->canonical;
  60. } elseif (isset($product->links->webshop)) {
  61. return $product->links->webshop;
  62. } else {
  63. // this should never happen, because links->webshop is a required property
  64. throw new Exception(sprintf('Product found for sku %s, but no link provided in data from database', $sku));
  65. }
  66. }
  67. /**
  68. * Return the full url for a given sku.
  69. *
  70. * @param int $sku
  71. *
  72. * @throws Exception
  73. * @throws ResourceNotFoundException
  74. *
  75. * @return string
  76. */
  77. public function getVariantUrlBySku(int $sku): string
  78. {
  79. $product = $this->productProvider->getProductBySku((string) $sku);
  80. if (!$product) {
  81. throw new ResourceNotFoundException(sprintf('Could not find a product for SKU %s', $sku));
  82. }
  83. foreach ($product->variants as $variant) {
  84. if ($variant->sku === (string) $sku && isset($variant->links->webshop)) {
  85. return $variant->links->webshop;
  86. }
  87. }
  88. if (isset($product->links->webshop)) {
  89. return $product->links->webshop;
  90. } elseif (isset($product->links->canonical)) {
  91. return $product->links->canonical;
  92. } else {
  93. // this should never happen, because links->webshop is a required property
  94. throw new Exception(sprintf('Product found for sku %s, but no link provided in data from database', $sku));
  95. }
  96. }
  97. /**
  98. * Return the full url for a given magento id
  99. *
  100. * @param int $magentoId
  101. *
  102. * @throws Exception
  103. * @throws ResourceNotFoundException
  104. *
  105. * @return string
  106. */
  107. public function getCategoryUrlByMagentoId(int $magentoId): string
  108. {
  109. $index = implode('_', [
  110. $this->storeContext->getDefaultLocale(),
  111. strtolower(Locale::canonicalize($this->storeContext->getLocaleInfo()->getCurrentLocale())),
  112. Prefix::CATEGORY
  113. ]);
  114. // raw elasticsearch response
  115. $searchRawData = $this->esClient->search([
  116. 'index' => $index,
  117. 'body' => [
  118. 'query' => [
  119. 'term' => [
  120. 'magentoId' => $magentoId
  121. ]
  122. ]
  123. ],
  124. 'size' => 1
  125. ]);
  126. if (!isset($searchRawData['hits']['hits'][0]['_source'])) {
  127. throw new ResourceNotFoundException(sprintf('Could not find a category for magentoId %s', $magentoId));
  128. }
  129. // array structure for Denios\Data\Catalog\Category;
  130. $categoryData = $searchRawData['hits']['hits'][0]['_source'];
  131. if (!isset($categoryData['links']['webshop'])) {
  132. // this should also never happen, because it is a required property of Category
  133. throw new Exception(sprintf(
  134. "Category for magentoId %s returned data, which is missing property 'links.webshop'",
  135. $magentoId
  136. ));
  137. }
  138. return $categoryData['links']['webshop'];
  139. }
  140. }