HttpClientFactory.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Model\SignifydGateway\Client;
  7. use Magento\Framework\HTTP\ZendClient;
  8. use Magento\Framework\HTTP\ZendClientFactory;
  9. use Magento\Framework\Json\EncoderInterface;
  10. use Magento\Signifyd\Model\Config;
  11. /**
  12. * Class HttpClientFactory
  13. * Creates and configures HTTP client for RequestBuilder
  14. */
  15. class HttpClientFactory
  16. {
  17. /**
  18. * Specifies basic HTTP access authentication Header.
  19. *
  20. * @var string
  21. */
  22. private static $authorizationType = 'Authorization';
  23. /**
  24. * JSON HTTP Content-Type Header.
  25. *
  26. * @var string
  27. */
  28. private static $jsonDataType = 'application/json';
  29. /**
  30. * @var string
  31. */
  32. private static $urlSeparator = '/';
  33. /**
  34. * @var Config
  35. */
  36. private $config;
  37. /**
  38. * @var ZendClientFactory
  39. */
  40. private $clientFactory;
  41. /**
  42. * @var EncoderInterface
  43. */
  44. private $dataEncoder;
  45. /**
  46. * HttpClientCreator constructor.
  47. *
  48. * @param Config $config
  49. * @param ZendClientFactory $clientFactory
  50. * @param EncoderInterface $dataEncoder
  51. */
  52. public function __construct(
  53. Config $config,
  54. ZendClientFactory $clientFactory,
  55. EncoderInterface $dataEncoder
  56. ) {
  57. $this->config = $config;
  58. $this->clientFactory = $clientFactory;
  59. $this->dataEncoder = $dataEncoder;
  60. }
  61. /**
  62. * Creates and configures HTTP client.
  63. *
  64. * @param string $url
  65. * @param string $method
  66. * @param array $params
  67. * @param int|null $storeId
  68. * @return ZendClient
  69. */
  70. public function create($url, $method, array $params = [], $storeId = null): ZendClient
  71. {
  72. $apiKey = $this->getApiKey($storeId);
  73. $apiUrl = $this->buildFullApiUrl($url, $storeId);
  74. $client = $this->createNewClient();
  75. $client->setHeaders(
  76. self::$authorizationType,
  77. sprintf('Basic %s', base64_encode($apiKey))
  78. );
  79. if (!empty($params)) {
  80. $encodedData = $this->dataEncoder->encode($params);
  81. $client->setRawData($encodedData, self::$jsonDataType);
  82. }
  83. $client->setMethod($method);
  84. $client->setUri($apiUrl);
  85. return $client;
  86. }
  87. /**
  88. * @return ZendClient
  89. */
  90. private function createNewClient()
  91. {
  92. return $this->clientFactory->create();
  93. }
  94. /**
  95. * Signifyd API key for merchant account.
  96. *
  97. * @see https://www.signifyd.com/docs/api/#/introduction/authentication
  98. * @param int|null $storeId
  99. * @return string
  100. */
  101. private function getApiKey($storeId): string
  102. {
  103. return $this->config->getApiKey($storeId);
  104. }
  105. /**
  106. * Full URL for Singifyd API based on relative URL.
  107. *
  108. * @param string $url
  109. * @param int|null $storeId
  110. * @return string
  111. */
  112. private function buildFullApiUrl($url, $storeId): string
  113. {
  114. $baseApiUrl = $this->getBaseApiUrl($storeId);
  115. $fullUrl = $baseApiUrl . self::$urlSeparator . ltrim($url, self::$urlSeparator);
  116. return $fullUrl;
  117. }
  118. /**
  119. * Base Sigifyd API URL without trailing slash.
  120. *
  121. * @param int|null $storeId
  122. * @return string
  123. */
  124. private function getBaseApiUrl($storeId): string
  125. {
  126. $baseApiUrl = $this->config->getApiUrl($storeId);
  127. return rtrim($baseApiUrl, self::$urlSeparator);
  128. }
  129. }