Passed
Branch fopcreatefop (fcebe7)
by Dieter
08:14
created

Result::isWorseStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 2
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;
24
25
use Amadeus\Client\Result\NotOk;
26
use Amadeus\Client\Session\Handler\SendResult;
27
28
/**
29
 * Result object to encapsulate the Web Services response to library users.
30
 *
31
 * @package Amadeus\Client
32
 * @author Dieter Devlieghere <[email protected]>
33
 */
34
class Result
35
{
36
    /**
37
     * Status indicator for a success situation
38
     */
39
    const STATUS_OK = 'OK';
40
    /**
41
     * Status indicator for an informational message situation.
42
     */
43
    const STATUS_INFO = 'INFO';
44
    /**
45
     * Status indicator for a warning situation.
46
     */
47
    const STATUS_WARN = 'WARN';
48
    /**
49
     * Status indicator for an error response.
50
     */
51
    const STATUS_ERROR = 'ERR';
52
    /**
53
     * Status indicator for a FATAL error response.
54
     */
55
    const STATUS_FATAL = 'FATAL';
56
    /**
57
     * Status indicator for a response which could not be checked for warnings/errors.
58
     */
59
    const STATUS_UNKNOWN = 'UNKNOWN';
60
61
    /**
62
     * Status of the result
63
     *
64
     * see self::STATUS_*
65
     *
66
     * @var string
67
     */
68
    public $status;
69
70
    /**
71
     * Array of errors or warnings found
72
     *
73
     * @var NotOk[]
74
     */
75
    public $messages = [];
76
77
    /**
78
     * The actual result received after performing the web service call.
79
     *
80
     * @var \stdClass|array
81
     */
82
    public $response;
83
84
    /**
85
     * The raw contents of the Soap Envelope received after performing the web service call.
86
     *
87
     * @var string
88
     */
89
    public $responseXml;
90
91
    /**
92
     * Result constructor.
93
     *
94
     * @param SendResult $sendResult
95
     * @param string $status
96
     */
97 139
    public function __construct($sendResult, $status = self::STATUS_OK)
98
    {
99 139
        $this->response = $sendResult->responseObject;
100 139
        $this->responseXml = $sendResult->responseXml;
101 139
        $this->status = $status;
102 139
    }
103
104
    /**
105
     * Sets error status.
106
     *
107
     * Will not override a more severe status.
108
     *
109
     * @param string $newStatus
110
     */
111 2
    public function setStatus($newStatus)
112
    {
113 2
        if ($this->isWorseStatus($newStatus, $this->status)) {
114 2
            $this->status = $newStatus;
115 2
        }
116 2
    }
117
118
    /**
119
     * Checks if new status is worse than current status
120
     *
121
     * @param string $newStatus
122
     * @param string $currentStatus
123
     * @return bool true if newStatus is worse than old status.
124
     */
125 2
    protected function isWorseStatus($newStatus, $currentStatus)
126
    {
127
        $levels = [
128 2
            self::STATUS_UNKNOWN => -1,
129 2
            self::STATUS_OK => 0,
130 2
            self::STATUS_INFO => 2,
131 2
            self::STATUS_WARN => 5,
132 2
            self::STATUS_ERROR => 10,
133 2
            self::STATUS_FATAL => 20,
134 2
        ];
135
136 2
        return ($currentStatus === null || $levels[$newStatus] > $levels[$currentStatus]);
137
    }
138
}
139