Completed
Push — master ( 624987...d784f3 )
by Massimiliano
09:03
created

Response::__construct()   D

Complexity

Conditions 9
Paths 14

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 10.8889

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 10
cts 14
cp 0.7143
rs 4.909
c 0
b 0
f 0
cc 9
eloc 17
nc 14
nop 1
crap 10.8889
1
<?php
2
3
namespace Fazland\SkebbyRestClient\DataStructure;
4
5
use Fazland\SkebbyRestClient\Constant\SendMethods;
6
use Fazland\SkebbyRestClient\Exception\EmptyResponseException;
7
use Fazland\SkebbyRestClient\Exception\UnknownErrorResponseException;
8
9
/**
10
 * @author Massimiliano Braglia <[email protected]>
11
 */
12
class Response
13
{
14
    /**
15
     * @var string
16
     */
17
    private $status;
18
19
    /**
20
     * @var string
21
     */
22
    private $code;
23
24
    /**
25
     * @var string
26
     */
27
    private $errorMessage;
28
29
    /**
30
     * @var string
31
     */
32
    private $messageId;
33
34
    /**
35
     * @param string $rawResponse
36
     *
37
     * @throws EmptyResponseException
38
     * @throws UnknownErrorResponseException
39 6
     */
40
    public function __construct($rawResponse)
41 6
    {
42 1
        if (empty($rawResponse)) {
43
            throw new EmptyResponseException();
44
        }
45 5
46
        $doc = simplexml_load_string($rawResponse);
47 5
        foreach (SendMethods::all() as $method) {
48 1
            if (! isset($doc->$method)) {
49
                continue;
50
            }
51 4
52 4
            $element = $doc->$method;
53 4
54 4
            if (! isset($element->status)) {
55 4
                throw new UnknownErrorResponseException("Missing response status value from Skebby");
56
            }
57
58
            $this->status = (string)$element->status;
59
            $this->messageId = isset($element->id) ? (string)$element->id : null;
60
61
            if (! $this->isSuccessful()) {
62
                $this->code = isset($element->code) ? (string)$element->code : null;
63
                $this->errorMessage = isset($element->message) ? (string)$element->message : 'Unknown error';
64
            }
65
66
            return;
67
        }
68
69
        throw new UnknownErrorResponseException("Missing response status value from Skebby");
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getStatus()
76
    {
77
        return $this->status;
78
    }
79
80
    public function isSuccessful()
81
    {
82
        return 'success' === $this->status;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getCode()
89
    {
90
        return $this->code;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getErrorMessage()
97
    {
98
        return $this->errorMessage;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getMessageId()
105
    {
106
        return $this->messageId;
107
    }
108
}
109