Passed
Pull Request — master (#11)
by
unknown
14:33 queued 04:24
created

Response   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 122
ccs 28
cts 29
cp 0.9655
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A hasError() 0 4 1
A getErrorCode() 0 4 1
A getError() 0 10 4
A toArray() 0 4 1
A toObject() 0 4 1
A unwrapXMLObject() 0 9 2
A getResultCount() 0 11 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
	    $extendedErrorInfo = isset($this->response->ExtendedErrorInfo) ? (string) $this->response->ExtendedErrorInfo : '';
57 3
	    $error = isset($this->response->ErrDesc) ? (string) $this->response->ErrDesc : '';
58 3
	    if ($extendedErrorInfo) {
59
	        $error .= " ($extendedErrorInfo)";
60
        }
61
62 3
		return $error;
63
	}
64
65
	/**
66
	 * Converts the response to an Array
67
	 * @method toArray
68
	 * @throws YourMembershipResponseException
69
	 * @author PA
70
	 * @date   2017-01-10
71
	 * @return array      Response
72
	 */
73 2
	public function toArray() : array
74
	{
75 2
		return $this->unwrapXMLObject(true);
76
	}
77
78
	/**
79
	 * Converts the response to an Object
80
	 * @method toObject
81
	 * @throws YourMembershipResponseException
82
	 * @author PA
83
	 * @date   2017-01-11
84
	 * @return stdClass  Response
85
	 */
86 3
	public function toObject() : \stdClass
87
	{
88 3
		return $this->unwrapXMLObject(false);
89
	}
90
91
	/**
92
	 * Unwraps XML Object into either StdClass or Array
93
	 * Lossy conversion, attributes are lost from XML
94
	 *
95
	 * @method unwrapXMLObject
96
	 * @throws YourMembershipResponseException
97
	 * @author PA
98
	 * @date   2017-01-11
99
	 * @param  bool            $asArray unwrap the object into an array instead of object
100
	 * @return mixed|null      Unwrapped Response
101
	 */
102 5
	private function unwrapXMLObject(bool $asArray)
103
	{
104
		//We cannot unwrap objects that have errors, so throw an exception
105 5
		if ($this->hasError()) {
106 1
			throw new YourMembershipResponseException($this->getError(), $this->getErrorCode(), $this->method);
107
		}
108
109 4
		return json_decode(json_encode($this->response->{$this->method}), $asArray);
110
	}
111
	/**
112
	 * Returns the Result Count
113
	 * @method getResultCount
114
	 * @author PA
115
	 * @date   2017-01-10
116
	 * @return int|false   false if no ResultCount is present
117
	 */
118 1
	public function getResultCount() : int
119
	{
120 1
		$count = false;
121
122 1
		if (isset($this->response->{$this->method}->Results)) {
123 1
			$attributes = $this->response->{$this->method}->Results->attributes();
124 1
			$count = (int) $attributes['ResultTotal'] ?? false;
125
		}
126
127 1
		return $count;
128
	}
129
130
}
131