Skip to content

Commit

Permalink
Implementing more response handler message checks #2
Browse files Browse the repository at this point in the history
  • Loading branch information
DerMika committed May 30, 2016
1 parent 5ae9b55 commit 1c542fb
Show file tree
Hide file tree
Showing 7 changed files with 520 additions and 59 deletions.
188 changes: 160 additions & 28 deletions src/Amadeus/Client/ResponseHandler/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,89 @@ public function analyzeResponse($sendResult, $messageName)
*/
protected function analyzeSecurityAuthenticateResponse($response)
{
return new Result($response); //TODO
return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
}

/**
* Analysing a PNR_Reply
* Analysing a Security_Authenticate
*
* @param SendResult $response Security_Authenticate result
* @return Result
*/
protected function analyzeSecuritySignOutResponse($response)
{
return $this->analyzeSimpleResponseErrorCodeAndMessage($response);
}

/**
* @param SendResult $response
* @return Result
*/
protected function analyzeAirFlightInfoResponse($response)
{
$analyzeResponse = new Result($response);

$code = null;
$message = null;

$domXpath = $this->makeDomXpath($response->responseXml);

$categoryNodes = $domXpath->query('//m:responseError/m:errorInfo/m:errorDetails/m:errorCategory');
if ($categoryNodes->length > 0) {
$analyzeResponse->status = $this->makeStatusFromErrorQualifier($categoryNodes->item(0)->nodeValue);
}

$codeNodes = $domXpath->query('//m:responseError/m:errorInfo/m:errorDetails/m:errorCode');
if ($codeNodes->length > 0) {
$code = $codeNodes->item(0)->nodeValue;
}

$messageNodes = $domXpath->query('//m:responseError/m:interactiveFreeText/m:freeText');
if ($messageNodes->length > 0) {
$message = $this->makeMessageFromMessagesNodeList($messageNodes);
}

$analyzeResponse->messages[] = new Result\NotOk($code, $message);

return $analyzeResponse;
}

/**
* Analysing a PNR_Retrieve response
*
* @param SendResult $response PNR_Retrieve result
* @return Result
*/
protected function analyzePnrRetrieveResponse($response)
{
return $this->analyzePnrReply($response);
}

/**
* @param SendResult $response PNR_AddMultiElements result
* @return Result
*/
protected function analyzePnrAddMultiElementsResponse($response)
{
return $this->analyzePnrReply($response);
}

/**
* @param SendResult $response PNR_Cancel result
* @return Result
*/
protected function analyzePnrCancelResponse($response)
{
return $this->analyzePnrReply($response);
}

/**
* Analysing a PNR_Reply
*
* @param SendResult $response PNR_Retrieve result
* @return Result
*/
protected function analyzePnrReply($response)
{
$analyzeResponse = new Result($response);

Expand All @@ -100,7 +173,7 @@ protected function analyzePnrRetrieveResponse($response)
$errorTextNodeList = $domXpath->query($queryAllErrorMsg);
$message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);

$analyzeResponse->errors[] = new Result\NotOk($code, trim($message), 'general');
$analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'general');
}

//Segment errors:
Expand All @@ -116,7 +189,7 @@ protected function analyzePnrRetrieveResponse($response)
$errorTextNodeList = $domXpath->query($querySegmentErrorMsg);
$message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);

$analyzeResponse->errors[] = new Result\NotOk($code, trim($message), 'segment');
$analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'segment');
}

//Element errors:
Expand All @@ -133,22 +206,13 @@ protected function analyzePnrRetrieveResponse($response)
$errorTextNodeList = $domXpath->query($queryElementErrorMsg);
$message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);

$analyzeResponse->errors[] = new Result\NotOk($code, trim($message), 'element');
$analyzeResponse->messages[] = new Result\NotOk($code, trim($message), 'element');
}


return $analyzeResponse;
}

/**
* @param SendResult $response PNR_AddMultiElements result
* @return Result
*/
protected function analyzePnrAddMultiElementsResponse($response)
{
return $this->analyzePnrRetrieveResponse($response);
}

/**
* @param SendResult $response Queue_List result
* @return Result
Expand All @@ -158,8 +222,7 @@ protected function analyzeQueueListResponse($response)
{
$analysisResponse = new Result($response);

$domDoc = new \DOMDocument('1.0', 'UTF-8');
$domDoc->loadXML($response->responseXml);
$domDoc = $this->loadDomDocument($response->responseXml);

$errorCodeNode = $domDoc->getElementsByTagName("errorCode")->item(0);

Expand All @@ -169,12 +232,39 @@ protected function analyzeQueueListResponse($response)
$errorCode = $errorCodeNode->nodeValue;
$errorMessage = $this->getErrorTextFromQueueErrorCode($errorCode);

$analysisResponse->warnings[] = new Result\NotOk($errorCode, $errorMessage);
$analysisResponse->messages[] = new Result\NotOk($errorCode, $errorMessage);
}

return $analysisResponse;
}

/**
* @param SendResult $response WebService message Send Result
* @return Result
* @throws Exception
*/
protected function analyzeSimpleResponseErrorCodeAndMessage($response)
{
$analyzeResponse = new Result($response);

$domDoc = $this->loadDomDocument($response->responseXml);

$errorCodeNode = $domDoc->getElementsByTagName("errorCode")->item(0);

if (!is_null($errorCodeNode)) {
$analyzeResponse->status = Result::STATUS_WARN;
$errorCode = $errorCodeNode->nodeValue;
$errorTextNodeList = $domDoc->getElementsByTagName("freeText");

$analyzeResponse->messages[] = new Result\NotOk(
$errorCode,
$this->makeMessageFromMessagesNodeList($errorTextNodeList)
);
}

return $analyzeResponse;
}

/**
* Returns the errortext from a Queue_*Reply errorcode
*
Expand Down Expand Up @@ -212,28 +302,70 @@ protected function getErrorTextFromQueueErrorCode($errorCode)
return $errorMessage;
}

/**
* @param string $response
* @return \DOMDocument
* @throws Exception when there's a problem loading the message
*/
protected function loadDomDocument($response)
{
$domDoc = new \DOMDocument('1.0', 'UTF-8');

$loadResult = $domDoc->loadXML($response);
if ($loadResult === false) {
throw new Exception('Could not load response message into DOMDocument');
}

return $domDoc;
}

/**
* @param $qualifier
* @return string Result::STATUS_*
*/
protected function makeStatusFromErrorQualifier($qualifier)
{
$status = null;

switch ($qualifier) {
case 'INF':
$status = Result::STATUS_INFO;
break;
case 'WEC':
case 'WZZ': //Mutually defined warning
$status = Result::STATUS_WARN;
break;
case 'EC':
$status = Result::STATUS_ERROR;
break;
case 'ZZZ': //Mutually defined
default:
$status = Result::STATUS_UNKNOWN;
break;
}

return $status;
}


/**
* Make a Xpath-queryable object for an XML string
*
* registers TNS namespace with prefix self::XMLNS_PREFIX
*
* @param string $response
* @return \DOMXPath
* @throws Exception when there's a problem loading the message
*/
protected function makeDomXpath($response)
{
$domDoc = new \DOMDocument('1.0', 'UTF-8');
$domXpath = null;
$loadResult = $domDoc->loadXML($response);

if ($loadResult === true) {
$uri = $domDoc->documentElement->lookupNamespaceUri(null);
$domDoc = $this->loadDomDocument($response);
$domXpath = new \DOMXPath($domDoc);

$domXpath = new \DOMXPath($domDoc);
$domXpath->registerNamespace(self::XMLNS_PREFIX, $uri);
} else {
throw new Exception('Could not load response message into DOMDocument');
}
$domXpath->registerNamespace(
self::XMLNS_PREFIX,
$domDoc->documentElement->lookupNamespaceUri(null)
);

return $domXpath;
}
Expand Down
15 changes: 6 additions & 9 deletions src/Amadeus/Client/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class Result
* Status indicator for a success situation
*/
const STATUS_OK = 'OK';
/**
* Status indicator for an informational message situation.
*/
const STATUS_INFO = 'INFO';
/**
* Status indicator for a warning situation.
*/
Expand All @@ -60,18 +64,11 @@ class Result
public $status;

/**
* Array of warnings found
*
* @var NotOk[]
*/
public $warnings = [];

/**
* Array of errors found
* Array of errors or warnings found
*
* @var NotOk[]
*/
public $errors = [];
public $messages = [];

/**
* The actual result received after performing the web service call.
Expand Down
5 changes: 5 additions & 0 deletions src/Amadeus/Client/Session/Handler/SendResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@
class SendResult
{
/**
* The response as an XML string
*
* @var string
*/
public $responseXml;

/**
* The response as returned by PHP's \SoapClient
*
* @var \stdClass|array
*/
public $responseObject;
Expand Down
Loading

0 comments on commit 1c542fb

Please sign in to comment.