Completed
Push — feature/refactor-app-design ( aadb9c...84f9ff )
by Avtandil
02:37
created

Response::isAssoc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Longman\TelegramBot\Http;
10
11
use Longman\TelegramBot\Entities\Chat;
12
use Longman\TelegramBot\Entities\ChatMember;
13
use Longman\TelegramBot\Entities\File;
14
use Longman\TelegramBot\Entities\Message;
15
use Longman\TelegramBot\Entities\Update;
16
use Longman\TelegramBot\Entities\User;
17
use Longman\TelegramBot\Entities\UserProfilePhotos;
18
use Longman\TelegramBot\Entities\WebhookInfo;
19
20
/**
21
 * Class Response
22
 *
23
 * @link https://core.telegram.org/bots/api#making-requests
24
 *
25
 * @todo method ResponseParameters getParameters()  Field which can help to automatically handle the error
26
 */
27
class Response
28
{
29
    /** @var bool */
30
    protected $ok = false;
31
32
    /** @var string */
33
    protected $description;
34
35
    /** @var array */
36
    protected $result;
37 11
38
    /** @var int */
39
    protected $error_code;
40 11
41 11
    /** @var array */
42
    protected $parameters;
43 11
44 11
    /** @var array */
45
    protected $raw_data;
46 11
47 6
    /** @var string */
48 4
    protected $bot_username;
49
50 2
    /**
51
     * Response constructor.
52
     *
53
     * @param array $data
54
     * @param string $bot_username
55 11
     *
56 11
     * @throws \Longman\TelegramBot\Exception\TelegramException
57 11
     */
58
    public function __construct(array $data, $bot_username = '')
59
    {
60
        $this->raw_data = $data;
61
62
        $this->ok = $data['ok'];
63 11
64
        if (isset($data['description'])) {
65 11
            $this->description = $data['description'];
66 11
        }
67
68
        if (isset($data['error_code'])) {
69
            $this->error_code = $data['error_code'];
70
        }
71
72
        if (isset($data['parameters'])) {
73 11
            $this->parameters = $data['parameters'];
74
        }
75 11
76 11
        $this->bot_username = $bot_username;
77
78 11
        $result = isset($data['result']) ? $data['result'] : null;
79
        if ($this->ok && is_array($result)) {
80
            if ($this->isAssoc($result)) {
81
                $this->result = $this->createResultObject($result, $bot_username);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createResultObject($result, $bot_username) of type object<Longman\TelegramBot\Entities\Chat> or object<Longman\TelegramBot\Entities\ChatMember> or object<Longman\TelegramBot\Entities\File> or object<Longman\TelegramBot\Entities\Message> or object<Longman\TelegramBot\Entities\User> or object<Longman\TelegramB...ties\UserProfilePhotos> or object<Longman\TelegramBot\Entities\WebhookInfo> is incompatible with the declared type array of property $result.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
82
            } else {
83
                $this->result = $this->createResultObjects($result, $bot_username);
84
            }
85
        }
86
    }
87
88
    /**
89 6
     * Check if array is associative
90
     *
91 6
     * @link https://stackoverflow.com/a/4254008
92
     *
93
     * @param array $array
94
     *
95
     * @return bool
96
     */
97
    protected function isAssoc(array $array)
98
    {
99 5
        return count(array_filter(array_keys($array), 'is_string')) > 0;
100
    }
101 5
102
    /**
103
     * If response is ok
104
     *
105
     * @return bool
106
     */
107
    public function isOk()
108
    {
109 5
        return $this->ok;
110
    }
111 5
112
    /**
113
     * Return result
114
     *
115
     * @return array
116
     */
117
    public function getResult()
118
    {
119 9
        return ! empty($this->result) ? $this->result : [];
120
    }
121 9
122
    /**
123
     * Return error code
124
     *
125
     * @return int
126
     */
127
    public function getErrorCode()
128
    {
129 5
        return ! empty($this->error_code) ? $this->error_code : null;
130
    }
131 5
132
    /**
133
     * Return human-readable description of the result / unsuccessful request
134
     *
135
     * @return string
136
     */
137
    public function getDescription()
138
    {
139 5
        return ! empty($this->description) ? $this->description : null;
140
    }
141 5
142
    /**
143
     * Get error
144
     *
145
     * @see https://secure.php.net/manual/en/function.print-r.php
146
     *
147
     * @return string
148
     */
149
    public function getError()
150
    {
151
        $error = sprintf('Error N: %s, Description: %s', $this->getErrorCode(), $this->getDescription());
152
153
        return $error;
154
    }
155
156
    /**
157
     * Create and return the object of the received result
158
     *
159
     * @param array $result
160
     * @param string $bot_username
161
     *
162
     * @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo
163
     * @throws \Longman\TelegramBot\Exception\TelegramException
164
     */
165
    protected function createResultObject($result, $bot_username)
166
    {
167
        $result_object_types = [
168
            'total_count' => UserProfilePhotos::class, // Response from getUserProfilePhotos
169
            'file_id'     => File::class,              // Response from getFile
170
            'title'       => Chat::class,              // Response from getChat
171
            'username'    => User::class,              // Response from getMe
172
            'user'        => ChatMember::class,        // Response from getChatMember
173
            'url'         => WebhookInfo::class,       // Response from getWebhookInfo
174
        ];
175 4
        foreach ($result_object_types as $type => $object_class) {
176
            if (isset($result[$type])) {
177
                return new $object_class($result, $bot_username);
178 4
            }
179
        }
180
181 4
        // Response from sendMessage
182
        return new Message($result, $bot_username);
183
    }
184
185
    /**
186
     * Create and return the objects array of the received result
187
     *
188 4
     * @param array $result
189 4
     * @param string $bot_username
190 4
     *
191
     * @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[]
192
     * @throws \Longman\TelegramBot\Exception\TelegramException
193
     */
194
    protected function createResultObjects($result, $bot_username)
195 2
    {
196
        $results = [];
197
        if (isset($result[0]['user'])) {
198
            // Response from getChatAdministrators
199
            foreach ($result as $user) {
200
                // We don't need to save the raw_data of the response object!
201
                $user['raw_data'] = null;
202
203
                $results[] = new ChatMember($user);
204
            }
205
        } else {
206
            // Get Update
207 2
            foreach ($result as $update) {
208
                // We don't need to save the raw_data of the response object!
209 2
                $update['raw_data'] = null;
210 2
211
                $results[] = new Update($update, $bot_username);
212
            }
213
        }
214
215
        return $results;
216
    }
217
}
218