Completed
Push — master ( 234de9...b59dba )
by Massimiliano
02:25
created

Response::__construct()   C

Complexity

Conditions 11
Paths 71

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 11.209

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 45
ccs 22
cts 25
cp 0.88
rs 5.2653
cc 11
eloc 26
nc 71
nop 1
crap 11.209

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use Fazland\SkebbyRestClient\Exception\XmlLoadException;
9
10
/**
11
 * @author Massimiliano Braglia <[email protected]>
12
 */
13
class Response
14
{
15
    /**
16
     * @var string
17
     */
18
    private $status;
19
20
    /**
21
     * @var string
22
     */
23
    private $code;
24
25
    /**
26
     * @var string
27
     */
28
    private $errorMessage;
29
30
    /**
31
     * @var string
32
     */
33
    private $messageId;
34
35
    /**
36
     * @param string $rawResponse
37
     *
38
     * @throws EmptyResponseException
39
     * @throws UnknownErrorResponseException
40
     */
41 8
    public function __construct($rawResponse)
42
    {
43 8
        if (empty($rawResponse)) {
44 1
            throw new EmptyResponseException();
45
        }
46
47 7
        $doc = null;
48
49 7
        $useErrors = libxml_use_internal_errors(true);
50
        try {
51 7
            $doc = @simplexml_load_string($rawResponse);
52
53 7
            if (false === $doc) {
54 7
                throw new XmlLoadException($rawResponse, libxml_get_errors());
55
            }
56
        } catch (\Throwable $e) {
57
            throw new UnknownErrorResponseException($e->getMessage(), $rawResponse);
58 7
        } finally {
59 7
            libxml_use_internal_errors($useErrors);
60
        }
61
62 7
        foreach (SendMethods::all() as $method) {
63 7
            if (! isset($doc->$method)) {
64 7
                continue;
65
            }
66
67 7
            $element = $doc->$method;
68
69 7
            if (! isset($element->status)) {
70 1
                throw new UnknownErrorResponseException('Missing response status value from Skebby', $rawResponse);
71
            }
72
73 6
            $this->status = (string)$element->status;
74 6
            $this->messageId = isset($element->id) ? (string)$element->id : null;
75
76 6
            if (! $this->isSuccessful()) {
77 1
                $this->code = isset($element->code) ? (string)$element->code : null;
78 1
                $this->errorMessage = isset($element->message) ? (string)$element->message : 'Unknown error';
79
            }
80
81 6
            return;
82
        }
83
84
        throw new UnknownErrorResponseException('Missing response status value from Skebby', $rawResponse);
85
    }
86
87
    /**
88
     * @return string
89
     */
90 1
    public function getStatus()
91
    {
92 1
        return $this->status;
93
    }
94
95 6
    public function isSuccessful()
96
    {
97 6
        return 'success' === $this->status;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getCode()
104
    {
105
        return $this->code;
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getErrorMessage()
112
    {
113
        return $this->errorMessage;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getMessageId()
120
    {
121
        return $this->messageId;
122
    }
123
124
    public function __toString()
125
    {
126
        return "Response status: $this->status, code: $this->code, error_message: $this->errorMessage, message_id: $this->messageId";
127
    }
128
}
129