* @author Sebastian Ertner * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @link http://www.temando.com/ */ class AuthAdapter implements AuthenticationApiInterface { /** * @var string */ private $endpoint; /** * @var string */ private $apiVersion; /** * @var RestClientInterface */ private $restClient; /** * @var ParserInterface */ private $responseParser; /** * @var LoggerInterface */ private $logger; /** * AuthAdapter constructor. * @param WsConfigInterface $config * @param RestClientInterface $restClient * @param ParserInterface $responseParser * @param LoggerInterface $logger */ public function __construct( WsConfigInterface $config, RestClientInterface $restClient, ParserInterface $responseParser, LoggerInterface $logger ) { $this->endpoint = $config->getSessionEndpoint(); $this->apiVersion = $config->getApiVersion(); $this->restClient = $restClient; $this->responseParser = $responseParser; $this->logger = $logger; } /** * @param AuthRequestInterface $request * @return Session * @throws AdapterException */ public function startSession(AuthRequestInterface $request) { $uri = sprintf('%s/sessions', $this->endpoint); $requestBody = $request->getRequestBody(); $headers = [ 'Cache-Control' => 'no-cache', 'Content-Type' => 'application/vnd.api+json', 'Accept' => 'application/vnd.api+json', 'Version' => $this->apiVersion, ]; $this->logger->log(LogLevel::DEBUG, sprintf("%s\n%s", $uri, $requestBody)); try { $rawResponse = $this->restClient->post($uri, $requestBody, $headers); $this->logger->log(LogLevel::DEBUG, $rawResponse); /** @var GetSession $response */ $response = $this->responseParser->parse($rawResponse, GetSession::class); $session = $response->getData(); } 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 $session; } /** * @return bool */ public function endSession() { return false; } }