Completed
Pull Request — develop (#288)
by Armando
27:44 queued 12:40
created

ServerResponse   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 75.44%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 31
c 5
b 0
f 2
lcom 1
cbo 8
dl 0
loc 159
ccs 43
cts 57
cp 0.7544
rs 9.8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isAssoc() 0 4 1
C __construct() 0 67 25
A isOk() 0 4 1
A getResult() 0 4 1
A getErrorCode() 0 4 1
A getDescription() 0 4 1
A printError() 0 4 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\Entities;
10
11
class ServerResponse extends Entity
12
{
13
    /**
14
     * @var bool
15
     */
16
    protected $ok;
17
18
    /**
19
     * @var null
20
     */
21
    protected $result;
22
23
    /**
24
     * @var null
25
     */
26
    protected $error_code;
27
28
    /**
29
     * @var null
30
     */
31
    protected $description;
32
33
    /**
34
     * ServerResponse constructor.
35
     *
36
     * @param array $data
37
     * @param       $bot_name
38
     *
39 14
     * @throws \Longman\TelegramBot\Exception\TelegramException
40
     */
41 14
    public function __construct(array $data, $bot_name)
42 12
    {
43 11
        if (isset($data['ok'], $data['result'])) {
44
            if (is_array($data['result'])) {
45 2
                if ($data['ok'] && !$this->isAssoc($data['result']) && !isset($data['result'][0]['user'])) {
46 2
                    //Get Update
47
                    foreach ($data['result'] as $update) {
48 9
                        $this->result[] = new Update($update, $bot_name);
49
                    }
50
                } elseif ($data['ok'] && !$this->isAssoc($data['result']) && isset($data['result'][0]['user'])) {
51
                    //Response from getChatAdministrators
52
                    $this->result = [];
0 ignored issues
show
Documentation Bug introduced by
It seems like array() of type array is incompatible with the declared type null 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...
53
                    foreach ($data['result'] as $user) {
54 9
                        $this->result[] = new ChatMember($user);
55 9
                    }
56
                } elseif ($data['ok'] && $this->isAssoc($data['result'])) {
57 1
                    if (isset($data['result']['total_count'])) {
58 8
                        //Response from getUserProfilePhotos
59
                        $this->result = new UserProfilePhotos($data['result']);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Longman\TelegramBot...Photos($data['result']) of type object<Longman\TelegramB...ties\UserProfilePhotos> is incompatible with the declared type null 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...
60 1
                    } elseif (isset($data['result']['file_id'])) {
61 7
                        //Response from getFile
62
                        $this->result = new File($data['result']);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Longman\TelegramBot...s\File($data['result']) of type object<Longman\TelegramBot\Entities\File> is incompatible with the declared type null 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...
63
                    } elseif (isset($data['result']['username'])) {
64 7
                        //Response from getMe
65
                        $this->result = new User($data['result']);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Longman\TelegramBot...s\User($data['result']) of type object<Longman\TelegramBot\Entities\User> is incompatible with the declared type null 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...
66
                    } elseif (isset($data['result']['id'])) {
67 7
                        //Response from getChat
68
                        $this->result = new Chat($data['result']);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Longman\TelegramBot...s\Chat($data['result']) of type object<Longman\TelegramBot\Entities\Chat> is incompatible with the declared type null 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...
69
                    } elseif (isset($data['result']['user'])) {
70
                        //Response from getChatMember
71
                        $this->result = new ChatMember($data['result']);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Longman\TelegramBot...Member($data['result']) of type object<Longman\TelegramBot\Entities\ChatMember> is incompatible with the declared type null 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...
72 7
                    } else {
73
                        //Response from sendMessage
74
                        $this->result = new Message($data['result'], $bot_name);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Longman\TelegramBot...a['result'], $bot_name) of type object<Longman\TelegramBot\Entities\Message> is incompatible with the declared type null 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...
75
                    }
76 11
                }
77 11
78 11
                $this->ok          = $data['ok'];
79
                $this->error_code  = null;
80 2
                $this->description = null;
81
            } else {
82 2
                if ($data['ok'] && $data['result'] === true) {
83 2
                    //Response from setWebhook set
84 2
                    $this->ok          = $data['ok'];
85
                    $this->result      = true;
0 ignored issues
show
Documentation Bug introduced by
It seems like true of type boolean is incompatible with the declared type null 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...
86 2
                    $this->error_code  = null;
87 1
                    $this->description = isset($data['description']) ? $data['description'] : '';
88
                } elseif (is_numeric($data['result'])) {
89 2
                    //Response from getChatMembersCount
90
                    $this->result = $data['result'];
0 ignored issues
show
Documentation Bug introduced by
It seems like $data['result'] of type integer or double or string is incompatible with the declared type null 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...
91
                } else {
92
                    $this->ok          = false;
93
                    $this->result      = null;
94
                    $this->error_code  = $data['error_code'];
95
                    $this->description = $data['description'];
96
                }
97
            }
98 12
        } else {
99
            //webHook not set
100
            $this->ok          = false;
101
            $this->result      = isset($data['result']) ? $data['result'] : null;
102
            $this->error_code  = isset($data['error_code']) ? $data['error_code'] : null;
103 2
            $this->description = isset($data['description']) ? $data['description'] : null;
104
105 2
            //throw new TelegramException('ok(variable) is not set!');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
106
        }
107
    }
108 2
109
    /**
110
     * Check if array is associative
111 2
     *
112 2
     * @param array $array
113
     * @return bool
114
     */
115
    protected function isAssoc(array $array)
116
    {
117 2
        return (bool) count(array_filter(array_keys($array), 'is_string'));
118 2
    }
119
120
    /**
121
     * If response is ok
122
     *
123
     * @return bool
124
     */
125 14
    public function isOk()
126
    {
127
        return $this->ok;
128
    }
129
130
    /**
131
     * Get result
132
     *
133 11
     * @return string
134
     */
135 11
    public function getResult()
136
    {
137
        return $this->result;
138
    }
139
140
    /**
141
     * Get error code
142
     *
143 5
     * @return string
144
     */
145 5
    public function getErrorCode()
146
    {
147
        return $this->error_code;
148
    }
149
150
    /**
151
     * Get description
152
     *
153 14
     * @return null
154
     */
155 14
    public function getDescription()
156
    {
157
        return $this->description;
158
    }
159
160
    /**
161
     * Print error
162
     *
163 5
     * @return string
164
     */
165 5
    public function printError()
166
    {
167
        return 'Error N: ' . $this->getErrorCode() . ' Description: ' . $this->getDescription();
168
    }
169
}
170