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.
Completed
Push — master ( 9f0041...1713d6 )
by Stan
02:31
created

Config::loadConfigFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 3
eloc 6
nc 2
nop 1
1
<?php
2
3
/**
4
 * Configuration class that stores all configuration values required to run bots.
5
 *
6
 * @package Teebot (Telegram bot framework)
7
 *
8
 * @author  Stanislav Drozdov <[email protected]>
9
 */
10
11
namespace Teebot;
12
13
use Teebot\Exception\Fatal;
14
use Teebot\Exception\Output;
15
use Teebot\Traits\Property;
16
17
class Config
18
{
19
    use Property;
20
21
    const DEFAULT_LIMIT             = 1;
22
23
    const DEFAULT_OFFSET            = -1;
24
25
    const REQUEST_TIMEOUT           = 6;
26
27
    const BOT_PREFIX                = 'bot';
28
29
    const COMMAND_NAMESPACE_PATTERN = 'Teebot\\Bot\\%s\\Command';
30
31
    const EVENT_NAMESPACE_PATTERN   = 'Teebot\\Bot\\%s\\EntityEvent';
32
33
    const CONFIG_FILENAME           = 'config.json';
34
35
    const BOT_DIR_PATTERN           = '%s/../Bot/%s';
36
37
    protected $botName = null;
38
39
    protected $token;
40
41
    protected $url = 'https://api.telegram.org';
42
43
    protected $timeout = 1;
44
45
    protected $method;
46
47
    protected $file_url = 'https://api.telegram.org/file/bot';
48
49
    protected $log_file = null;
50
51
    protected $events;
52
53
    protected $commandNamespace = null;
54
55
    protected $entityEventNamespace = null;
56
57
    protected $botDir = null;
58
59
    /**
60
     * Constructs configuration object with either bot name or bot config file passed.
61
     *
62
     * @param string $botName   The name of the bot to execute
63
     * @param string $botConfig Path to bot's configuration file
64
     */
65
    public function __construct(string $botName = '', $botConfig = '')
66
    {
67
        $this->initBotConfiguration($botName, $botConfig);
68
    }
69
70
    /**
71
     * Initialises bot configuration via bot name or configuration file.
72
     *
73
     * @param string $botName   The name of the bot to execute
74
     * @param string $botConfig Path to bot's configuration file
75
     *
76
     * @return bool
77
     */
78
    public function initBotConfiguration(string $botName = '', $botConfig = '')
79
    {
80
        $this->botName = $botName;
81
82
        try {
83
            if (!empty($botName)) {
84
                $this->botDir = $this->getBotDir($botName);
85
                $botConfig    = $this->botDir . static::CONFIG_FILENAME;
86
87
                $this->setNamespaces($botName);
88
            }
89
90
            if (empty($botConfig)) {
91
                Output::log(new Fatal("Path to configuration file was not sent!"));
92
            }
93
94
            $this->loadConfigFile($botConfig);
95
        } catch (Fatal $e) {
96
            Output::log($e);
97
        }
98
99
        return true;
100
    }
101
102
    /**
103
     * Returns bot directory built with default path pattern.
104
     *
105
     * @param string $botName The name of the bot to execute
106
     *
107
     * @return string
108
     */
109
    protected function getBotDir(string $botName) : string
110
    {
111
        $dir = sprintf(
112
            static::BOT_DIR_PATTERN,
113
            __DIR__,
114
            $botName
115
        );
116
117
        if (!file_exists($dir)) {
118
            Output::log(new Fatal('Bot does not exist!'));
119
        }
120
121
        return realpath($dir) . "/";
122
    }
123
124
    /**
125
     * Sets namespace for command and event entities classes to be able to load them via autoloader in the future.
126
     *
127
     * @param string $botName The name of the bot to execute
128
     */
129
    protected function setNamespaces(string $botName)
130
    {
131
        $this->commandNamespace     = sprintf(static::COMMAND_NAMESPACE_PATTERN, $botName);
132
        $this->entityEventNamespace = sprintf(static::EVENT_NAMESPACE_PATTERN, $botName);
133
    }
134
135
    /**
136
     * Loads configuration file in JSON format.
137
     *
138
     * @param string $configFile Path to configuration file
139
     */
140
    protected function loadConfigFile($configFile)
141
    {
142
        if (!is_file($configFile) || !is_readable($configFile)) {
143
            Output::log(new Fatal('File "' . $configFile . '" does not exists or not readable!'));
144
        }
145
146
        $config      = file_get_contents($configFile);
147
        $configArray = json_decode($config, true);
148
149
        $this->setProperties($configArray);
150
    }
151
152
    /**
153
     * Returns bot token string, if the value was not set in config - default value will be used
154
     * 
155
     * @return string
156
     */
157
    public function getToken()
158
    {
159
        return $this->token;
160
    }
161
162
    /**
163
     * Returns Bot-API request url
164
     * 
165
     * @return string
166
     */
167
    public function getUrl()
168
    {
169
        return $this->url;
170
    }
171
172
    /**
173
     * Returns request timeout in seconds, if the value was not set in config - default value will be used
174
     * 
175
     * @return int
176
     */
177
    public function getTimeout()
178
    {
179
        return $this->timeout;
180
    }
181
182
    /**
183
     * Returns name of the bot if it was set
184
     * 
185
     * @return null|string
186
     */
187
    public function getBotName()
188
    {
189
        return $this->botName;
190
    }
191
192
    /**
193
     * Returns request method name
194
     * 
195
     * @return string
196
     */
197
    public function getMethod()
198
    {
199
        return $this->method;
200
    }
201
202
    /**
203
     * Returns command's name space if bots are placed in default Bot directory
204
     * 
205
     * @return string
206
     */
207
    public function getCommandNamespace()
208
    {
209
        return $this->commandNamespace;
210
    }
211
212
    /**
213
     * Returns entity's name space if bots are placed in default Bot directory
214
     * 
215
     * @return string
216
     */
217
    public function getEntityEventNamespace()
218
    {
219
        return $this->entityEventNamespace;
220
    }
221
222
    /**
223
     * Returns base url from the files to download from Telegram's storage servers, if the value
224
     * was not set in config - default value will be used
225
     * 
226
     * @return string
227
     */
228
    public function getFileUrl()
229
    {
230
        return $this->file_url;
231
    }
232
233
    /**
234
     * Returns path to log file for Errors, if not set - all errors will be echoed.
235
     *
236
     * @return null|string
237
     */
238
    public function getLogFile()
239
    {
240
        return $this->log_file;
241
    }
242
243
    /**
244
     * Returns an array with defined events map, if not set default namespaces and mapping will be used.
245
     *
246
     * @return array
247
     */
248
    public function getEvents()
249
    {
250
        return $this->events;
251
    }
252
253
    /**
254
     * Returns base url for the files to download from Telegram's storage servers, token will be also added.
255
     * If the value of file url was not set in config - default value will be used
256
     *
257
     * @return null|string
258
     */
259
    public function getFileBasePath()
260
    {
261
        if (empty($this->file_url) || empty($this->token)) {
262
            return null;
263
        }
264
265
        return $this->file_url . $this->token . '/';
266
    }
267
}
268