123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- /**
- * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
- */
- namespace Temando\Shipping\Rest;
- use Psr\Log\LoggerInterface;
- use Psr\Log\LogLevel;
- use Temando\Shipping\Rest\Adapter\OrderApiInterface;
- use Temando\Shipping\Rest\Exception\AdapterException;
- use Temando\Shipping\Rest\Exception\RestClientErrorException;
- use Temando\Shipping\Rest\Request\OrderRequest;
- use Temando\Shipping\Rest\Request\RequestHeadersInterface;
- use Temando\Shipping\Rest\Response\Document\Errors;
- use Temando\Shipping\Rest\Response\Document\SaveOrder;
- use Temando\Shipping\Rest\Response\Document\SaveOrderInterface;
- use Temando\Shipping\Rest\SchemaMapper\ParserInterface;
- use Temando\Shipping\Webservice\Config\WsConfigInterface;
- /**
- * Temando REST API Order Operations Adapter
- *
- * @package Temando\Shipping\Rest
- * @author Christoph Aßmann <christoph.assmann@netresearch.de>
- * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
- * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
- * @link https://www.temando.com/
- */
- class OrderAdapter implements OrderApiInterface
- {
- /**
- * @var string
- */
- private $endpoint;
- /**
- * @var string
- */
- private $accountId;
- /**
- * @var string
- */
- private $bearerToken;
- /**
- * @var RequestHeadersInterface
- */
- private $requestHeaders;
- /**
- * @var AuthenticationInterface
- */
- private $auth;
- /**
- * @var RestClientInterface
- */
- private $restClient;
- /**
- * @var ParserInterface
- */
- private $responseParser;
- /**
- * @var LoggerInterface
- */
- private $logger;
- /**
- * OrderAdapter constructor.
- * @param WsConfigInterface $config
- * @param RequestHeadersInterface $requestHeaders
- * @param AuthenticationInterface $auth
- * @param RestClientInterface $restClient
- * @param ParserInterface $responseParser
- * @param LoggerInterface $logger
- */
- public function __construct(
- WsConfigInterface $config,
- RequestHeadersInterface $requestHeaders,
- AuthenticationInterface $auth,
- RestClientInterface $restClient,
- ParserInterface $responseParser,
- LoggerInterface $logger
- ) {
- $this->endpoint = $config->getApiEndpoint();
- $this->accountId = $config->getAccountId();
- $this->bearerToken = $config->getBearerToken();
- $this->requestHeaders = $requestHeaders;
- $this->auth = $auth;
- $this->restClient = $restClient;
- $this->responseParser = $responseParser;
- $this->logger = $logger;
- }
- /**
- * Create order.
- *
- * @param OrderRequest $request
- * @return SaveOrderInterface
- * @throws AdapterException
- */
- public function createOrder(OrderRequest $request)
- {
- $requestParams = $request->getRequestParams();
- $uri = sprintf('%s/orders?%s', $this->endpoint, http_build_query($requestParams));
- $requestBody = $request->getRequestBody();
- $this->logger->log(LogLevel::DEBUG, sprintf("%s\n%s", $uri, $requestBody));
- try {
- $this->auth->connect($this->accountId, $this->bearerToken);
- $headers = $this->requestHeaders->getHeaders();
- $rawResponse = $this->restClient->post($uri, $requestBody, $headers);
- $this->logger->log(LogLevel::DEBUG, $rawResponse);
- /** @var SaveOrder $response */
- $response = $this->responseParser->parse($rawResponse, SaveOrder::class);
- } catch (RestClientErrorException $e) {
- $this->logger->log(LogLevel::ERROR, $e->getMessage());
- /** @var Errors $response */
- $response = $this->responseParser->parse($e->getMessage(), Errors::class);
- throw AdapterException::errorResponse($response, $e);
- } catch (\Exception $e) {
- throw AdapterException::create($e);
- }
- return $response;
- }
- /**
- * Update order.
- *
- * @param OrderRequest $request
- * @return SaveOrderInterface
- * @throws AdapterException
- */
- public function updateOrder(OrderRequest $request)
- {
- $uri = sprintf('%s/orders/%s', $this->endpoint, ...$request->getPathParams());
- $requestBody = $request->getRequestBody();
- $this->logger->log(LogLevel::DEBUG, sprintf("%s\n%s", $uri, $requestBody));
- try {
- $this->auth->connect($this->accountId, $this->bearerToken);
- $headers = $this->requestHeaders->getHeaders();
- $rawResponse = $this->restClient->put($uri, $requestBody, $headers);
- $this->logger->log(LogLevel::DEBUG, $rawResponse);
- /** @var SaveOrder $response */
- $response = $this->responseParser->parse($rawResponse, SaveOrder::class);
- } catch (RestClientErrorException $e) {
- $this->logger->log(LogLevel::ERROR, $e->getMessage());
- /** @var Errors $response */
- $response = $this->responseParser->parse($e->getMessage(), Errors::class);
- throw AdapterException::errorResponse($response, $e);
- } catch (\Exception $e) {
- throw AdapterException::create($e);
- }
- return $response;
- }
- }
|