ShipmentAdapter.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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\BatchApiInterface;
  9. use Temando\Shipping\Rest\Adapter\ShipmentApiInterface;
  10. use Temando\Shipping\Rest\Exception\AdapterException;
  11. use Temando\Shipping\Rest\Exception\RestClientErrorException;
  12. use Temando\Shipping\Rest\Request\ItemRequestInterface;
  13. use Temando\Shipping\Rest\Request\RequestHeadersInterface;
  14. use Temando\Shipping\Rest\Response\DataObject\Batch;
  15. use Temando\Shipping\Rest\Response\DataObject\Shipment;
  16. use Temando\Shipping\Rest\Response\Document\Errors;
  17. use Temando\Shipping\Rest\Response\Document\GetBatch;
  18. use Temando\Shipping\Rest\Response\Document\GetShipment;
  19. use Temando\Shipping\Rest\SchemaMapper\ParserInterface;
  20. use Temando\Shipping\Webservice\Config\WsConfigInterface;
  21. /**
  22. * Temando REST API Shipment 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 ShipmentAdapter implements BatchApiInterface, ShipmentApiInterface
  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. * ShipmentAdapter 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. * Read a batch from the platform.
  92. *
  93. * @param ItemRequestInterface $request
  94. * @return Batch
  95. * @throws AdapterException
  96. */
  97. public function getBatch(ItemRequestInterface $request)
  98. {
  99. $uri = sprintf('%s/shipments/batches/%s', $this->endpoint, ...$request->getPathParams());
  100. $this->logger->log(LogLevel::DEBUG, $uri);
  101. try {
  102. $this->auth->connect($this->accountId, $this->bearerToken);
  103. $headers = $this->requestHeaders->getHeaders();
  104. $rawResponse = $this->restClient->get($uri, [], $headers);
  105. $this->logger->log(LogLevel::DEBUG, $rawResponse);
  106. /** @var GetBatch $response */
  107. $response = $this->responseParser->parse($rawResponse, GetBatch::class);
  108. $batch = $response->getData();
  109. $batch->setShipments($response->getIncluded());
  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 $batch;
  119. }
  120. /**
  121. * Read one shipment from the platform.
  122. *
  123. * @param ItemRequestInterface $request
  124. * @return Shipment
  125. * @throws AdapterException
  126. */
  127. public function getShipment(ItemRequestInterface $request)
  128. {
  129. $uri = sprintf('%s/shipments/%s', $this->endpoint, ...$request->getPathParams());
  130. $this->logger->log(LogLevel::DEBUG, $uri);
  131. try {
  132. $this->auth->connect($this->accountId, $this->bearerToken);
  133. $headers = $this->requestHeaders->getHeaders();
  134. $rawResponse = $this->restClient->get($uri, [], $headers);
  135. $this->logger->log(LogLevel::DEBUG, $rawResponse);
  136. /** @var GetShipment $response */
  137. $response = $this->responseParser->parse($rawResponse, GetShipment::class);
  138. $shipment = $response->getData();
  139. } catch (RestClientErrorException $e) {
  140. $this->logger->log(LogLevel::ERROR, $e->getMessage());
  141. /** @var Errors $response */
  142. $response = $this->responseParser->parse($e->getMessage(), Errors::class);
  143. throw AdapterException::errorResponse($response, $e);
  144. } catch (\Exception $e) {
  145. throw AdapterException::create($e);
  146. }
  147. return $shipment;
  148. }
  149. /**
  150. * Cancel shipment at the platform.
  151. *
  152. * @param ItemRequestInterface $request
  153. * @return Shipment
  154. * @throws AdapterException
  155. */
  156. public function cancelShipment(ItemRequestInterface $request)
  157. {
  158. $uri = sprintf('%s/shipments/%s/cancel', $this->endpoint, ...$request->getPathParams());
  159. $this->logger->log(LogLevel::DEBUG, $uri);
  160. try {
  161. $this->auth->connect($this->accountId, $this->bearerToken);
  162. $headers = $this->requestHeaders->getHeaders();
  163. $rawResponse = $this->restClient->get($uri, [], $headers);
  164. $this->logger->log(LogLevel::DEBUG, $rawResponse);
  165. /** @var GetShipment $response */
  166. $response = $this->responseParser->parse($rawResponse, GetShipment::class);
  167. $shipment = $response->getData();
  168. } catch (RestClientErrorException $e) {
  169. $this->logger->log(LogLevel::ERROR, $e->getMessage());
  170. /** @var Errors $response */
  171. $response = $this->responseParser->parse($e->getMessage(), Errors::class);
  172. throw AdapterException::errorResponse($response, $e);
  173. } catch (\Exception $e) {
  174. throw AdapterException::create($e);
  175. }
  176. return $shipment;
  177. }
  178. }