Skip to content

Commit

Permalink
First implementation of fix for #2 - Return a Result object
Browse files Browse the repository at this point in the history
  • Loading branch information
DerMika committed May 17, 2016
1 parent 64faa9e commit 9b34503
Show file tree
Hide file tree
Showing 24 changed files with 1,793 additions and 181 deletions.
237 changes: 182 additions & 55 deletions src/Amadeus/Client.php

Large diffs are not rendered by default.

122 changes: 103 additions & 19 deletions src/Amadeus/Client/ResponseHandler/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
namespace Amadeus\Client\ResponseHandler;

use Amadeus\Client\Exception;
use Amadeus\Client\Util\MsgBodyExtractor;
use Amadeus\Client\Result;
use Amadeus\Client\Session\Handler\SendResult;

/**
* Base Response Handler
Expand All @@ -43,55 +44,110 @@ class Base implements ResponseHandlerInterface
/**
* Analyze the response from the server and throw an exception when an error has been detected.
*
* @param string $response The last response received by the client
* @param SendResult $sendResult The Send Result from the Session Handler
* @param string $messageName The message that was called
*
* @throws Exception
* @throws \RuntimeException
* @return bool
* @return Result
*/
public function analyzeResponse($response, $messageName)
public function analyzeResponse($sendResult, $messageName)
{
$methodName = 'analyze' . str_replace('_', '', ucfirst($messageName)).'Response';

if (method_exists($this, $methodName)) {
return $this->$methodName(
MsgBodyExtractor::extract($response)
$sendResult
);
} else {
throw new \RuntimeException('Response checker for ' . $messageName . ' is not implemented');
return new Result($sendResult, Result::STATUS_UNKNOWN);
}
}

/**
* @param string $response PNR_AddMultiElements XML string
* @return bool
* @throws Exception
* @param SendResult $response PNR_AddMultiElements result
* @return Result
*/
protected function analyzePnrAddMultiElementsResponse($response)
{
$analyzeResponse = new Result($response);

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

//General Errors:
$queryAllErrorCodes = "//m:generalErrorInfo//m:errorOrWarningCodeDetails/m:errorDetails/m:errorCode";
$queryAllErrorMsg = "//m:generalErrorInfo/m:errorWarningDescription/m:freeText";

$errorCodeNodeList = $domXpath->query($queryAllErrorCodes);

if ($errorCodeNodeList->length > 0) {
$analyzeResponse->status = Result::STATUS_ERROR;

$code = $errorCodeNodeList->item(0)->nodeValue;
$errorTextNodeList = $domXpath->query($queryAllErrorMsg);
$message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);

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

//Segment errors:
$querySegmentErrorCodes = "//m:originDestinationDetails//m:errorInfo/m:errorOrWarningCodeDetails/m:errorDetails/m:errorCode";
$querySegmentErrorMsg = "//m:originDestinationDetails//m:errorInfo/m:errorWarningDescription/m:freeText";

$errorCodeNodeList = $domXpath->query($querySegmentErrorCodes);

if ($errorCodeNodeList->length > 0) {
$analyzeResponse->status = Result::STATUS_ERROR;

$code = $errorCodeNodeList->item(0)->nodeValue;
$errorTextNodeList = $domXpath->query($querySegmentErrorMsg);
$message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);

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

//Element errors:
$queryElementErrorCodes = "//m:dataElementsIndiv/m:elementErrorInformation/m:errorOrWarningCodeDetails/m:errorDetails/m:errorCode";
$queryElementErrorMsg = "//m:dataElementsIndiv//m:elementErrorInformation/m:errorWarningDescription/m:freeText";

$errorCodeNodeList = $domXpath->query($queryElementErrorCodes);

if ($errorCodeNodeList->length > 0) {
$analyzeResponse->status = Result::STATUS_ERROR;

$code = $errorCodeNodeList->item(0)->nodeValue;

$errorTextNodeList = $domXpath->query($queryElementErrorMsg);
$message = $this->makeMessageFromMessagesNodeList($errorTextNodeList);

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


return $analyzeResponse;
}

/**
* @param string $response Queue_List XML string
* @return bool
* @param SendResult $response Queue_List result
* @return Result
* @throws Exception
*/
protected function analyzeQueueListResponse($response)
{
$analysisResponse = true;
$analysisResponse = new Result($response);

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

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

if (!is_null($errorCodeNode)) {
$analysisResponse->status = Result::STATUS_WARN;

$errorCode = $errorCodeNode->nodeValue;
$errorMessage = $this->getErrorTextFromQueueErrorCode($errorCode);

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

return $analysisResponse;
Expand Down Expand Up @@ -128,26 +184,54 @@ protected function getErrorTextFromQueueErrorCode($errorCode)
$errorMessage = (array_key_exists($errorCode, $recognizedErrors)) ? $recognizedErrors[$errorCode] : '';

if ($errorMessage === '') {
$errorMessage = " QUEUE ERROR '" . $errorCode . "' (Error message unavailable)";
$errorMessage = "QUEUE ERROR '" . $errorCode . "' (Error message unavailable)";
}

return $errorMessage;
}


/**
* Make a Xpath-queryable object for an XML string
*
* @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');
$domDoc->loadXML($response);
$uri = $domDoc->documentElement->lookupNamespaceUri(null);
$domXpath = null;
$loadResult = $domDoc->loadXML($response);

if ($loadResult === true) {
$uri = $domDoc->documentElement->lookupNamespaceUri(null);

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

return $domXpath;
}

/**
* Convert a DomNodeList of nodes containing a (potentially partial) error message into a string.
*
* @param \DOMNodeList $errorTextNodeList
* @return string|null
*/
protected function makeMessageFromMessagesNodeList($errorTextNodeList)
{
return implode(
' - ',
array_map(
function($item) {
return $item->nodeValue;
},
iterator_to_array($errorTextNodeList)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
namespace Amadeus\Client\ResponseHandler;

use Amadeus\Client\Exception;
use Amadeus\Client\Result;
use Amadeus\Client\Session\Handler\SendResult;

/**
* ResponseHandlerInterface
Expand All @@ -35,12 +37,12 @@ interface ResponseHandlerInterface
/**
* Analyze the response from the server and throw an exception when an error has been detected.
*
* @param string $response The last response received by the client
* @param SendResult $sendResult The Send Result from the Session Handler
* @param string $messageName The message that was called
*
* @throws Exception When an error is detected
* @throws \RuntimeException When there is a problem calling the response handler
* @return mixed Will return true if no problems occurred.
* @return Result
*/
public function analyzeResponse($response, $messageName);
public function analyzeResponse($sendResult, $messageName);
}
102 changes: 102 additions & 0 deletions src/Amadeus/Client/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* amadeus-ws-client
*
* Copyright 2015 Amadeus Benelux NV
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package Amadeus
* @license https://opensource.org/licenses/Apache-2.0 Apache 2.0
*/

namespace Amadeus\Client;

use Amadeus\Client\Result\NotOk;
use Amadeus\Client\Session\Handler\SendResult;

/**
* Result object to encapsulate the Web Services response to library users.
*
* @package Amadeus\Client
* @author Dieter Devlieghere <dieter.devlieghere@benelux.amadeus.com>
*/
class Result
{
/**
* Status indicator for a success situation
*/
const STATUS_OK = 'OK';
/**
* Status indicator for a warning situation.
*/
const STATUS_WARN = 'WARN';
/**
* Status indicator for an error response.
*/
const STATUS_ERROR = 'ERR';
/**
* Status indicator for a response which could not be checked for warnings/errors.
*/
const STATUS_UNKNOWN = 'UNKNOWN';

/**
* Status of the result
*
* see self::STATUS_*
*
* @var string
*/
public $status;

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

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

/**
* The actual result received after performing the web service call.
*
* @var \stdClass
*/
public $response;

/**
* The raw contents of the Soap Envelope received after performing the web service call.
*
* @var string
*/
public $responseXml;

/**
* Result constructor.
*
* @param SendResult $sendResult
* @param string $status
*/
public function __construct($sendResult, $status = self::STATUS_OK)
{
$this->response = $sendResult->responseObject;
$this->responseXml = $sendResult->responseXml;
$this->status = $status;
}
}
61 changes: 61 additions & 0 deletions src/Amadeus/Client/Result/NotOk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* amadeus-ws-client
*
* Copyright 2015 Amadeus Benelux NV
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package Amadeus
* @license https://opensource.org/licenses/Apache-2.0 Apache 2.0
*/

namespace Amadeus\Client\Result;

/**
* NotOk
*
* @package Amadeus\Client\Result
* @author Dieter Devlieghere <dieter.devlieghere@benelux.amadeus.com>
*/
class NotOk
{
/**
* @var mixed
*/
public $code;

/**
* @var string
*/
public $text;

/**
* @var string
*/
public $level;

/**
* NotOk constructor.
*
* @param string|int|null $code
* @param string|null $text
* @param string|null $level
*/
public function __construct($code = null, $text = null, $level = null)
{
$this->code = $code;
$this->text = $text;
$this->level = $level;
}
}
Loading

0 comments on commit 9b34503

Please sign in to comment.