Skip to content

Commit

Permalink
Extractor refactoring (#303)
Browse files Browse the repository at this point in the history
Fixes #302
  • Loading branch information
therealartz authored and DerMika committed Mar 21, 2019
1 parent f4dfed3 commit a8b79e0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 28 deletions.
8 changes: 7 additions & 1 deletion src/Amadeus/Client/Session/Handler/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ abstract class Base implements HandlerInterface, LoggerAwareInterface
*/
protected $messagesAndVersions = [];

/**
* @var Client\Util\MsgBodyExtractor
*/
protected $extractor;

/**
* Get the session parameters of the active session
*
Expand Down Expand Up @@ -172,6 +177,7 @@ public function __construct(SessionHandlerParams $params)
$this->setStateful($params->stateful);
$this->setTransactionFlowLink($params->enableTransactionFlowLink);
$this->setConsumerId($params->consumerId);
$this->extractor = new Client\Util\MsgBodyExtractor();
}


Expand Down Expand Up @@ -219,7 +225,7 @@ public function sendMessage($messageName, Client\Struct\BaseWsMessage $messageBo
throw new Client\Exception($ex->getMessage(), $ex->getCode(), $ex);
}

$result->responseXml = Client\Util\MsgBodyExtractor::extract($this->getLastResponse($messageName));
$result->responseXml = $this->extractor->extract($this->getLastResponse($messageName));

return $result;
}
Expand Down
44 changes: 21 additions & 23 deletions src/Amadeus/Client/Util/MsgBodyExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,39 @@
*/
class MsgBodyExtractor
{
/**
* Regular expression for extracting the Soap Envelope Body's content.
*
* @var string
*/
const REGEXP_SOAP_ENVELOPE_CONTENTS = "|\\<SOAP-ENV:Body\\>(.*?)\\<\\/SOAP-ENV:Body\\>|s";

/**
* Regular expression for extracting the Soap Envelope Body's content - legacy format for Soap Header v2 and older
*
* @var string
*/
const REGEXP_SOAP_ENVELOPE_CONTENTS_LEGACY = "|\\<soap:Body\\>(.*?)\\<\\/soap:Body\\>|s";

/**
* Extracts the message content from the soap envelope (i.e. everything under the soap body)
*
* @param string $soapResponse
* @return string|null
*/
public static function extract($soapResponse)
public function extract($soapResponse)
{
$messageBody = null;
$matches = [];

if (preg_match(self::REGEXP_SOAP_ENVELOPE_CONTENTS, $soapResponse, $matches) === 1) {
$messageBody = $matches[1];
}
$messageBody = $this->getStringBetween($soapResponse, '<SOAP-ENV:Body>', '</SOAP-ENV:Body>');

if (empty($messageBody)) {
if (preg_match(self::REGEXP_SOAP_ENVELOPE_CONTENTS_LEGACY, $soapResponse, $matches) === 1) {
$messageBody = $matches[1];
}
if (empty($messageBody) || false === $messageBody) {
$messageBody = $this->getStringBetween($soapResponse, '<soap:Body>', '</soap:Body>');
}

return $messageBody;
}

/**
* Get substring between two strings
*
* @param $string
* @param $start
* @param $end
* @return bool|string
*/
private function getStringBetween($string, $start, $end)
{
$startPos = strpos($string, $start) + strlen($start);

$endPos = strlen($string) - strpos($string, $end);

return substr($string, $startPos, -$endPos);
}
}
3 changes: 2 additions & 1 deletion tests/Amadeus/Client/Session/Handler/SoapHeader2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,14 @@ public function testCanSendAuthCallAndStartSession()
$dummyRequest = $this->getTestFile('soapheader2' . DIRECTORY_SEPARATOR . 'dummySecurityAuth.txt');
$dummyReply = $this->getTestFile('soapheader2' . DIRECTORY_SEPARATOR . 'dummySecurityAuthReply.txt');

$extractor = new Client\Util\MsgBodyExtractor();
$wsResponse = new \stdClass();
$wsResponse->processStatus = new \stdClass();
$wsResponse->processStatus->statusCode = 'P';

$messageResult = new SendResult();
$messageResult->responseObject = $wsResponse;
$messageResult->responseXml = Client\Util\MsgBodyExtractor::extract($dummyReply);
$messageResult->responseXml = $extractor->extract($dummyReply);
$messageResult->messageVersion = '6.1';


Expand Down
3 changes: 2 additions & 1 deletion tests/Amadeus/Client/Session/Handler/SoapHeader4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,8 @@ public function testCanHandleMessageWithSoapFault()
$this->assertInstanceOf('\SoapFault', $sendResult->exception);
$this->assertEquals('284|Application|SECURED PNR', $sendResult->exception->getMessage());
$this->assertEquals('11.3', $sendResult->messageVersion);
$this->assertEquals(Client\Util\MsgBodyExtractor::extract($dummyPnrReply), $sendResult->responseXml);
$extractor = new Client\Util\MsgBodyExtractor();
$this->assertEquals($extractor->extract($dummyPnrReply), $sendResult->responseXml);
}

public function testCanHandleMessageThrowingNonSoapFaultException()
Expand Down
6 changes: 4 additions & 2 deletions tests/Amadeus/Client/Util/MsgBodyExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function testCanExtractMessageFromSoapEnvelope()
$xml = $this->getTestFile("pnrResponseFullEnvelope.txt");
$expectedResult = $this->getTestFile("pnrResponseFullEnvelopeOnlyPnrReply.txt");

$actual = MsgBodyExtractor::extract($xml);
$extractor = new MsgBodyExtractor();
$actual = $extractor->extract($xml);

$this->assertEquals($expectedResult, $actual);
}
Expand All @@ -49,7 +50,8 @@ public function testCanExtractMessageFromSoapEnvelopeWithNewline()
$xml = $this->getTestFile("dummyPnrRetrieveWithNewLine.txt");
$expectedResult = $this->getTestFile("dummyPnrRetrieveWithNewLineOnlyPnrReply.txt");

$actual = MsgBodyExtractor::extract($xml);
$extractor = new MsgBodyExtractor();
$actual = $extractor->extract($xml);

$this->assertEquals($expectedResult, $actual);
}
Expand Down

0 comments on commit a8b79e0

Please sign in to comment.