Skip to content

Commit

Permalink
Support for stateful session calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
DerMika committed Jan 29, 2016
1 parent 4f13589 commit 20f4b7c
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 12 deletions.
20 changes: 20 additions & 0 deletions src/Amadeus/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ public function getStateful()
return $this->sessionHandler->getStateful();
}

/**
* Get the last raw XML message that was sent out
*
* @return string|null
*/
public function getLastRequest()
{
$this->sessionHandler->getLastRequest();
}

/**
* Get the last raw XML message that was received
*
* @return string|null
*/
public function getLastResponse()
{
$this->sessionHandler->getLastRequest();
}

/**
* Construct Amadeus Web Services client
*
Expand Down
25 changes: 25 additions & 0 deletions src/Amadeus/Client/Session/Handler/HandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,37 @@ public function sendMessage($messageName, BaseWsMessage $messageBody, $messageOp
public function getOriginatorOffice();

/**
* Set the Stateful mode off or on.
*
* @param bool $stateful
*/
public function setStateful($stateful);

/**
* Get the session parameters of the active session
*
* @return array|null
*/
public function getSessionData();

/**
* Get the current stateful mode (true is stateful, false is stateless)
*
* @return bool
*/
public function getStateful();

/**
* Get the last raw XML message that was sent out
*
* @return string|null
*/
public function getLastRequest();

/**
* Get the last raw XML message that was received
*
* @return string|null
*/
public function getLastResponse();
}
80 changes: 68 additions & 12 deletions src/Amadeus/Client/Session/Handler/SoapHeader4.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ class SoapHeader4 extends Base
*/
protected $isStateful = true;
/**
* The context
* The context of the currently active session
*
* @todo implement this feature - currently the application using the client must know the context itself.
* @var mixed
*/
protected $context;
Expand All @@ -86,7 +87,6 @@ class SoapHeader4 extends Base
'sequenceNumber' => null,
'securityToken' => null
];

/**
* SoapClient options used during initialisation
*
Expand Down Expand Up @@ -154,6 +154,36 @@ public function getStateful()
return $this->isStateful;
}

/**
* Get the session parameters of the active session
*
* @return array|null
*/
public function getSessionData()
{
return $this->sessionData;
}

/**
* Get the last raw XML message that was sent out
*
* @return string|null
*/
public function getLastRequest()
{
return $this->getSoapClient()->__getLastRequest();
}

/**
* Get the last raw XML message that was received
*
* @return string|null
*/
public function getLastResponse()
{
return $this->getSoapClient()->__getLastResponse();
}


/**
* @param string $messageName Method Operation name as defined in the WSDL.
Expand All @@ -162,6 +192,7 @@ public function getStateful()
* @return mixed
* @throws \InvalidArgumentException
* @throws Client\Exception
* @throws \SoapFault
*/
public function sendMessage($messageName, BaseWsMessage $messageBody, $messageOptions = [])
{
Expand Down Expand Up @@ -199,7 +230,7 @@ public function sendMessage($messageName, BaseWsMessage $messageBody, $messageOp
}

if ($messageOptions['asString'] === true) {
$result = $this->getSoapClient()->__getLastResponse();
$result = $this->getLastResponse();
}

return $result;
Expand All @@ -211,7 +242,6 @@ public function sendMessage($messageName, BaseWsMessage $messageBody, $messageOp
* If authenticated, increment sequence number for next message and set session info to soapheader
* If not, set auth info to soapheader
*
* @todo decide if you want to end the session
* @uses $this->isAuthenticated
* @uses $this->sessionData
* @param string $messageName
Expand Down Expand Up @@ -245,32 +275,58 @@ protected function handlePostMessage($messageName, $messageOptions, $result)

if ($messageName === "Security_Authenticate") {
//You really don't need the Security_Authenticate call anymore with SoapHeader 4!
//TODO
throw new \RuntimeException('NOT YET IMPLEMENTED: Extract session data from Security_AuthenticateReply');
}

//CHECK FOR SESSION DATA:
if ($this->getStateful() === true) {
//We need to extract session info
$this->sessionData = $this->getSessionDataFromHeader(
$this->getSoapClient()->__getLastResponseHeaders()
$this->getLastResponse()
);
$this->isAuthenticated = !empty($this->sessionData);

$this->isAuthenticated = (!empty($this->sessionData['sessionId']) &&
!empty($this->sessionData['sequenceNumber']) &&
!empty($this->sessionData['securityToken']));

} else {
$this->isAuthenticated = false;
}

//TODO: check for errors in response?
}

/**
* @param $responseHeaders
* @param string $responseMsg the full response XML received.
* @return array
*/
protected function getSessionDataFromHeader($responseHeaders)
protected function getSessionDataFromHeader($responseMsg)
{
$this->log(LogLevel::WARNING, __METHOD__ . "() TODO: IMPLEMENT THIS METHOD");
$this->log(LogLevel::INFO, var_export($responseHeaders, true));
$newSessionData = [
'sessionId' => null,
'sequenceNumber' => null,
'securityToken' => null
];

$responseDomDoc = new \DOMDocument('1.0', 'UTF-8');
$responseDomDoc->loadXML($responseMsg);
$responseDomXpath = new \DOMXPath($responseDomDoc);
$responseDomXpath->registerNamespace('awsse', 'http://xml.amadeus.com/2010/06/Session_v3');

$queryTransactionStatusCode = "string(//awsse:Session/@TransactionStatusCode/text())";

$transactionStatusCode = $responseDomXpath->evaluate($queryTransactionStatusCode);

if (mb_strtolower($transactionStatusCode) !== "end") {
$querySessionId = "string(//awsse:Session/awsse:SessionId/text())";
$querySequenceNumber = "string(//awsse:Session/awsse:SequenceNumber/text())";
$querySecurityToken = "string(//awsse:Session/awsse:SecurityToken/text())";

$newSessionData['sessionId'] = $responseDomXpath->evaluate($querySessionId);
$newSessionData['sequenceNumber'] = $responseDomXpath->evaluate($querySequenceNumber);
$newSessionData['securityToken'] = $responseDomXpath->evaluate($querySecurityToken);
}

return $newSessionData;
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/Amadeus/Client/Session/Handler/SoapHeader4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,26 @@ public function testCanReadSessionEndFromResponse()
$sessionHandler = new SoapHeader4($sessionHandlerParams);

$method = self::getMethod($sessionHandler, 'generateSecurityHeaderRawXml');
}

public function testCanReadSessionDataFromResponse()
{
$sessionHandlerParams = $this->makeSessionHandlerParams();
$sessionHandler = new SoapHeader4($sessionHandlerParams);
$method = self::getMethod($sessionHandler, 'getSessionDataFromHeader');

$expected = [
'sessionId' => '01ZWHV5EMT',
'sequenceNumber' => 1,
'securityToken' => '3WY60GB9B0FX2SLIR756QZ4G2'
];

$xml = $this->getTestFile("dummyPnrResponse.txt");

$actual = $method->invoke($sessionHandler, $xml);

$this->assertInternalType('array', $actual);
$this->assertEquals($expected, $actual);
}


Expand Down Expand Up @@ -276,4 +295,14 @@ protected function toDomElement($xmlString)

return $doc->firstChild;
}

/**
* @param $fileName
* @return string
*/
protected function getTestFile($fileName)
{
$fullPath = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR."testfiles".DIRECTORY_SEPARATOR.$fileName);
return file_get_contents($fullPath);
}
}
Loading

0 comments on commit 20f4b7c

Please sign in to comment.