OrderAdapter.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest;
  6. use Psr\Log\LoggerInterface;
  7. use Psr\Log\LogLevel;
  8. use Temando\Shipping\Rest\Adapter\OrderApiInterface;
  9. use Temando\Shipping\Rest\Exception\AdapterException;
  10. use Temando\Shipping\Rest\Exception\RestClientErrorException;
  11. use Temando\Shipping\Rest\Request\OrderRequest;
  12. use Temando\Shipping\Rest\Request\RequestHeadersInterface;
  13. use Temando\Shipping\Rest\Response\Document\Errors;
  14. use Temando\Shipping\Rest\Response\Document\SaveOrder;
  15. use Temando\Shipping\Rest\Response\Document\SaveOrderInterface;
  16. use Temando\Shipping\Rest\SchemaMapper\ParserInterface;
  17. use Temando\Shipping\Webservice\Config\WsConfigInterface;
  18. /**
  19. * Temando REST API Order Operations Adapter
  20. *
  21. * @package Temando\Shipping\Rest
  22. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  23. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  24. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * @link https://www.temando.com/
  26. */
  27. class OrderAdapter implements OrderApiInterface
  28. {
  29. /**
  30. * @var string
  31. */
  32. private $endpoint;
  33. /**
  34. * @var string
  35. */
  36. private $accountId;
  37. /**
  38. * @var string
  39. */
  40. private $bearerToken;
  41. /**
  42. * @var RequestHeadersInterface
  43. */
  44. private $requestHeaders;
  45. /**
  46. * @var AuthenticationInterface
  47. */
  48. private $auth;
  49. /**
  50. * @var RestClientInterface
  51. */
  52. private $restClient;
  53. /**
  54. * @var ParserInterface
  55. */
  56. private $responseParser;
  57. /**
  58. * @var LoggerInterface
  59. */
  60. private $logger;
  61. /**
  62. * OrderAdapter constructor.
  63. * @param WsConfigInterface $config
  64. * @param RequestHeadersInterface $requestHeaders
  65. * @param AuthenticationInterface $auth
  66. * @param RestClientInterface $restClient
  67. * @param ParserInterface $responseParser
  68. * @param LoggerInterface $logger
  69. */
  70. public function __construct(
  71. WsConfigInterface $config,
  72. RequestHeadersInterface $requestHeaders,
  73. AuthenticationInterface $auth,
  74. RestClientInterface $restClient,
  75. ParserInterface $responseParser,
  76. LoggerInterface $logger
  77. ) {
  78. $this->endpoint = $config->getApiEndpoint();
  79. $this->accountId = $config->getAccountId();
  80. $this->bearerToken = $config->getBearerToken();
  81. $this->requestHeaders = $requestHeaders;
  82. $this->auth = $auth;
  83. $this->restClient = $restClient;
  84. $this->responseParser = $responseParser;
  85. $this->logger = $logger;
  86. }
  87. /**
  88. * Create order.
  89. *
  90. * @param OrderRequest $request
  91. * @return SaveOrderInterface
  92. * @throws AdapterException
  93. */
  94. public function createOrder(OrderRequest $request)
  95. {
  96. $requestParams = $request->getRequestParams();
  97. $uri = sprintf('%s/orders?%s', $this->endpoint, http_build_query($requestParams));
  98. $requestBody = $request->getRequestBody();
  99. $this->logger->log(LogLevel::DEBUG, sprintf("%s\n%s", $uri, $requestBody));
  100. try {
  101. $this->auth->connect($this->accountId, $this->bearerToken);
  102. $headers = $this->requestHeaders->getHeaders();
  103. $rawResponse = $this->restClient->post($uri, $requestBody, $headers);
  104. $this->logger->log(LogLevel::DEBUG, $rawResponse);
  105. /** @var SaveOrder $response */
  106. $response = $this->responseParser->parse($rawResponse, SaveOrder::class);
  107. } catch (RestClientErrorException $e) {
  108. $this->logger->log(LogLevel::ERROR, $e->getMessage());
  109. /** @var Errors $response */
  110. $response = $this->responseParser->parse($e->getMessage(), Errors::class);
  111. throw AdapterException::errorResponse($response, $e);
  112. } catch (\Exception $e) {
  113. throw AdapterException::create($e);
  114. }
  115. return $response;
  116. }
  117. /**
  118. * Update order.
  119. *
  120. * @param OrderRequest $request
  121. * @return SaveOrderInterface
  122. * @throws AdapterException
  123. */
  124. public function updateOrder(OrderRequest $request)
  125. {
  126. $uri = sprintf('%s/orders/%s', $this->endpoint, ...$request->getPathParams());
  127. $requestBody = $request->getRequestBody();
  128. $this->logger->log(LogLevel::DEBUG, sprintf("%s\n%s", $uri, $requestBody));
  129. try {
  130. $this->auth->connect($this->accountId, $this->bearerToken);
  131. $headers = $this->requestHeaders->getHeaders();
  132. $rawResponse = $this->restClient->put($uri, $requestBody, $headers);
  133. $this->logger->log(LogLevel::DEBUG, $rawResponse);
  134. /** @var SaveOrder $response */
  135. $response = $this->responseParser->parse($rawResponse, SaveOrder::class);
  136. } catch (RestClientErrorException $e) {
  137. $this->logger->log(LogLevel::ERROR, $e->getMessage());
  138. /** @var Errors $response */
  139. $response = $this->responseParser->parse($e->getMessage(), Errors::class);
  140. throw AdapterException::errorResponse($response, $e);
  141. } catch (\Exception $e) {
  142. throw AdapterException::create($e);
  143. }
  144. return $response;
  145. }
  146. }