FulfillmentAdapter.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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\FulfillmentApiInterface;
  9. use Temando\Shipping\Rest\Exception\AdapterException;
  10. use Temando\Shipping\Rest\Exception\RestClientErrorException;
  11. use Temando\Shipping\Rest\Request\FulfillmentRequestInterface;
  12. use Temando\Shipping\Rest\Request\ItemRequestInterface;
  13. use Temando\Shipping\Rest\Request\ListRequestInterface;
  14. use Temando\Shipping\Rest\Request\RequestHeadersInterface;
  15. use Temando\Shipping\Rest\Response\DataObject\Fulfillment;
  16. use Temando\Shipping\Rest\Response\Document\Errors;
  17. use Temando\Shipping\Rest\Response\Document\GetFulfillment;
  18. use Temando\Shipping\Rest\Response\Document\GetFulfillments;
  19. use Temando\Shipping\Rest\SchemaMapper\ParserInterface;
  20. use Temando\Shipping\Webservice\Config\WsConfigInterface;
  21. /**
  22. * Temando REST API Fulfillment 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 FulfillmentAdapter implements FulfillmentApiInterface
  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. * OrderAdapter 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. * @param ItemRequestInterface $request
  92. *
  93. * @return Fulfillment
  94. * @throws AdapterException
  95. */
  96. public function getFulfillment(ItemRequestInterface $request)
  97. {
  98. $uri = sprintf('%s/fulfillments/%s', $this->endpoint, ...$request->getPathParams());
  99. $this->logger->log(LogLevel::DEBUG, $uri);
  100. try {
  101. $this->auth->connect($this->accountId, $this->bearerToken);
  102. $headers = $this->requestHeaders->getHeaders();
  103. $rawResponse = $this->restClient->get($uri, [], $headers);
  104. $this->logger->log(LogLevel::DEBUG, $rawResponse);
  105. /** @var GetFulfillment $response */
  106. $response = $this->responseParser->parse($rawResponse, GetFulfillment::class);
  107. $fulfillment = $response->getData();
  108. } catch (RestClientErrorException $e) {
  109. $this->logger->log(LogLevel::ERROR, $e->getMessage());
  110. /** @var Errors $response */
  111. $response = $this->responseParser->parse($e->getMessage(), Errors::class);
  112. throw AdapterException::errorResponse($response, $e);
  113. } catch (\Exception $e) {
  114. throw AdapterException::create($e);
  115. }
  116. return $fulfillment;
  117. }
  118. /**
  119. * @param ListRequestInterface $request
  120. *
  121. * @return Fulfillment[]
  122. * @throws AdapterException
  123. */
  124. public function getFulfillments(ListRequestInterface $request)
  125. {
  126. $uri = sprintf('%s/fulfillments', $this->endpoint);
  127. $queryParams = $request->getRequestParams();
  128. $this->logger->log(LogLevel::DEBUG, sprintf('%s?%s', $uri, http_build_query($queryParams)));
  129. try {
  130. $this->auth->connect($this->accountId, $this->bearerToken);
  131. $headers = $this->requestHeaders->getHeaders();
  132. $rawResponse = $this->restClient->get($uri, $queryParams, $headers);
  133. $this->logger->log(LogLevel::DEBUG, $rawResponse);
  134. /** @var GetFulfillments $response */
  135. $response = $this->responseParser->parse($rawResponse, GetFulfillments::class);
  136. $fulfillments = $response->getData();
  137. } catch (RestClientErrorException $e) {
  138. $this->logger->log(LogLevel::ERROR, $e->getMessage());
  139. /** @var Errors $response */
  140. $response = $this->responseParser->parse($e->getMessage(), Errors::class);
  141. throw AdapterException::errorResponse($response, $e);
  142. } catch (\Exception $e) {
  143. $this->logger->critical($e->getMessage(), ['exception' => $e]);
  144. $fulfillments = [];
  145. }
  146. return $fulfillments;
  147. }
  148. /**
  149. * Create fulfillment at the platform.
  150. *
  151. * @param FulfillmentRequestInterface $request
  152. *
  153. * @return Fulfillment
  154. * @throws AdapterException
  155. */
  156. public function createFulfillment(FulfillmentRequestInterface $request)
  157. {
  158. $uri = sprintf('%s/fulfillments', $this->endpoint);
  159. $requestBody = $request->getRequestBody();
  160. $this->logger->log(LogLevel::DEBUG, sprintf("%s\n%s", $uri, $requestBody));
  161. try {
  162. $this->auth->connect($this->accountId, $this->bearerToken);
  163. $headers = $this->requestHeaders->getHeaders();
  164. $rawResponse = $this->restClient->post($uri, $requestBody, $headers);
  165. $this->logger->log(LogLevel::DEBUG, $rawResponse);
  166. /** @var GetFulfillment $response */
  167. $response = $this->responseParser->parse($rawResponse, GetFulfillment::class);
  168. $fulfillment = $response->getData();
  169. } catch (RestClientErrorException $e) {
  170. $this->logger->log(LogLevel::ERROR, $e->getMessage());
  171. /** @var Errors $response */
  172. $response = $this->responseParser->parse($e->getMessage(), Errors::class);
  173. throw AdapterException::errorResponse($response, $e);
  174. } catch (\Exception $e) {
  175. throw AdapterException::create($e);
  176. }
  177. return $fulfillment;
  178. }
  179. /**
  180. * Update fulfillment at the platform.
  181. *
  182. * @param FulfillmentRequestInterface $request
  183. *
  184. * @return Fulfillment
  185. * @throws AdapterException
  186. */
  187. public function updateFulfillment(FulfillmentRequestInterface $request)
  188. {
  189. $uri = sprintf('%s/fulfillments/%s', $this->endpoint, ...$request->getPathParams());
  190. $requestBody = $request->getRequestBody();
  191. $this->logger->log(LogLevel::DEBUG, sprintf("%s\n%s", $uri, $requestBody));
  192. try {
  193. $this->auth->connect($this->accountId, $this->bearerToken);
  194. $headers = $this->requestHeaders->getHeaders();
  195. $rawResponse = $this->restClient->patch($uri, $requestBody, $headers);
  196. $this->logger->log(LogLevel::DEBUG, $rawResponse);
  197. /** @var GetFulfillment $response */
  198. $response = $this->responseParser->parse($rawResponse, GetFulfillment::class);
  199. $fulfillment = $response->getData();
  200. } catch (RestClientErrorException $e) {
  201. $this->logger->log(LogLevel::ERROR, $e->getMessage());
  202. /** @var Errors $response */
  203. $response = $this->responseParser->parse($e->getMessage(), Errors::class);
  204. throw AdapterException::errorResponse($response, $e);
  205. } catch (\Exception $e) {
  206. throw AdapterException::create($e);
  207. }
  208. return $fulfillment;
  209. }
  210. }