ServiceUrl.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model\Argument\Interpreter;
  7. use Magento\Framework\Data\Argument\InterpreterInterface;
  8. use Magento\Store\Model\StoreRepository;
  9. use Magento\Store\Model\StoreManagerInterface;
  10. /**
  11. * Interpreter that builds Service URL by input path and optional parameters
  12. */
  13. class ServiceUrl implements InterpreterInterface
  14. {
  15. /**
  16. * @var \Magento\Framework\Url
  17. */
  18. private $url;
  19. /**
  20. * @var string
  21. */
  22. private $service;
  23. /**
  24. * @var StoreManagerInterface
  25. */
  26. private $storeManager;
  27. /**
  28. * @var string
  29. */
  30. private $version;
  31. /**
  32. * @var StoreRepository
  33. */
  34. private $storeRepository;
  35. /**
  36. * @param \Magento\Framework\Url $url
  37. * @param StoreManagerInterface $storeManager
  38. * @param StoreRepository $storeRepository
  39. * @param string $service
  40. * @param string $version
  41. */
  42. public function __construct(
  43. \Magento\Framework\Url $url,
  44. StoreManagerInterface $storeManager,
  45. StoreRepository $storeRepository,
  46. $service = "rest",
  47. $version = "V1"
  48. ) {
  49. $this->url = $url;
  50. $this->service = $service;
  51. $this->storeManager = $storeManager;
  52. $this->version = $version;
  53. $this->storeRepository = $storeRepository;
  54. }
  55. /**
  56. * Prepare rest suffix for url. For example rest/default/V1
  57. *
  58. * @return string
  59. */
  60. private function getServiceUrl()
  61. {
  62. $store = $this->storeRepository->getById($this->storeManager->getStore()->getId());
  63. return $this->url->getUrl(
  64. $this->service . "/" . $store->getCode() . "/" . $this->version
  65. );
  66. }
  67. /**
  68. * Compute and return effective value of an argument
  69. *
  70. * @param array $data
  71. * @return string
  72. * @throws \InvalidArgumentException
  73. */
  74. public function evaluate(array $data)
  75. {
  76. if (!isset($data['path']) || empty($data['path'])) {
  77. throw new \InvalidArgumentException('URL path is missing.');
  78. }
  79. if (isset($data['service'])) {
  80. $this->service = "rest";
  81. }
  82. if (isset($data["version"])) {
  83. $this->version = $data["version"];
  84. }
  85. return $this->getServiceUrl() . ltrim($data["path"], "/");
  86. }
  87. }