GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ServerResponse::createResultObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
nc 1
nop 2
dl 0
loc 19
ccs 6
cts 6
cp 1
crap 1
rs 9.8666
c 2
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the TelegramBot package.
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Longman\TelegramBot\Entities;
11
12
use Longman\TelegramBot\Entities\ChatMember\ChatMember;
13
use Longman\TelegramBot\Entities\ChatMember\Factory as ChatMemberFactory;
14
use Longman\TelegramBot\Entities\Games\GameHighScore;
15
use Longman\TelegramBot\Request;
16
17
/**
18
 * Class ServerResponse
19
 *
20
 * @link https://core.telegram.org/bots/api#making-requests
21
 *
22
 * @method bool   getOk()          If the request was successful
23
 * @method mixed  getResult()      The result of the query
24
 * @method int    getErrorCode()   Error code of the unsuccessful request
25
 * @method string getDescription() Human-readable description of the result / unsuccessful request
26
 *
27
 * @todo method ResponseParameters getParameters()  Field which can help to automatically handle the error
28
 */
29
class ServerResponse extends Entity
30
{
31
    /**
32
     * ServerResponse constructor.
33 9
     *
34
     * @param array  $data
35
     * @param string $bot_username
36 9
     */
37 9
    public function __construct(array $data, string $bot_username = '')
38
    {
39 9
        // Make sure we don't double-save the raw_data
40 9
        unset($data['raw_data']);
41
        $data['raw_data'] = $data;
42 9
43 6
        $is_ok  = (bool) ($data['ok'] ?? false);
44 4
        $result = $data['result'] ?? null;
45
46 2
        if ($is_ok && is_array($result)) {
47
            if ($this->isAssoc($result)) {
48
                $data['result'] = $this->createResultObject($result, $bot_username);
49
            } else {
50 9
                $data['result'] = $this->createResultObjects($result, $bot_username);
51 9
            }
52
        }
53
54
        parent::__construct($data, $bot_username);
55
    }
56
57
    /**
58
     * Check if array is associative
59
     *
60
     * @link https://stackoverflow.com/a/4254008
61
     *
62 6
     * @param array $array
63
     *
64 6
     * @return bool
65
     */
66
    protected function isAssoc(array $array): bool
67
    {
68
        return count(array_filter(array_keys($array), 'is_string')) > 0;
69
    }
70
71
    /**
72 5
     * If response is ok
73
     *
74 5
     * @return bool
75
     */
76
    public function isOk(): bool
77
    {
78
        return (bool) $this->getOk();
79
    }
80
81
    /**
82
     * Print error
83
     *
84
     * @see https://secure.php.net/manual/en/function.print-r.php
85
     *
86
     * @param bool $return
87
     *
88
     * @return bool|string
89
     */
90
    public function printError($return = false)
91
    {
92
        $error = sprintf('Error N: %s, Description: %s', $this->getErrorCode(), $this->getDescription());
93
94
        if ($return) {
95
            return $error;
96
        }
97
98
        echo $error;
99
100
        return true;
101
    }
102
103
    /**
104
     * Create and return the object of the received result
105
     *
106
     * @param array  $result
107
     * @param string $bot_username
108 4
     *
109
     * @return Chat|ChatMember|File|Message|User|UserProfilePhotos|WebhookInfo
110
     */
111 4
    private function createResultObject(array $result, string $bot_username): Entity
112
    {
113
        $result_object_types = [
114 4
            'getChat'              => Chat::class,
115
            'getChatMember'        => ChatMemberFactory::class,
116
            'getFile'              => File::class,
117
            'getMe'                => User::class,
118
            'getStickerSet'        => StickerSet::class,
119
            'getUserProfilePhotos' => UserProfilePhotos::class,
120
            'getWebhookInfo'       => WebhookInfo::class,
121 4
        ];
122 4
123 2
        $action       = Request::getCurrentAction();
124
        $object_class = $result_object_types[$action] ?? Message::class;
125 4
126
        // We don't need to save the raw_data of the response object!
127
        $result['raw_data'] = null;
128
129
        return Factory::resolveEntityClass($object_class, $result, $bot_username);
130 2
    }
131
132
    /**
133
     * Create and return the objects array of the received result
134
     *
135
     * @param array  $results
136
     * @param string $bot_username
137
     *
138
     * @return BotCommand[]|ChatMember[]|GameHighScore[]|Message[]|Update[]
139
     */
140
    private function createResultObjects(array $results, string $bot_username): array
141
    {
142 2
        $result_object_types = [
143
            'getMyCommands'         => BotCommand::class,
144 2
            'getChatAdministrators' => ChatMemberFactory::class,
145 2
            'getGameHighScores'     => GameHighScore::class,
146
            'sendMediaGroup'        => Message::class,
147
        ];
148
149
        $action       = Request::getCurrentAction();
150
        $object_class = $result_object_types[$action] ?? Update::class;
151
152
        $objects = [];
153
154
        foreach ($results as $result) {
155 2
            // We don't need to save the raw_data of the response object!
156
            $result['raw_data'] = null;
157 1
158
            $objects[] = Factory::resolveEntityClass($object_class, $result, $bot_username);
159 1
        }
160
161
        return $objects;
162
    }
163
}
164