Completed
Push — master ( ec0178...c6402c )
by Alessandro
04:17
created

Response::isSuccessful()   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 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
     */
40 7
    public function __construct($rawResponse)
41
    {
42 7
        if (empty($rawResponse)) {
43 1
            throw new EmptyResponseException();
44
        }
45
46 6
        $doc = simplexml_load_string($rawResponse);
47 6
        foreach (SendMethods::all() as $method) {
48 6
            if (! isset($doc->$method)) {
49 6
                continue;
50
            }
51
52 6
            $element = $doc->$method;
53
54 6
            if (! isset($element->status)) {
55 1
                throw new UnknownErrorResponseException('Missing response status value from Skebby');
56
            }
57
58 5
            $this->status = (string)$element->status;
59 5
            $this->messageId = isset($element->id) ? (string)$element->id : null;
60
61 5
            if (! $this->isSuccessful()) {
62 1
                $this->code = isset($element->code) ? (string)$element->code : null;
63 1
                $this->errorMessage = isset($element->message) ? (string)$element->message : 'Unknown error';
64 1
            }
65
66 5
            return;
67
        }
68
69
        throw new UnknownErrorResponseException('Missing response status value from Skebby');
70
    }
71
72
    /**
73
     * @return string
74
     */
75 1
    public function getStatus()
76
    {
77 1
        return $this->status;
78
    }
79
80 5
    public function isSuccessful()
81
    {
82 5
        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