SellerBuilder.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model\SignifydGateway\Request;
  7. use Magento\Directory\Model\RegionFactory;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Sales\Model\Order;
  10. use Magento\Sales\Model\Order\Shipment;
  11. use Magento\Store\Api\Data\StoreInterface;
  12. use Magento\Store\Model\Information;
  13. use Magento\Store\Model\ScopeInterface;
  14. /**
  15. * Prepare data related to the seller of the product.
  16. *
  17. * This information is optional unless you are operating a marketplace,
  18. * listing goods on behalf of multiple sellers who each hold a seller account registered with your site.
  19. */
  20. class SellerBuilder
  21. {
  22. /**
  23. * @var ScopeConfigInterface
  24. */
  25. private $scopeConfig;
  26. /**
  27. * @var RegionFactory
  28. */
  29. private $regionFactory;
  30. /**
  31. * @var array
  32. */
  33. private $regionCodes = [];
  34. /**
  35. * @param ScopeConfigInterface $scopeConfig
  36. * @param RegionFactory $regionFactory
  37. */
  38. public function __construct(
  39. ScopeConfigInterface $scopeConfig,
  40. RegionFactory $regionFactory
  41. ) {
  42. $this->scopeConfig = $scopeConfig;
  43. $this->regionFactory = $regionFactory;
  44. }
  45. /**
  46. * Returns seller data params
  47. *
  48. * @param Order $order
  49. * @return array
  50. */
  51. public function build(Order $order)
  52. {
  53. $store = $order->getStore();
  54. return [
  55. 'seller' => [
  56. 'name' => $this->getConfigValue(Information::XML_PATH_STORE_INFO_NAME, $store),
  57. 'domain' => $this->getPublicDomain($store),
  58. 'shipFromAddress' => [
  59. 'streetAddress' => $this->getConfigValue(Shipment::XML_PATH_STORE_ADDRESS1, $store),
  60. 'unit' => $this->getConfigValue(Shipment::XML_PATH_STORE_ADDRESS2, $store),
  61. 'city' => $this->getConfigValue(Shipment::XML_PATH_STORE_CITY, $store),
  62. 'provinceCode' => $this->getRegionCodeById(
  63. $this->getConfigValue(Shipment::XML_PATH_STORE_REGION_ID, $store)
  64. ),
  65. 'postalCode' => $this->getConfigValue(Shipment::XML_PATH_STORE_ZIP, $store),
  66. 'countryCode' => $this->getConfigValue(Shipment::XML_PATH_STORE_COUNTRY_ID, $store),
  67. ],
  68. 'corporateAddress' => [
  69. 'streetAddress' => $this->getConfigValue(Information::XML_PATH_STORE_INFO_STREET_LINE1, $store),
  70. 'unit' => $this->getConfigValue(Information::XML_PATH_STORE_INFO_STREET_LINE2, $store),
  71. 'city' => $this->getConfigValue(Information::XML_PATH_STORE_INFO_CITY, $store),
  72. 'provinceCode' => $this->getRegionCodeById(
  73. $this->getConfigValue(Information::XML_PATH_STORE_INFO_REGION_CODE, $store)
  74. ),
  75. 'postalCode' => $this->getConfigValue(Information::XML_PATH_STORE_INFO_POSTCODE, $store),
  76. 'countryCode' => $this->getConfigValue(Information::XML_PATH_STORE_INFO_COUNTRY_CODE, $store),
  77. ]
  78. ]
  79. ];
  80. }
  81. /**
  82. * Returns region code by id
  83. *
  84. * @param int $regionId
  85. * @return string
  86. */
  87. private function getRegionCodeById($regionId)
  88. {
  89. if (!isset($this->regionCodes[$regionId])) {
  90. $this->regionCodes[$regionId] = $this->regionFactory->create()->load($regionId)->getCode();
  91. }
  92. return $this->regionCodes[$regionId];
  93. }
  94. /**
  95. * Returns value from config
  96. *
  97. * @param string $value
  98. * @param StoreInterface $store
  99. * @return mixed
  100. */
  101. private function getConfigValue($value, StoreInterface $store)
  102. {
  103. return $this->scopeConfig->getValue(
  104. $value,
  105. ScopeInterface::SCOPE_STORE,
  106. $store
  107. );
  108. }
  109. /**
  110. * Returns public domain name
  111. *
  112. * @param StoreInterface $store
  113. * @return string|null null if no DNS records corresponding to a current host found
  114. */
  115. private function getPublicDomain(StoreInterface $store)
  116. {
  117. $baseUrl = $store->getBaseUrl();
  118. $domain = parse_url($baseUrl, PHP_URL_HOST);
  119. if (\function_exists('checkdnsrr') && false === \checkdnsrr($domain)) {
  120. return null;
  121. }
  122. return $domain;
  123. }
  124. }