Completed
Push — master ( fbac60...3b23d7 )
by Camilo
14s
created

TelegramResponse::getException()   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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace unreal4u\TelegramAPI\InternalFunctionality;
6
7
use unreal4u\TelegramAPI\Exceptions\InvalidResultType;
8
9
class TelegramResponse
10
{
11
    /**
12
     * Nothing is done so far with this, but it's always a good idea to have the original around
13
     * @var string
14
     */
15
    private $rawData = '';
16
17
    /**
18
     * The actual representation of the decoded data
19
     * @var array
20
     */
21
    private $decodedData = [];
22
23
    /**
24
     * The headers sent with the response.
25
     * @var array
26
     */
27
    private $headers = [];
28
29
    /**
30
     * @var \Exception
31
     */
32
    private $exception = null;
33
34
    /**
35
     * Marks the actual response as an error
36
     * @var bool
37
     */
38
    private $isError = false;
39
40 41
    public function __construct(string $rawData, array $headers = [], \Exception $e = null)
41
    {
42 41
        $this->fillRawData($rawData);
43 41
        $this->headers = $headers;
44
45 41
        if (!is_null($e)) {
46 1
            $this->exception = $e;
47 1
            $this->isError = true;
48
        }
49 41
    }
50
51
    /**
52
     * Will return true if the request was an unsuccessful one, false otherwise
53
     * @return bool
54
     */
55 1
    public function isError(): bool
56
    {
57 1
        return $this->isError;
58
    }
59
60
    /**
61
     * Fills in the raw data
62
     *
63
     * @param string $rawData
64
     * @return TelegramResponse
65
     */
66 41
    public function fillRawData(string $rawData): TelegramResponse
67
    {
68 41
        $this->rawData = $rawData;
69 41
        $this->decodedData = json_decode($this->rawData, true);
70
71 41
        return $this;
72
    }
73
74
    /**
75
     * To quickly find out what type of request we are dealing with
76
     *
77
     * Unused so far
78
     *
79
     * @return string
80
     * @throws InvalidResultType
81
     */
82 8
    public function getTypeOfResult(): string
83
    {
84 8
        switch (gettype($this->decodedData['result'])) {
85 8
            case 'array':
86 6
            case 'integer':
87 5
            case 'boolean':
88 5
                return gettype($this->decodedData['result']);
89
            default:
90 3
                throw new InvalidResultType(
91 3
                    sprintf('The passed data type ("%s") is not supported', gettype($this->decodedData['result']))
92
                );
93
        }
94
    }
95
96
    /**
97
     * Most of the requests Telegram sends, come as an array, so send the response back as an array by default
98
     *
99
     * @return array
100
     */
101 23
    public function getResult(): array
102
    {
103 23
        return array_key_exists('result', $this->decodedData) ? (array) $this->decodedData['result'] : [];
104
    }
105
106
    /**
107
     * Hack: for some requests Telegram sends back an array, integer, string or a boolean value, convert it to boolean
108
     * here
109
     * @return bool
110
     */
111 5
    public function getResultBoolean(): bool
112
    {
113 5
        return array_key_exists('result', $this->decodedData) ? (bool) $this->decodedData['result'] : false;
114
    }
115
116
    /**
117
     * Hack: for some requests Telegram send back an array, integer, string or a boolean value, convert it to int here
118
     * @return int
119
     */
120 1
    public function getResultInt(): int
121
    {
122 1
        return array_key_exists('result', $this->decodedData) ? (int)$this->decodedData['result'] : -1;
123
    }
124
125
    /**
126
     * Hack: for some requests Telegram send back an array, integer, string or a boolean value, convert it to string
127
     * here
128
     * @return string
129
     */
130 2
    public function getResultString(): string
131
    {
132 2
        return array_key_exists('result', $this->decodedData) ? (string)$this->decodedData['result'] : '';
133
    }
134
135
    /**
136
     * @return string
137
     */
138 1
    public function getRawData()
139
    {
140 1
        return $this->rawData;
141
    }
142
143
    /**
144
     * @return array
145
     */
146 2
    public function getHeaders(): array
147
    {
148 2
        return $this->headers;
149
    }
150
151
    /**
152
     * Returns the raw error data
153
     * @return array
154
     */
155 1
    public function getErrorData(): array
156
    {
157 1
        return $this->decodedData;
158
    }
159
160
    /**
161
     * @return \Exception
162
     */
163 1
    public function getException(): \Exception
164
    {
165 1
        return $this->exception;
166
    }
167
}
168