ExperienceAdapter.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\ExperienceApiInterface;
  9. use Temando\Shipping\Rest\Exception\AdapterException;
  10. use Temando\Shipping\Rest\Exception\RestClientErrorException;
  11. use Temando\Shipping\Rest\Request\ListRequestInterface;
  12. use Temando\Shipping\Rest\Request\QualifyRequest;
  13. use Temando\Shipping\Rest\Request\RequestHeadersInterface;
  14. use Temando\Shipping\Rest\Response\DataObject\Experience;
  15. use Temando\Shipping\Rest\Response\DataObject\OrderQualification;
  16. use Temando\Shipping\Rest\Response\Document\Errors;
  17. use Temando\Shipping\Rest\Response\Document\GetExperiences;
  18. use Temando\Shipping\Rest\Response\Document\QualifyOrder;
  19. use Temando\Shipping\Rest\SchemaMapper\ParserInterface;
  20. use Temando\Shipping\Webservice\Config\WsConfigInterface;
  21. /**
  22. * Temando REST API Experience Operations Adapter
  23. *
  24. * @package Temando\Shipping\Rest
  25. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  26. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  27. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  28. * @link https://www.temando.com/
  29. */
  30. class ExperienceAdapter implements ExperienceApiInterface
  31. {
  32. /**
  33. * @var string
  34. */
  35. private $endpoint;
  36. /**
  37. * @var string
  38. */
  39. private $accountId;
  40. /**
  41. * @var string
  42. */
  43. private $bearerToken;
  44. /**
  45. * @var RequestHeadersInterface
  46. */
  47. private $requestHeaders;
  48. /**
  49. * @var AuthenticationInterface
  50. */
  51. private $auth;
  52. /**
  53. * @var RestClientInterface
  54. */
  55. private $restClient;
  56. /**
  57. * @var ParserInterface
  58. */
  59. private $responseParser;
  60. /**
  61. * @var LoggerInterface
  62. */
  63. private $logger;
  64. /**
  65. * ExperienceAdapter constructor.
  66. * @param WsConfigInterface $config
  67. * @param RequestHeadersInterface $requestHeaders
  68. * @param AuthenticationInterface $auth
  69. * @param RestClientInterface $restClient
  70. * @param ParserInterface $responseParser
  71. * @param LoggerInterface $logger
  72. */
  73. public function __construct(
  74. WsConfigInterface $config,
  75. RequestHeadersInterface $requestHeaders,
  76. AuthenticationInterface $auth,
  77. RestClientInterface $restClient,
  78. ParserInterface $responseParser,
  79. LoggerInterface $logger
  80. ) {
  81. $this->endpoint = $config->getApiEndpoint();
  82. $this->accountId = $config->getAccountId();
  83. $this->bearerToken = $config->getBearerToken();
  84. $this->requestHeaders = $requestHeaders;
  85. $this->auth = $auth;
  86. $this->restClient = $restClient;
  87. $this->responseParser = $responseParser;
  88. $this->logger = $logger;
  89. }
  90. /**
  91. * Retrieve shipping options for the current quote.
  92. *
  93. * @param QualifyRequest $request
  94. * @return OrderQualification[]
  95. * @throws AdapterException
  96. */
  97. public function qualify(QualifyRequest $request)
  98. {
  99. $uri = sprintf('%s/qualify', $this->endpoint);
  100. $requestBody = $request->getRequestBody();
  101. $this->logger->log(LogLevel::DEBUG, sprintf("%s\n%s", $uri, $requestBody));
  102. try {
  103. $this->auth->connect($this->accountId, $this->bearerToken);
  104. $headers = $this->requestHeaders->getHeaders();
  105. $rawResponse = $this->restClient->post($uri, $requestBody, $headers);
  106. $this->logger->log(LogLevel::DEBUG, $rawResponse);
  107. /** @var QualifyOrder $response */
  108. $response = $this->responseParser->parse($rawResponse, QualifyOrder::class);
  109. $qualifications = $response->getData();
  110. } catch (RestClientErrorException $e) {
  111. $this->logger->log(LogLevel::ERROR, $e->getMessage());
  112. /** @var Errors $response */
  113. $response = $this->responseParser->parse($e->getMessage(), Errors::class);
  114. throw AdapterException::errorResponse($response, $e);
  115. } catch (\Exception $e) {
  116. throw AdapterException::create($e);
  117. }
  118. return $qualifications;
  119. }
  120. /**
  121. * Obtain shipping experiences.
  122. *
  123. * @param ListRequestInterface $request
  124. * @return Experience[]
  125. * @throws AdapterException
  126. */
  127. public function getExperiences(ListRequestInterface $request)
  128. {
  129. $uri = sprintf('%s/experiences', $this->endpoint);
  130. $queryParams = $request->getRequestParams();
  131. $this->logger->log(LogLevel::DEBUG, sprintf('%s?%s', $uri, http_build_query($queryParams)));
  132. try {
  133. $this->auth->connect($this->accountId, $this->bearerToken);
  134. $headers = $this->requestHeaders->getHeaders();
  135. $rawResponse = $this->restClient->get($uri, $queryParams, $headers);
  136. $this->logger->log(LogLevel::DEBUG, $rawResponse);
  137. /** @var GetExperiences $response */
  138. $response = $this->responseParser->parse($rawResponse, GetExperiences::class);
  139. $experiences = $response->getData();
  140. } catch (RestClientErrorException $e) {
  141. $this->logger->log(LogLevel::ERROR, $e->getMessage());
  142. /** @var Errors $response */
  143. $response = $this->responseParser->parse($e->getMessage(), Errors::class);
  144. throw AdapterException::errorResponse($response, $e);
  145. } catch (\Exception $e) {
  146. $this->logger->critical($e->getMessage(), ['exception' => $e]);
  147. $experiences = [];
  148. }
  149. return $experiences;
  150. }
  151. }