Completed
Pull Request — master (#15)
by Massimiliano
05:59
created

Response   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 70.27%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 129
ccs 26
cts 37
cp 0.7027
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Fazland\SkebbyRestClient\DataStructure;
5
6
use Fazland\SkebbyRestClient\Constant\SendMethods;
7
use Fazland\SkebbyRestClient\Exception\EmptyResponseException;
8
use Fazland\SkebbyRestClient\Exception\UnknownErrorResponseException;
9
use Fazland\SkebbyRestClient\Exception\XmlLoadException;
10
11
/**
12
 * Represents a Skebby Response.
13
 *
14
 * @author Massimiliano Braglia <[email protected]>
15
 */
16
class Response
17
{
18
    private string $status;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

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