1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* amadeus-ws-client |
4
|
|
|
* |
5
|
|
|
* Copyright 2015 Amadeus Benelux NV |
6
|
|
|
* |
7
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
8
|
|
|
* you may not use this file except in compliance with the License. |
9
|
|
|
* You may obtain a copy of the License at |
10
|
|
|
* |
11
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
12
|
|
|
* |
13
|
|
|
* Unless required by applicable law or agreed to in writing, software |
14
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
15
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16
|
|
|
* See the License for the specific language governing permissions and |
17
|
|
|
* limitations under the License. |
18
|
|
|
* |
19
|
|
|
* @package Amadeus |
20
|
|
|
* @license https://opensource.org/licenses/Apache-2.0 Apache 2.0 |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Amadeus\Client\ResponseHandler; |
24
|
|
|
|
25
|
|
|
use Amadeus\Client\Exception; |
26
|
|
|
use Amadeus\Client\Result; |
27
|
|
|
use Amadeus\Client\Session\Handler\SendResult; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Default Response Handler |
31
|
|
|
* |
32
|
|
|
* Analyses the responses received from the Amadeus WS server and checks for error messages. |
33
|
|
|
* If errors are found, the error information will be extracted and the response status will be changed |
34
|
|
|
* accordingly. |
35
|
|
|
* |
36
|
|
|
* @package Amadeus\Client\ResponseHandler |
37
|
|
|
* @author Dieter Devlieghere <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
class Base implements ResponseHandlerInterface |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* All response handlers already instantiated |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $responseHandlers = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Analyze the response from the server and throw an exception when an error has been detected. |
50
|
|
|
* |
51
|
|
|
* @param SendResult $sendResult The Send Result from the Session Handler |
52
|
|
|
* @param string $messageName The message that was called |
53
|
|
|
* |
54
|
|
|
* @throws Exception |
55
|
|
|
* @throws \RuntimeException |
56
|
|
|
* @return Result |
57
|
|
|
*/ |
58
|
635 |
|
public function analyzeResponse($sendResult, $messageName) |
59
|
|
|
{ |
60
|
635 |
|
if (!empty($sendResult->exception)) { |
61
|
15 |
|
return $this->makeResultForException($sendResult); |
62
|
|
|
} |
63
|
|
|
|
64
|
620 |
|
$handler = $this->findHandlerForMessage($messageName); |
65
|
|
|
|
66
|
620 |
|
if ($handler instanceof MessageResponseHandler) { |
67
|
615 |
|
return $handler->analyze($sendResult); |
68
|
|
|
} else { |
69
|
5 |
|
return new Result($sendResult, Result::STATUS_UNKNOWN); |
70
|
2 |
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param SendResult $sendResult |
75
|
|
|
* @return Result |
76
|
|
|
*/ |
77
|
15 |
|
protected function makeResultForException($sendResult) |
78
|
|
|
{ |
79
|
15 |
|
$result = new Result($sendResult, Result::STATUS_FATAL); |
80
|
|
|
|
81
|
15 |
|
$result->messages[] = $this->makeMessageFromException($sendResult->exception); |
82
|
|
|
|
83
|
15 |
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param \Exception $exception |
88
|
|
|
* @return Result\NotOk |
89
|
|
|
* @throws Exception |
90
|
|
|
*/ |
91
|
15 |
|
protected function makeMessageFromException(\Exception $exception) |
92
|
|
|
{ |
93
|
15 |
|
$message = new Result\NotOk(); |
94
|
|
|
|
95
|
15 |
|
if ($exception instanceof \SoapFault) { |
96
|
15 |
|
$info = explode('|', $exception->getMessage()); |
97
|
15 |
|
$message->code = $info[0]; |
98
|
15 |
|
if (count($info) === 3) { |
99
|
15 |
|
$message->level = $info[1]; |
100
|
15 |
|
$message->text = $info[2]; |
101
|
6 |
|
} |
102
|
6 |
|
} |
103
|
|
|
|
104
|
15 |
|
return $message; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Find or create the correct handler object for a given message |
109
|
|
|
* |
110
|
|
|
* @param string $messageName |
111
|
|
|
* @return MessageResponseHandler|null |
112
|
|
|
*/ |
113
|
620 |
|
private function findHandlerForMessage($messageName) |
114
|
|
|
{ |
115
|
620 |
|
$handler = null; |
116
|
|
|
|
117
|
620 |
|
if (array_key_exists($messageName, $this->responseHandlers) && |
118
|
374 |
|
$this->responseHandlers[$messageName] instanceof MessageResponseHandler |
119
|
248 |
|
) { |
120
|
5 |
|
$handler = $this->responseHandlers[$messageName]; |
121
|
2 |
|
} else { |
122
|
620 |
|
$section = substr($messageName, 0, strpos($messageName, '_')); |
123
|
620 |
|
$message = substr($messageName, strpos($messageName, '_') + 1); |
124
|
|
|
|
125
|
620 |
|
$handlerClass = __NAMESPACE__.'\\'.$section.'\\Handler'.$message; |
126
|
|
|
|
127
|
620 |
|
if (class_exists($handlerClass)) { |
128
|
|
|
/** @var MessageResponseHandler $handler */ |
129
|
615 |
|
$handler = new $handlerClass(); |
130
|
|
|
|
131
|
615 |
|
$this->responseHandlers[$messageName] = $handler; |
132
|
246 |
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
620 |
|
return $handler; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|