Completed
Push — master ( 27f914...b1731d )
by Dieter
05:26
created

Base::analyzeAirMultiAvailabilityResponse()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 15

Duplication

Lines 26
Ratio 100 %

Code Coverage

Tests 18
CRAP Score 4

Importance

Changes 0
Metric Value
dl 26
loc 26
ccs 18
cts 18
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 5
nop 1
crap 4
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 80
     * @param string $messageName The message that was called
53
     *
54 80
     * @throws Exception
55
     * @throws \RuntimeException
56 80
     * @return Result
57 2
     */
58 78
    public function analyzeResponse($sendResult, $messageName)
59 77
    {
60
        if (!empty($sendResult->exception)) {
61 77
            return $this->makeResultForException($sendResult);
62
        }
63 1
64
        $handler = $this->findHandlerForMessage($messageName);
65
66
        if ($handler instanceof MessageResponseHandler) {
67
            return $handler->analyze($sendResult);
68
        } else {
69
            return new Result($sendResult, Result::STATUS_UNKNOWN);
70
        }
71
    }
72
73 2
    /**
74
     * @param SendResult $sendResult
75 2
     * @return Result
76
     */
77
    protected function makeResultForException($sendResult)
78
    {
79
        $result = new Result($sendResult, Result::STATUS_FATAL);
80
81
        $result->messages[] = $this->makeMessageFromException($sendResult->exception);
82
83
        return $result;
84 2
    }
85
86 2
    /**
87
     * @param \Exception $exception
88
     * @return Result\NotOk
89
     * @throws Exception
90
     */
91
    protected function makeMessageFromException(\Exception $exception)
92
    {
93
        $message = new Result\NotOk();
94
95 1
        if ($exception instanceof \SoapFault) {
96
            $info = explode('|', $exception->getMessage());
97 1
            $message->code = $info[0];
98 1
            if (count($info) === 3) {
99 1
                $message->level = $info[1];
100
                $message->text = $info[2];
101 1
            }
102
        }
103 1
104
        return $message;
105
    }
106 1
107
    /**
108 1
     * Find or create the correct handler object for a given message
109
     *
110 1
     * @param string $messageName
111
     * @return MessageResponseHandler|null
112 1
     */
113 View Code Duplication
    private function findHandlerForMessage($messageName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114 1
    {
115 1
        $handler = null;
116 1
117
        if (array_key_exists($messageName, $this->responseHandlers) &&
118 1
            $this->responseHandlers[$messageName] instanceof MessageResponseHandler
119 1
        ) {
120 1
            $handler = $this->responseHandlers[$messageName];
121 1
        } else {
122
            $section = substr($messageName, 0, strpos($messageName, '_'));
123 1
            $message = substr($messageName, strpos($messageName, '_') + 1);
124 1
125 1
            $handlerClass = __NAMESPACE__.'\\'.$section.'\\Handler'.$message;
126 1
127 1
            if (class_exists($handlerClass)) {
128 1
                /** @var MessageResponseHandler $handler */
129
                $handler = new $handlerClass();
130 1
131
                $this->responseHandlers[$messageName] = $handler;
132
            }
133 2
        }
134
135 2
        return $handler;
136
    }
137
}
138