AuthAdapter.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\AuthenticationApiInterface;
  9. use Temando\Shipping\Rest\Exception\AdapterException;
  10. use Temando\Shipping\Rest\Exception\RestClientErrorException;
  11. use Temando\Shipping\Rest\Request\AuthRequestInterface;
  12. use Temando\Shipping\Rest\Response\DataObject\Session;
  13. use Temando\Shipping\Rest\Response\Document\Errors;
  14. use Temando\Shipping\Rest\Response\Document\GetSession;
  15. use Temando\Shipping\Rest\SchemaMapper\ParserInterface;
  16. use Temando\Shipping\Webservice\Config\WsConfigInterface;
  17. /**
  18. * Temando REST API Authentication Adapter
  19. *
  20. * @package Temando\Shipping\Rest
  21. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  22. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * @link http://www.temando.com/
  25. */
  26. class AuthAdapter implements AuthenticationApiInterface
  27. {
  28. /**
  29. * @var string
  30. */
  31. private $endpoint;
  32. /**
  33. * @var string
  34. */
  35. private $apiVersion;
  36. /**
  37. * @var RestClientInterface
  38. */
  39. private $restClient;
  40. /**
  41. * @var ParserInterface
  42. */
  43. private $responseParser;
  44. /**
  45. * @var LoggerInterface
  46. */
  47. private $logger;
  48. /**
  49. * AuthAdapter constructor.
  50. * @param WsConfigInterface $config
  51. * @param RestClientInterface $restClient
  52. * @param ParserInterface $responseParser
  53. * @param LoggerInterface $logger
  54. */
  55. public function __construct(
  56. WsConfigInterface $config,
  57. RestClientInterface $restClient,
  58. ParserInterface $responseParser,
  59. LoggerInterface $logger
  60. ) {
  61. $this->endpoint = $config->getSessionEndpoint();
  62. $this->apiVersion = $config->getApiVersion();
  63. $this->restClient = $restClient;
  64. $this->responseParser = $responseParser;
  65. $this->logger = $logger;
  66. }
  67. /**
  68. * @param AuthRequestInterface $request
  69. * @return Session
  70. * @throws AdapterException
  71. */
  72. public function startSession(AuthRequestInterface $request)
  73. {
  74. $uri = sprintf('%s/sessions', $this->endpoint);
  75. $requestBody = $request->getRequestBody();
  76. $headers = [
  77. 'Cache-Control' => 'no-cache',
  78. 'Content-Type' => 'application/vnd.api+json',
  79. 'Accept' => 'application/vnd.api+json',
  80. 'Version' => $this->apiVersion,
  81. ];
  82. $this->logger->log(LogLevel::DEBUG, sprintf("%s\n%s", $uri, $requestBody));
  83. try {
  84. $rawResponse = $this->restClient->post($uri, $requestBody, $headers);
  85. $this->logger->log(LogLevel::DEBUG, $rawResponse);
  86. /** @var GetSession $response */
  87. $response = $this->responseParser->parse($rawResponse, GetSession::class);
  88. $session = $response->getData();
  89. } catch (RestClientErrorException $e) {
  90. $this->logger->log(LogLevel::ERROR, $e->getMessage());
  91. /** @var Errors $response */
  92. $response = $this->responseParser->parse($e->getMessage(), Errors::class);
  93. throw AdapterException::errorResponse($response, $e);
  94. } catch (\Exception $e) {
  95. throw AdapterException::create($e);
  96. }
  97. return $session;
  98. }
  99. /**
  100. * @return bool
  101. */
  102. public function endSession()
  103. {
  104. return false;
  105. }
  106. }