Passed
Push — extended-error-info ( 9ff7c9...d02425 )
by
unknown
04:41
created

Response::getExtendedError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace P2A\YourMembership\Core;
4
5
use P2A\YourMembership\Exceptions\YourMembershipResponseException;
6
/**
7
 * Your Membership Response Object
8
 */
9
class Response
10
{
11
	private $method;
12
	private $response;
13
14 9
	public function __construct(string $method, $response)
15
	{
16 9
		$this->method = $method;
17
18 9
		$body = $response->getBody()->getContents();
19 9
		$this->response = new \SimpleXMLElement($body);
20
21 9
	}
22
23
	/**
24
	 * Checks if the response contains an Error
25
	 * @method hasError
26
	 * @author PA
27
	 * @date   2017-01-10
28
	 * @return bool       hasError
29
	 */
30 6
	public function hasError() : bool
31
	{
32 6
		return ($this->getErrorCode() != 0);
33
	}
34
35
	/**
36
	 * Fetches the Error Code from the Response
37
	 * @method getErrorCode
38
	 * @author PA
39
	 * @date   2017-01-10
40
	 * @return int          Error Code
41
	 */
42 7
	public function getErrorCode() : int
43
	{
44 7
		return (int) $this->response->ErrCode;
45
	}
46
47
	/**
48
	 * Fetches the Error Message (including Extended Error Info) From Response
49
	 * @method getError
50
	 * @author PA
51
	 * @date   2017-01-10
52
	 * @return string     Error Message
53
	 */
54 3
	public function getError() : string
55
	{
56 3
	    $error = (string) $this->response->ErrDesc;
57 3
	    if ($extendedError = $this->getExtendedError()) {
58
	        $error .= " ($extendedError)";
59
        }
60
61 3
		return $error;
62
	}
63
64
    /**
65
     * Fetches the Extended Error from the Response
66
     * @return string
67
     * @author TLS
68
     * @date   1-30-2017
69
     */
70 3
	public function getExtendedError() : string
71
    {
72 3
        return isset($this->response->ExtendedErrorInfo) ? (string) $this->response->ExtendedErrorInfo : '';
73
    }
74
75
	/**
76
	 * Converts the response to an Array
77
	 * @method toArray
78
	 * @throws YourMembershipResponseException
79
	 * @author PA
80
	 * @date   2017-01-10
81
	 * @return array      Response
82
	 */
83 2
	public function toArray() : array
84
	{
85 2
		return $this->unwrapXMLObject(true);
86
	}
87
88
	/**
89
	 * Converts the response to an Object
90
	 * @method toObject
91
	 * @throws YourMembershipResponseException
92
	 * @author PA
93
	 * @date   2017-01-11
94
	 * @return stdClass  Response
95
	 */
96 3
	public function toObject() : \stdClass
97
	{
98 3
		return $this->unwrapXMLObject(false);
99
	}
100
101
	/**
102
	 * Unwraps XML Object into either StdClass or Array
103
	 * Lossy conversion, attributes are lost from XML
104
	 *
105
	 * @method unwrapXMLObject
106
	 * @throws YourMembershipResponseException
107
	 * @author PA
108
	 * @date   2017-01-11
109
	 * @param  bool            $asArray unwrap the object into an array instead of object
110
	 * @return mixed|null      Unwrapped Response
111
	 */
112 5
	private function unwrapXMLObject(bool $asArray)
113
	{
114
		//We cannot unwrap objects that have errors, so throw an exception
115 5
		if ($this->hasError()) {
116 1
			throw new YourMembershipResponseException($this->getError(), $this->getErrorCode(), $this->method);
117
		}
118
119 4
		return json_decode(json_encode($this->response->{$this->method}), $asArray);
120
	}
121
	/**
122
	 * Returns the Result Count
123
	 * @method getResultCount
124
	 * @author PA
125
	 * @date   2017-01-10
126
	 * @return int|false   false if no ResultCount is present
127
	 */
128 1
	public function getResultCount() : int
129
	{
130 1
		$count = false;
131
132 1
		if (isset($this->response->{$this->method}->Results)) {
133 1
			$attributes = $this->response->{$this->method}->Results->attributes();
134 1
			$count = (int) $attributes['ResultTotal'] ?? false;
135
		}
136
137 1
		return $count;
138
	}
139
140
}
141