Passed
Branch master (6e6484)
by Petr
06:04
created

Response::hasError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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