Response::__construct()   B
last analyzed

Complexity

Conditions 11
Paths 71

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 11.209

Importance

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