ConfigBuilder.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\Api;
  7. use Magento\Store\Model\ScopeInterface;
  8. use Vertex\Data\ConfigurationInterface;
  9. use Vertex\Data\ConfigurationInterfaceFactory;
  10. use Vertex\Data\LoginInterface;
  11. use Vertex\Data\LoginInterfaceFactory;
  12. use Vertex\Tax\Model\Config as ModuleConfig;
  13. /**
  14. * Creates a {@see ConfigurationInterface} for use with the Vertex API library
  15. */
  16. class ConfigBuilder
  17. {
  18. /** @var ConfigurationInterfaceFactory */
  19. private $configFactory;
  20. /** @var LoginInterfaceFactory */
  21. private $loginFactory;
  22. /** @var ModuleConfig */
  23. private $moduleConfig;
  24. /** @var string|null */
  25. private $scopeCode;
  26. /** @var string */
  27. private $scopeType = ScopeInterface::SCOPE_STORE;
  28. public function __construct(
  29. ModuleConfig $moduleConfig,
  30. ConfigurationInterfaceFactory $configFactory,
  31. LoginInterfaceFactory $loginFactory
  32. ) {
  33. $this->moduleConfig = $moduleConfig;
  34. $this->configFactory = $configFactory;
  35. $this->loginFactory = $loginFactory;
  36. }
  37. /**
  38. * Create a {@see ConfigurationInterface} object for use with the Vertex API
  39. *
  40. * @return ConfigurationInterface
  41. */
  42. public function build()
  43. {
  44. /** @var ConfigurationInterface $configuration */
  45. $configuration = $this->configFactory->create();
  46. /** @var LoginInterface $login */
  47. $login = $this->loginFactory->create();
  48. $login->setTrustedId($this->moduleConfig->getTrustedId($this->scopeCode, $this->scopeType));
  49. $configuration->setLogin($login);
  50. $configuration->setTaxAreaLookupWsdl($this->getTaxAreaLookupWsdl());
  51. $configuration->setTaxCalculationWsdl($this->getTaxCalculationWsdl());
  52. return $configuration;
  53. }
  54. /**
  55. * Set the Scope Code
  56. *
  57. * @param string|null $scopeCode
  58. * @return ConfigBuilder
  59. */
  60. public function setScopeCode($scopeCode)
  61. {
  62. $this->scopeCode = $scopeCode;
  63. return $this;
  64. }
  65. /**
  66. * Set the Scope Type
  67. *
  68. * @param string|null $scopeType
  69. * @return ConfigBuilder
  70. */
  71. public function setScopeType($scopeType)
  72. {
  73. $this->scopeType = $scopeType;
  74. return $this;
  75. }
  76. /**
  77. * Assemble a URL
  78. *
  79. * @param string[] $urlParts indexed as parse_url would index them
  80. * @return string
  81. */
  82. private function assembleUrl($urlParts)
  83. {
  84. $url = $urlParts['scheme'] . '://' . $urlParts['host'] . $urlParts['path'];
  85. if (isset($urlParts['query'])) {
  86. $url .= '?' . $urlParts['query'];
  87. }
  88. if (isset($urlParts['fragment'])) {
  89. $url .= '#' . $urlParts['fragment'];
  90. }
  91. return $url;
  92. }
  93. /**
  94. * Add a WSDL query parameter if one does not exist on the URL
  95. *
  96. * @param string $url
  97. * @return string
  98. */
  99. private function ensureWsdlQuery($url)
  100. {
  101. $urlParts = parse_url($url);
  102. $query = isset($urlParts['query']) ? $urlParts['query'] : null;
  103. $wsdlFound = false;
  104. if ($query !== null) {
  105. $queryParts = explode('&', $query);
  106. foreach ($queryParts as $parameter) {
  107. $parameterParts = explode('=', $parameter);
  108. $name = $parameterParts[0];
  109. if (strtolower($name) === 'wsdl') {
  110. $wsdlFound = true;
  111. break;
  112. }
  113. }
  114. }
  115. if (!$wsdlFound) {
  116. $urlParts['query'] = $query . (empty($query) ? 'wsdl' : '&wsdl');
  117. }
  118. return $this->assembleUrl($urlParts);
  119. }
  120. /**
  121. * Retrieve the Tax Area Lookup WSDL URL
  122. *
  123. * @return string
  124. */
  125. private function getTaxAreaLookupWsdl()
  126. {
  127. return $this->ensureWsdlQuery($this->moduleConfig->getVertexAddressHost($this->scopeCode, $this->scopeType));
  128. }
  129. /**
  130. * Retrieve the Tax Calculation WSDL URL
  131. *
  132. * @return string
  133. */
  134. private function getTaxCalculationWsdl()
  135. {
  136. return $this->ensureWsdlQuery($this->moduleConfig->getVertexHost($this->scopeCode, $this->scopeType));
  137. }
  138. }