Passed
Push — master ( 5c7744...96b75b )
by Shahrad
10:30
created
src/lib/Telegram.php 2 patches
Braces   +9 added lines, -3 removed lines patch added patch discarded remove patch
@@ -258,7 +258,9 @@  discard block
 block discarded – undo
258 258
 	public static function getUpdate(): Update|false
259 259
 	{
260 260
 		$input = self::getInput();
261
-		if (empty($input)) return false;
261
+		if (empty($input)) {
262
+		    return false;
263
+		}
262 264
 		return Telegram::processUpdate($input, self::getApiKey());
263 265
 	}
264 266
 
@@ -284,7 +286,9 @@  discard block
 block discarded – undo
284 286
 	public function fetchWith(WebhookHandler $webhook_handler, ?Update $update = null): void
285 287
 	{
286 288
 		if (is_subclass_of($webhook_handler, WebhookHandler::class)) {
287
-			if ($update === null) $update = self::getUpdate();
289
+			if ($update === null) {
290
+			    $update = self::getUpdate();
291
+			}
288 292
 			$webhook_handler->resolve($update);
289 293
 		}
290 294
 	}
@@ -297,7 +301,9 @@  discard block
 block discarded – undo
297 301
 	 */
298 302
 	protected function getTokenFromEnvFile(string $file): ?string
299 303
 	{
300
-		if (!file_exists($file)) return null;
304
+		if (!file_exists($file)) {
305
+		    return null;
306
+		}
301 307
 		return DotEnv::load($file)::get('TELEGRAM_API_KEY');
302 308
 	}
303 309
 
Please login to merge, or discard this patch.
Indentation   +318 added lines, -318 removed lines patch added patch discarded remove patch
@@ -18,323 +18,323 @@
 block discarded – undo
18 18
 class Telegram
19 19
 {
20 20
 
21
-	/**
22
-	 * @var string
23
-	 */
24
-	private string $api_key;
25
-
26
-	/**
27
-	 * @var string
28
-	 */
29
-	public static string $VERSION = 'v1.0.0';
30
-
31
-	/**
32
-	 * Telegram constructor.
33
-	 *
34
-	 * @param string $api_key
35
-	 */
36
-	public function __construct(string $api_key = '')
37
-	{
38
-		if ($api_key === '') {
39
-			$env_file = $this->getEnvFilePath();
40
-			$api_key = DotEnv::load($env_file)->get('TELEGRAM_API_KEY');
41
-		}
42
-
43
-		if (empty($api_key) || !is_string($api_key)) {
44
-			throw new TelegramException('API Key is required');
45
-		}
46
-
47
-		DotEnv::put('TG_CURRENT_KEY', ($this->api_key = $api_key));
48
-		DotEnv::put('TELEGRAM_API_KEY', ($this->api_key = $api_key));
49
-	}
50
-
51
-	/**
52
-	 * Get env file path and return it
53
-	 *
54
-	 * @return string
55
-	 */
56
-	private function getEnvFilePath(): string
57
-	{
58
-		$defaultEnvPaths = [
59
-			$_SERVER['DOCUMENT_ROOT'] . '/.env',
60
-			getcwd() . '/../.env',
61
-			getcwd() . '/.env',
62
-		];
63
-
64
-		foreach ($defaultEnvPaths as $path) {
65
-			if (file_exists($path)) {
66
-				return $path;
67
-			}
68
-		}
69
-
70
-		return '';
71
-	}
72
-
73
-	/**
74
-	 * Get API key from temporary ENV variable
75
-	 *
76
-	 * @return ?string
77
-	 */
78
-	public static function getApiKey(): ?string
79
-	{
80
-		return DotEnv::get('TG_CURRENT_KEY');
81
-	}
82
-
83
-	/**
84
-	 * Get bot info from given API key
85
-	 *
86
-	 * @return Response
87
-	 * @throws TelegramException
88
-	 */
89
-	public function getInfo(): Response
90
-	{
91
-		$result = Request::getMe();
92
-
93
-		if (!$result->isOk()) {
94
-			throw new TelegramException($result->getErrorCode() . ': ' . $result->getDescription());
95
-		}
96
-
97
-		return $result;
98
-	}
99
-
100
-	/**
101
-	 * Set Webhook for bot
102
-	 *
103
-	 * @param string $url
104
-	 * @param array $data Optional parameters.
105
-	 * @return Response
106
-	 * @throws TelegramException
107
-	 */
108
-	public function setWebhook(string $url, array $data = []): Response
109
-	{
110
-		if ($url === '') {
111
-			throw new TelegramException('Hook url is empty!');
112
-		}
113
-
114
-		if (!str_starts_with($url, 'https://')) {
115
-			throw new TelegramException('Hook url must start with https://');
116
-		}
117
-
118
-		$data = array_intersect_key($data, array_flip([
119
-			'certificate',
120
-			'ip_address',
121
-			'max_connections',
122
-			'allowed_updates',
123
-			'drop_pending_updates',
124
-		]));
125
-		$data['url'] = $url;
126
-
127
-		$result = Request::setWebhook($data);
128
-
129
-		if (!$result->isOk()) {
130
-			throw new TelegramException(
131
-				'Webhook was not set! Error: ' . $result->getErrorCode() . ' ' . $result->getDescription()
132
-			);
133
-		}
134
-
135
-		return $result;
136
-	}
137
-
138
-	/**
139
-	 * Delete any assigned webhook
140
-	 *
141
-	 * @param array $data
142
-	 * @return Response
143
-	 * @throws TelegramException
144
-	 */
145
-	public function deleteWebhook(array $data = []): Response
146
-	{
147
-		$result = Request::deleteWebhook($data);
148
-
149
-		if (!$result->isOk()) {
150
-			throw new TelegramException(
151
-				'Webhook was not deleted! Error: ' . $result->getErrorCode() . ' ' . $result->getDescription()
152
-			);
153
-		}
154
-
155
-		return $result;
156
-	}
157
-
158
-	/**
159
-	 * This method sets the admin username. and will be used to send you a message if the bot is not working.
160
-	 *
161
-	 * @param int $chat_id
162
-	 * @return void
163
-	 */
164
-	public function setAdmin(int $chat_id): void
165
-	{
166
-		defined('TG_ADMIN_ID') or define('TG_ADMIN_ID', $chat_id);
167
-	}
168
-
169
-	/**
170
-	 * Get input from stdin and return it
171
-	 *
172
-	 * @return ?string
173
-	 */
174
-	public static function getInput(): ?string
175
-	{
176
-		return file_get_contents('php://input') ?? null;
177
-	}
178
-
179
-	/**
180
-	 * This method will convert a string to an update object
181
-	 *
182
-	 * @param string $input The input string
183
-	 * @param string $apiKey The API key
184
-	 * @return Update|false
185
-	 */
186
-	public static function processUpdate(string $input, string $apiKey): Update|false
187
-	{
188
-		if (empty($input)) {
189
-			throw new TelegramException(
190
-				'Input is empty! Please check your code and try again.'
191
-			);
192
-		}
193
-
194
-		if (!self::validateToken($apiKey)) {
195
-			throw new TelegramException(
196
-				'Invalid token! Please check your code and try again.'
197
-			);
198
-		}
199
-
200
-		if (self::validateWebData($apiKey, $input)) {
201
-			if (Common::isUrlEncode($input)) {
202
-				$web_data = Common::urlDecode($input);
203
-			}
204
-
205
-			if (Common::isJson($input)) {
206
-				$web_data = json_decode($input, true);
207
-			}
208
-
209
-			$input = json_encode([
210
-				'web_data' => $web_data,
211
-			]);
212
-		}
213
-
214
-		if (!Common::isJson($input)) {
215
-			throw new TelegramException(
216
-				'Input is not a valid JSON string! Please check your code and try again.'
217
-			);
218
-		}
219
-
220
-		$input = json_decode($input, true);
221
-
222
-		return new Update($input);
223
-	}
224
-
225
-	/**
226
-	 * Validate webapp data from is from Telegram
227
-	 *
228
-	 * @link https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app
229
-	 *
230
-	 * @param string $token The bot token
231
-	 * @param string $body The message body from getInput()
232
-	 * @return bool
233
-	 */
234
-	public static function validateWebData(string $token, string $body): bool
235
-	{
236
-		if (!Common::isJson($body)) {
237
-			$raw_data = rawurldecode(str_replace('_auth=', '', $body));
238
-			$data = Common::urlDecode($raw_data);
239
-
240
-			if (empty($data['user'])) {
241
-				return false;
242
-			}
243
-
244
-			$data['user'] = urldecode($data['user']);
245
-
246
-		} else {
247
-			$data = json_decode($body, true);
248
-
249
-			if (empty($data['user'])) {
250
-				return false;
251
-			}
252
-
253
-			$data['user'] = json_encode($data['user']);
254
-		}
255
-
256
-		$data_check_string = "auth_date={$data['auth_date']}\nquery_id={$data['query_id']}\nuser={$data['user']}";
257
-		$secret_key = hash_hmac('sha256', $token, "WebAppData", true);
258
-
259
-		return hash_hmac('sha256', $data_check_string, $secret_key) == $data['hash'];
260
-	}
261
-
262
-	/**
263
-	 * Get the update from input
264
-	 *
265
-	 * @return Update|false
266
-	 */
267
-	public static function getUpdate(): Update|false
268
-	{
269
-		$input = self::getInput();
270
-		if (empty($input)) return false;
271
-		return Telegram::processUpdate($input, self::getApiKey());
272
-	}
273
-
274
-	/**
275
-	 * Validate the token
276
-	 *
277
-	 * @param string $token (e.g. 123456789:ABC-DEF1234ghIkl-zyx57W2v1u123ew11) {digit}:{alphanumeric[34]}
278
-	 * @return bool
279
-	 */
280
-	public static function validateToken(string $token): bool
281
-	{
282
-		preg_match_all('/([0-9]+:[a-zA-Z0-9-_]+)/', $token, $matches);
283
-		return count($matches[0]) == 1;
284
-	}
285
-
286
-	/**
287
-	 * Pass the update to the given webhook handler
288
-	 *
289
-	 * @param WebhookHandler $webhook_handler The webhook handler
290
-	 * @param ?Update $update By default, it will get the update from input
291
-	 * @return void
292
-	 */
293
-	public function fetchWith(WebhookHandler $webhook_handler, ?Update $update = null): void
294
-	{
295
-		if (is_subclass_of($webhook_handler, WebhookHandler::class)) {
296
-			if ($update === null) $update = self::getUpdate();
297
-			$webhook_handler->resolve($update);
298
-		}
299
-	}
300
-
301
-	/**
302
-	 * Get token from env file.
303
-	 *
304
-	 * @param string $file
305
-	 * @return ?string
306
-	 */
307
-	protected function getTokenFromEnvFile(string $file): ?string
308
-	{
309
-		if (!file_exists($file)) return null;
310
-		return DotEnv::load($file)::get('TELEGRAM_API_KEY');
311
-	}
312
-
313
-	/**
314
-	 * Debug mode
315
-	 *
316
-	 * @param ?int $admin_id Fill this or use setAdmin()
317
-	 * @return void
318
-	 */
319
-	public static function setDebugMode(?int $admin_id = null): void
320
-	{
321
-		error_reporting(E_ALL);
322
-		ini_set('display_errors', 1);
323
-		defined('DEBUG_MODE') or define('DEBUG_MODE', true);
324
-		if ($admin_id) {
325
-			defined('TG_ADMIN_ID') or define('TG_ADMIN_ID', $admin_id);
326
-		}
327
-	}
328
-
329
-	/**
330
-	 * Just another echo
331
-	 *
332
-	 * @param string $text
333
-	 * @return void
334
-	 */
335
-	public static function echo(string $text): void
336
-	{
337
-		echo $text;
338
-	}
21
+    /**
22
+     * @var string
23
+     */
24
+    private string $api_key;
25
+
26
+    /**
27
+     * @var string
28
+     */
29
+    public static string $VERSION = 'v1.0.0';
30
+
31
+    /**
32
+     * Telegram constructor.
33
+     *
34
+     * @param string $api_key
35
+     */
36
+    public function __construct(string $api_key = '')
37
+    {
38
+        if ($api_key === '') {
39
+            $env_file = $this->getEnvFilePath();
40
+            $api_key = DotEnv::load($env_file)->get('TELEGRAM_API_KEY');
41
+        }
42
+
43
+        if (empty($api_key) || !is_string($api_key)) {
44
+            throw new TelegramException('API Key is required');
45
+        }
46
+
47
+        DotEnv::put('TG_CURRENT_KEY', ($this->api_key = $api_key));
48
+        DotEnv::put('TELEGRAM_API_KEY', ($this->api_key = $api_key));
49
+    }
50
+
51
+    /**
52
+     * Get env file path and return it
53
+     *
54
+     * @return string
55
+     */
56
+    private function getEnvFilePath(): string
57
+    {
58
+        $defaultEnvPaths = [
59
+            $_SERVER['DOCUMENT_ROOT'] . '/.env',
60
+            getcwd() . '/../.env',
61
+            getcwd() . '/.env',
62
+        ];
63
+
64
+        foreach ($defaultEnvPaths as $path) {
65
+            if (file_exists($path)) {
66
+                return $path;
67
+            }
68
+        }
69
+
70
+        return '';
71
+    }
72
+
73
+    /**
74
+     * Get API key from temporary ENV variable
75
+     *
76
+     * @return ?string
77
+     */
78
+    public static function getApiKey(): ?string
79
+    {
80
+        return DotEnv::get('TG_CURRENT_KEY');
81
+    }
82
+
83
+    /**
84
+     * Get bot info from given API key
85
+     *
86
+     * @return Response
87
+     * @throws TelegramException
88
+     */
89
+    public function getInfo(): Response
90
+    {
91
+        $result = Request::getMe();
92
+
93
+        if (!$result->isOk()) {
94
+            throw new TelegramException($result->getErrorCode() . ': ' . $result->getDescription());
95
+        }
96
+
97
+        return $result;
98
+    }
99
+
100
+    /**
101
+     * Set Webhook for bot
102
+     *
103
+     * @param string $url
104
+     * @param array $data Optional parameters.
105
+     * @return Response
106
+     * @throws TelegramException
107
+     */
108
+    public function setWebhook(string $url, array $data = []): Response
109
+    {
110
+        if ($url === '') {
111
+            throw new TelegramException('Hook url is empty!');
112
+        }
113
+
114
+        if (!str_starts_with($url, 'https://')) {
115
+            throw new TelegramException('Hook url must start with https://');
116
+        }
117
+
118
+        $data = array_intersect_key($data, array_flip([
119
+            'certificate',
120
+            'ip_address',
121
+            'max_connections',
122
+            'allowed_updates',
123
+            'drop_pending_updates',
124
+        ]));
125
+        $data['url'] = $url;
126
+
127
+        $result = Request::setWebhook($data);
128
+
129
+        if (!$result->isOk()) {
130
+            throw new TelegramException(
131
+                'Webhook was not set! Error: ' . $result->getErrorCode() . ' ' . $result->getDescription()
132
+            );
133
+        }
134
+
135
+        return $result;
136
+    }
137
+
138
+    /**
139
+     * Delete any assigned webhook
140
+     *
141
+     * @param array $data
142
+     * @return Response
143
+     * @throws TelegramException
144
+     */
145
+    public function deleteWebhook(array $data = []): Response
146
+    {
147
+        $result = Request::deleteWebhook($data);
148
+
149
+        if (!$result->isOk()) {
150
+            throw new TelegramException(
151
+                'Webhook was not deleted! Error: ' . $result->getErrorCode() . ' ' . $result->getDescription()
152
+            );
153
+        }
154
+
155
+        return $result;
156
+    }
157
+
158
+    /**
159
+     * This method sets the admin username. and will be used to send you a message if the bot is not working.
160
+     *
161
+     * @param int $chat_id
162
+     * @return void
163
+     */
164
+    public function setAdmin(int $chat_id): void
165
+    {
166
+        defined('TG_ADMIN_ID') or define('TG_ADMIN_ID', $chat_id);
167
+    }
168
+
169
+    /**
170
+     * Get input from stdin and return it
171
+     *
172
+     * @return ?string
173
+     */
174
+    public static function getInput(): ?string
175
+    {
176
+        return file_get_contents('php://input') ?? null;
177
+    }
178
+
179
+    /**
180
+     * This method will convert a string to an update object
181
+     *
182
+     * @param string $input The input string
183
+     * @param string $apiKey The API key
184
+     * @return Update|false
185
+     */
186
+    public static function processUpdate(string $input, string $apiKey): Update|false
187
+    {
188
+        if (empty($input)) {
189
+            throw new TelegramException(
190
+                'Input is empty! Please check your code and try again.'
191
+            );
192
+        }
193
+
194
+        if (!self::validateToken($apiKey)) {
195
+            throw new TelegramException(
196
+                'Invalid token! Please check your code and try again.'
197
+            );
198
+        }
199
+
200
+        if (self::validateWebData($apiKey, $input)) {
201
+            if (Common::isUrlEncode($input)) {
202
+                $web_data = Common::urlDecode($input);
203
+            }
204
+
205
+            if (Common::isJson($input)) {
206
+                $web_data = json_decode($input, true);
207
+            }
208
+
209
+            $input = json_encode([
210
+                'web_data' => $web_data,
211
+            ]);
212
+        }
213
+
214
+        if (!Common::isJson($input)) {
215
+            throw new TelegramException(
216
+                'Input is not a valid JSON string! Please check your code and try again.'
217
+            );
218
+        }
219
+
220
+        $input = json_decode($input, true);
221
+
222
+        return new Update($input);
223
+    }
224
+
225
+    /**
226
+     * Validate webapp data from is from Telegram
227
+     *
228
+     * @link https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app
229
+     *
230
+     * @param string $token The bot token
231
+     * @param string $body The message body from getInput()
232
+     * @return bool
233
+     */
234
+    public static function validateWebData(string $token, string $body): bool
235
+    {
236
+        if (!Common::isJson($body)) {
237
+            $raw_data = rawurldecode(str_replace('_auth=', '', $body));
238
+            $data = Common::urlDecode($raw_data);
239
+
240
+            if (empty($data['user'])) {
241
+                return false;
242
+            }
243
+
244
+            $data['user'] = urldecode($data['user']);
245
+
246
+        } else {
247
+            $data = json_decode($body, true);
248
+
249
+            if (empty($data['user'])) {
250
+                return false;
251
+            }
252
+
253
+            $data['user'] = json_encode($data['user']);
254
+        }
255
+
256
+        $data_check_string = "auth_date={$data['auth_date']}\nquery_id={$data['query_id']}\nuser={$data['user']}";
257
+        $secret_key = hash_hmac('sha256', $token, "WebAppData", true);
258
+
259
+        return hash_hmac('sha256', $data_check_string, $secret_key) == $data['hash'];
260
+    }
261
+
262
+    /**
263
+     * Get the update from input
264
+     *
265
+     * @return Update|false
266
+     */
267
+    public static function getUpdate(): Update|false
268
+    {
269
+        $input = self::getInput();
270
+        if (empty($input)) return false;
271
+        return Telegram::processUpdate($input, self::getApiKey());
272
+    }
273
+
274
+    /**
275
+     * Validate the token
276
+     *
277
+     * @param string $token (e.g. 123456789:ABC-DEF1234ghIkl-zyx57W2v1u123ew11) {digit}:{alphanumeric[34]}
278
+     * @return bool
279
+     */
280
+    public static function validateToken(string $token): bool
281
+    {
282
+        preg_match_all('/([0-9]+:[a-zA-Z0-9-_]+)/', $token, $matches);
283
+        return count($matches[0]) == 1;
284
+    }
285
+
286
+    /**
287
+     * Pass the update to the given webhook handler
288
+     *
289
+     * @param WebhookHandler $webhook_handler The webhook handler
290
+     * @param ?Update $update By default, it will get the update from input
291
+     * @return void
292
+     */
293
+    public function fetchWith(WebhookHandler $webhook_handler, ?Update $update = null): void
294
+    {
295
+        if (is_subclass_of($webhook_handler, WebhookHandler::class)) {
296
+            if ($update === null) $update = self::getUpdate();
297
+            $webhook_handler->resolve($update);
298
+        }
299
+    }
300
+
301
+    /**
302
+     * Get token from env file.
303
+     *
304
+     * @param string $file
305
+     * @return ?string
306
+     */
307
+    protected function getTokenFromEnvFile(string $file): ?string
308
+    {
309
+        if (!file_exists($file)) return null;
310
+        return DotEnv::load($file)::get('TELEGRAM_API_KEY');
311
+    }
312
+
313
+    /**
314
+     * Debug mode
315
+     *
316
+     * @param ?int $admin_id Fill this or use setAdmin()
317
+     * @return void
318
+     */
319
+    public static function setDebugMode(?int $admin_id = null): void
320
+    {
321
+        error_reporting(E_ALL);
322
+        ini_set('display_errors', 1);
323
+        defined('DEBUG_MODE') or define('DEBUG_MODE', true);
324
+        if ($admin_id) {
325
+            defined('TG_ADMIN_ID') or define('TG_ADMIN_ID', $admin_id);
326
+        }
327
+    }
328
+
329
+    /**
330
+     * Just another echo
331
+     *
332
+     * @param string $text
333
+     * @return void
334
+     */
335
+    public static function echo(string $text): void
336
+    {
337
+        echo $text;
338
+    }
339 339
 
340 340
 }
341 341
\ No newline at end of file
Please login to merge, or discard this patch.
src/lib/TelegramLog.php 2 patches
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -25,142 +25,142 @@
 block discarded – undo
25 25
 class TelegramLog
26 26
 {
27 27
 
28
-	/**
29
-	 * Logger instance
30
-	 *
31
-	 * @var LoggerInterface
32
-	 */
33
-	protected static LoggerInterface $logger;
34
-
35
-	/**
36
-	 * Logger instance for update
37
-	 *
38
-	 * @var LoggerInterface
39
-	 */
40
-	protected static LoggerInterface $update_logger;
41
-
42
-	/**
43
-	 * Always log the request and response data to the debug log, also for successful requests
44
-	 *
45
-	 * @var bool
46
-	 */
47
-	public static bool $always_log_request_and_response = false;
48
-
49
-	/**
50
-	 * Temporary stream handle for debug log
51
-	 *
52
-	 * @var resource|null
53
-	 */
54
-	protected static $debug_log_temp_stream_handle;
55
-
56
-	/**
57
-	 * Remove bot token from debug stream
58
-	 *
59
-	 * @var bool
60
-	 */
61
-	public static bool $remove_bot_token = true;
62
-
63
-	/**
64
-	 * Initialise logging.
65
-	 *
66
-	 * @param LoggerInterface|null $logger
67
-	 * @param LoggerInterface|null $update_logger
68
-	 */
69
-	public static function initialize(LoggerInterface $logger = null, LoggerInterface $update_logger = null): void
70
-	{
71
-		self::$logger = $logger ?: new NullLogger();
72
-		self::$update_logger = $update_logger ?: new NullLogger();
73
-	}
74
-
75
-	/**
76
-	 * Get the stream handle of the temporary debug output
77
-	 *
78
-	 * @return mixed The stream if debug is active, else false
79
-	 */
80
-	public static function getDebugLogTempStream(): mixed
81
-	{
82
-		if ((self::$debug_log_temp_stream_handle === null) && $temp_stream_handle = fopen('php://temp', 'wb+')) {
83
-			self::$debug_log_temp_stream_handle = $temp_stream_handle;
84
-		}
85
-
86
-		return self::$debug_log_temp_stream_handle;
87
-	}
88
-
89
-	/**
90
-	 * Write the temporary debug stream to log and close the stream handle
91
-	 *
92
-	 * @param string $message Message (with placeholder) to write to the debug log
93
-	 */
94
-	public static function endDebugLogTempStream(string $message = '%s'): void
95
-	{
96
-		if (is_resource(self::$debug_log_temp_stream_handle)) {
97
-			rewind(self::$debug_log_temp_stream_handle);
98
-			$stream_contents = stream_get_contents(self::$debug_log_temp_stream_handle);
99
-
100
-			if (self::$remove_bot_token) {
101
-				$stream_contents = preg_replace('/\/bot(\d+):[\w\-]+\//', '/botBOT_TOKEN_REMOVED/', $stream_contents);
102
-			}
103
-
104
-			self::debug(sprintf($message, $stream_contents));
105
-			fclose(self::$debug_log_temp_stream_handle);
106
-			self::$debug_log_temp_stream_handle = null;
107
-		}
108
-	}
109
-
110
-	/**
111
-	 * Handle any logging method call.
112
-	 *
113
-	 * @param string $name
114
-	 * @param array $arguments
115
-	 */
116
-	public static function __callStatic(string $name, array $arguments)
117
-	{
118
-		// Get the correct logger instance.
119
-		$logger = null;
120
-		self::initialize();
121
-		if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug',], true)) {
122
-			$logger = self::$logger;
123
-		} elseif ($name === 'update') {
124
-			$logger = self::$update_logger;
125
-			$name = 'info';
126
-		}
127
-
128
-		// Clearly we have no logging enabled.
129
-		if ($logger === null) {
130
-			return;
131
-		}
132
-
133
-		// Replace any placeholders from the passed context.
134
-		if (count($arguments) >= 2) {
135
-			$arguments[0] = self::interpolate($arguments[0], $arguments[1]);
136
-		}
137
-
138
-		call_user_func_array([$logger, $name], $arguments);
139
-	}
140
-
141
-	/**
142
-	 * Interpolates context values into the message placeholders.
143
-	 *
144
-	 * @see https://www.php-fig.org/psr/psr-3/#12-message
145
-	 *
146
-	 * @param string $message
147
-	 * @param array $context
148
-	 *
149
-	 * @return string
150
-	 */
151
-	protected static function interpolate(string $message, array $context = []): string
152
-	{
153
-		// Build a replacement array with braces around the context keys.
154
-		$replace = [];
155
-		foreach ($context as $key => $val) {
156
-			// check that the value can be cast to string
157
-			if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
158
-				$replace["{{$key}}"] = $val;
159
-			}
160
-		}
161
-
162
-		// Interpolate replacement values into the message and return.
163
-		return strtr($message, $replace);
164
-	}
28
+    /**
29
+     * Logger instance
30
+     *
31
+     * @var LoggerInterface
32
+     */
33
+    protected static LoggerInterface $logger;
34
+
35
+    /**
36
+     * Logger instance for update
37
+     *
38
+     * @var LoggerInterface
39
+     */
40
+    protected static LoggerInterface $update_logger;
41
+
42
+    /**
43
+     * Always log the request and response data to the debug log, also for successful requests
44
+     *
45
+     * @var bool
46
+     */
47
+    public static bool $always_log_request_and_response = false;
48
+
49
+    /**
50
+     * Temporary stream handle for debug log
51
+     *
52
+     * @var resource|null
53
+     */
54
+    protected static $debug_log_temp_stream_handle;
55
+
56
+    /**
57
+     * Remove bot token from debug stream
58
+     *
59
+     * @var bool
60
+     */
61
+    public static bool $remove_bot_token = true;
62
+
63
+    /**
64
+     * Initialise logging.
65
+     *
66
+     * @param LoggerInterface|null $logger
67
+     * @param LoggerInterface|null $update_logger
68
+     */
69
+    public static function initialize(LoggerInterface $logger = null, LoggerInterface $update_logger = null): void
70
+    {
71
+        self::$logger = $logger ?: new NullLogger();
72
+        self::$update_logger = $update_logger ?: new NullLogger();
73
+    }
74
+
75
+    /**
76
+     * Get the stream handle of the temporary debug output
77
+     *
78
+     * @return mixed The stream if debug is active, else false
79
+     */
80
+    public static function getDebugLogTempStream(): mixed
81
+    {
82
+        if ((self::$debug_log_temp_stream_handle === null) && $temp_stream_handle = fopen('php://temp', 'wb+')) {
83
+            self::$debug_log_temp_stream_handle = $temp_stream_handle;
84
+        }
85
+
86
+        return self::$debug_log_temp_stream_handle;
87
+    }
88
+
89
+    /**
90
+     * Write the temporary debug stream to log and close the stream handle
91
+     *
92
+     * @param string $message Message (with placeholder) to write to the debug log
93
+     */
94
+    public static function endDebugLogTempStream(string $message = '%s'): void
95
+    {
96
+        if (is_resource(self::$debug_log_temp_stream_handle)) {
97
+            rewind(self::$debug_log_temp_stream_handle);
98
+            $stream_contents = stream_get_contents(self::$debug_log_temp_stream_handle);
99
+
100
+            if (self::$remove_bot_token) {
101
+                $stream_contents = preg_replace('/\/bot(\d+):[\w\-]+\//', '/botBOT_TOKEN_REMOVED/', $stream_contents);
102
+            }
103
+
104
+            self::debug(sprintf($message, $stream_contents));
105
+            fclose(self::$debug_log_temp_stream_handle);
106
+            self::$debug_log_temp_stream_handle = null;
107
+        }
108
+    }
109
+
110
+    /**
111
+     * Handle any logging method call.
112
+     *
113
+     * @param string $name
114
+     * @param array $arguments
115
+     */
116
+    public static function __callStatic(string $name, array $arguments)
117
+    {
118
+        // Get the correct logger instance.
119
+        $logger = null;
120
+        self::initialize();
121
+        if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug',], true)) {
122
+            $logger = self::$logger;
123
+        } elseif ($name === 'update') {
124
+            $logger = self::$update_logger;
125
+            $name = 'info';
126
+        }
127
+
128
+        // Clearly we have no logging enabled.
129
+        if ($logger === null) {
130
+            return;
131
+        }
132
+
133
+        // Replace any placeholders from the passed context.
134
+        if (count($arguments) >= 2) {
135
+            $arguments[0] = self::interpolate($arguments[0], $arguments[1]);
136
+        }
137
+
138
+        call_user_func_array([$logger, $name], $arguments);
139
+    }
140
+
141
+    /**
142
+     * Interpolates context values into the message placeholders.
143
+     *
144
+     * @see https://www.php-fig.org/psr/psr-3/#12-message
145
+     *
146
+     * @param string $message
147
+     * @param array $context
148
+     *
149
+     * @return string
150
+     */
151
+    protected static function interpolate(string $message, array $context = []): string
152
+    {
153
+        // Build a replacement array with braces around the context keys.
154
+        $replace = [];
155
+        foreach ($context as $key => $val) {
156
+            // check that the value can be cast to string
157
+            if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
158
+                $replace["{{$key}}"] = $val;
159
+            }
160
+        }
161
+
162
+        // Interpolate replacement values into the message and return.
163
+        return strtr($message, $replace);
164
+    }
165 165
 
166 166
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -118,7 +118,7 @@
 block discarded – undo
118 118
 		// Get the correct logger instance.
119 119
 		$logger = null;
120 120
 		self::initialize();
121
-		if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug',], true)) {
121
+		if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug', ], true)) {
122 122
 			$logger = self::$logger;
123 123
 		} elseif ($name === 'update') {
124 124
 			$logger = self::$update_logger;
Please login to merge, or discard this patch.
src/lib/Factory.php 1 patch
Indentation   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -12,26 +12,26 @@
 block discarded – undo
12 12
 abstract class Factory
13 13
 {
14 14
 
15
-	/**
16
-	 * @param array $data
17
-	 * @return Entity
18
-	 */
19
-	abstract public static function make(array $data): Entity;
15
+    /**
16
+     * @param array $data
17
+     * @return Entity
18
+     */
19
+    abstract public static function make(array $data): Entity;
20 20
 
21
-	/**
22
-	 * This method is used to create a new instance of the entity.
23
-	 *
24
-	 * @param string $class
25
-	 * @param mixed $property
26
-	 * @return Entity
27
-	 */
28
-	public static function resolveEntityClass(string $class, mixed $property): Entity
29
-	{
30
-		if (is_subclass_of($class, Factory::class)) {
31
-			return $class::make($property);
32
-		}
21
+    /**
22
+     * This method is used to create a new instance of the entity.
23
+     *
24
+     * @param string $class
25
+     * @param mixed $property
26
+     * @return Entity
27
+     */
28
+    public static function resolveEntityClass(string $class, mixed $property): Entity
29
+    {
30
+        if (is_subclass_of($class, Factory::class)) {
31
+            return $class::make($property);
32
+        }
33 33
 
34
-		return new $class($property);
35
-	}
34
+        return new $class($property);
35
+    }
36 36
 
37 37
 }
Please login to merge, or discard this patch.
src/lib/Request.php 1 patch
Indentation   +222 added lines, -222 removed lines patch added patch discarded remove patch
@@ -143,227 +143,227 @@
 block discarded – undo
143 143
 class Request
144 144
 {
145 145
 
146
-	/**
147
-	 * Available fields for InputFile helper
148
-	 *
149
-	 * This is basically the list of all fields that allow InputFile objects
150
-	 * for which input can be simplified by providing local path directly  as string.
151
-	 *
152
-	 * @var array
153
-	 */
154
-	private static array $input_file_fields = [
155
-		'setWebhook' => ['certificate'],
156
-		'sendPhoto' => ['photo'],
157
-		'sendAudio' => ['audio', 'thumb'],
158
-		'sendDocument' => ['document', 'thumb'],
159
-		'sendVideo' => ['video', 'thumb'],
160
-		'sendAnimation' => ['animation', 'thumb'],
161
-		'sendVoice' => ['voice', 'thumb'],
162
-		'sendVideoNote' => ['video_note', 'thumb'],
163
-		'setChatPhoto' => ['photo'],
164
-		'sendSticker' => ['sticker'],
165
-		'uploadStickerFile' => ['png_sticker'],
166
-		'createNewStickerSet' => ['png_sticker', 'tgs_sticker', 'webm_sticker'],
167
-		'addStickerToSet' => ['png_sticker', 'tgs_sticker', 'webm_sticker'],
168
-		'setStickerSetThumb' => ['thumb'],
169
-	];
170
-
171
-	/**
172
-	 * URI of the Telegram API
173
-	 *
174
-	 * @var string
175
-	 */
176
-	private static string $api_base_uri = 'https://api.telegram.org';
177
-
178
-	/**
179
-	 * URI of the Telegram API for downloading files (relative to $api_base_url or absolute)
180
-	 *
181
-	 * @var string
182
-	 */
183
-	private static string $api_base_download_uri = '/file/bot{API_KEY}';
184
-
185
-	/**
186
-	 * The current action that is being executed
187
-	 *
188
-	 * @var string
189
-	 */
190
-	private static string $current_action = '';
191
-
192
-	/**
193
-	 * Get the Telegram API path
194
-	 *
195
-	 * @return string
196
-	 */
197
-	public static function getApiPath(): string
198
-	{
199
-		return self::$api_base_uri . '/bot' . Telegram::getApiKey() . '/';
200
-	}
201
-
202
-	/**
203
-	 * Initialize a http client
204
-	 *
205
-	 * @return Client
206
-	 */
207
-	private static function getClient(): Client
208
-	{
209
-		return new Client();
210
-	}
211
-
212
-	/**
213
-	 * Use this method to get stats of given user in a supergroup or channel.
214
-	 *
215
-	 * @param int $user_id User identifier
216
-	 * @param int $chat_id Identifier of the chat to get stats for
217
-	 *
218
-	 * @return string [left, member, administrator, creator]
219
-	 */
220
-	public static function getChatMemberStatus(int $user_id, int $chat_id): string
221
-	{
222
-		$response = self::getChatMember([
223
-			'user_id' => $user_id,
224
-			'chat_id' => $chat_id,
225
-		]);
226
-
227
-		return $response->getResult()->status ?? "left";
228
-	}
229
-
230
-	/**
231
-	 * Properly set up the request params
232
-	 *
233
-	 * If any item of the array is a resource, reformat it to a multipart request.
234
-	 * Else, just return the passed data as form params.
235
-	 *
236
-	 * @param array $data
237
-	 * @return array
238
-	 */
239
-	private static function setUpRequestParams(array $data): array
240
-	{
241
-		$multipart = [];
242
-		$has_resource = false;
243
-
244
-		$options = [
245
-			'header' => [
246
-				'Content-Type' => 'application/json',
247
-				'Accept' => 'application/json',
248
-				'User-Agent' => 'TelegramBot-PHP/' . Telegram::$VERSION
249
-			]
250
-		];
251
-
252
-		foreach ($data as $key => &$item) {
253
-			if (array_key_exists(self::$current_action, self::$input_file_fields) && in_array($key, self::$input_file_fields[self::$current_action], true)) {
254
-
255
-				if (is_string($item) && file_exists($item)) {
256
-					$has_resource = true;
257
-
258
-				} elseif (filter_var($item, FILTER_VALIDATE_URL)) {
259
-					$has_resource = false;
260
-					continue;
261
-
262
-				} else {
263
-					throw new TelegramException(
264
-						'Invalid file path or URL: ' . $item . ' for ' . self::$current_action . ' action'
265
-					);
266
-				}
267
-
268
-				$multipart[$key] = $item;
269
-				continue;
270
-			}
271
-
272
-			if ($item instanceof Entity) {
273
-				$item = $item->getRawData();
274
-			}
275
-
276
-			if (is_array($item)) {
277
-				$item = json_encode($item);
278
-			}
279
-
280
-			$options['query'][$key] = $item;
281
-		}
282
-		unset($item);
283
-
284
-		if ($has_resource) {
285
-			$options['multipart'] = FormData::create($multipart);
286
-		}
287
-
288
-		return $options;
289
-	}
290
-
291
-	/**
292
-	 * Create a Http Request
293
-	 *
294
-	 * @param string $action Action to execute
295
-	 * @param array $data Data to attach to the execution
296
-	 *
297
-	 * @return array An array of the setUpRequestParams and the url
298
-	 */
299
-	public static function create(string $action, array $data): array
300
-	{
301
-		return [
302
-			'url' => self::getApiPath() . $action,
303
-			'options' => self::setUpRequestParams($data)
304
-		];
305
-	}
306
-
307
-	/**
308
-	 * Execute HTTP Request
309
-	 *
310
-	 * @param string $action Action to execute
311
-	 * @param array $data Data to attach to the execution
312
-	 *
313
-	 * @return string Result of the HTTP Request
314
-	 */
315
-	private static function execute(string $action, array $data): string
316
-	{
317
-		$request = self::create($action, $data);
318
-
319
-		$response = self::getClient()->get($request['url'], $request['options']);
320
-
321
-		return $response->getBody();
322
-	}
323
-
324
-	/**
325
-	 * Send command
326
-	 *
327
-	 * @param string $action
328
-	 * @param array $data
329
-	 *
330
-	 * @return Response
331
-	 * @throws TelegramException
332
-	 */
333
-	public static function send(string $action, array $data = []): Response
334
-	{
335
-		self::$current_action = $action;
336
-
337
-		$raw_response = self::execute($action, $data);
338
-
339
-		if (!Common::isJson($raw_response)) {
340
-			TelegramLog::debug($raw_response);
341
-			throw new TelegramException('Invalid response from API');
342
-		}
343
-
344
-		$response = new Response(json_decode($raw_response, true));
345
-
346
-		if (!$response->isOk() && $response->getErrorCode() === 401 && $response->getDescription() === 'Unauthorized') {
347
-			throw new InvalidBotTokenException();
348
-		}
349
-
350
-		self::$current_action = '';
351
-
352
-		return $response;
353
-	}
354
-
355
-	/**
356
-	 * Any statically called method should be relayed to the `send` method.
357
-	 *
358
-	 * @param string $action
359
-	 * @param array $data
360
-	 *
361
-	 * @return Response
362
-	 * @throws TelegramException
363
-	 */
364
-	public static function __callStatic(string $action, array $data): Response
365
-	{
366
-		return static::send($action, reset($data) ?: []);
367
-	}
146
+    /**
147
+     * Available fields for InputFile helper
148
+     *
149
+     * This is basically the list of all fields that allow InputFile objects
150
+     * for which input can be simplified by providing local path directly  as string.
151
+     *
152
+     * @var array
153
+     */
154
+    private static array $input_file_fields = [
155
+        'setWebhook' => ['certificate'],
156
+        'sendPhoto' => ['photo'],
157
+        'sendAudio' => ['audio', 'thumb'],
158
+        'sendDocument' => ['document', 'thumb'],
159
+        'sendVideo' => ['video', 'thumb'],
160
+        'sendAnimation' => ['animation', 'thumb'],
161
+        'sendVoice' => ['voice', 'thumb'],
162
+        'sendVideoNote' => ['video_note', 'thumb'],
163
+        'setChatPhoto' => ['photo'],
164
+        'sendSticker' => ['sticker'],
165
+        'uploadStickerFile' => ['png_sticker'],
166
+        'createNewStickerSet' => ['png_sticker', 'tgs_sticker', 'webm_sticker'],
167
+        'addStickerToSet' => ['png_sticker', 'tgs_sticker', 'webm_sticker'],
168
+        'setStickerSetThumb' => ['thumb'],
169
+    ];
170
+
171
+    /**
172
+     * URI of the Telegram API
173
+     *
174
+     * @var string
175
+     */
176
+    private static string $api_base_uri = 'https://api.telegram.org';
177
+
178
+    /**
179
+     * URI of the Telegram API for downloading files (relative to $api_base_url or absolute)
180
+     *
181
+     * @var string
182
+     */
183
+    private static string $api_base_download_uri = '/file/bot{API_KEY}';
184
+
185
+    /**
186
+     * The current action that is being executed
187
+     *
188
+     * @var string
189
+     */
190
+    private static string $current_action = '';
191
+
192
+    /**
193
+     * Get the Telegram API path
194
+     *
195
+     * @return string
196
+     */
197
+    public static function getApiPath(): string
198
+    {
199
+        return self::$api_base_uri . '/bot' . Telegram::getApiKey() . '/';
200
+    }
201
+
202
+    /**
203
+     * Initialize a http client
204
+     *
205
+     * @return Client
206
+     */
207
+    private static function getClient(): Client
208
+    {
209
+        return new Client();
210
+    }
211
+
212
+    /**
213
+     * Use this method to get stats of given user in a supergroup or channel.
214
+     *
215
+     * @param int $user_id User identifier
216
+     * @param int $chat_id Identifier of the chat to get stats for
217
+     *
218
+     * @return string [left, member, administrator, creator]
219
+     */
220
+    public static function getChatMemberStatus(int $user_id, int $chat_id): string
221
+    {
222
+        $response = self::getChatMember([
223
+            'user_id' => $user_id,
224
+            'chat_id' => $chat_id,
225
+        ]);
226
+
227
+        return $response->getResult()->status ?? "left";
228
+    }
229
+
230
+    /**
231
+     * Properly set up the request params
232
+     *
233
+     * If any item of the array is a resource, reformat it to a multipart request.
234
+     * Else, just return the passed data as form params.
235
+     *
236
+     * @param array $data
237
+     * @return array
238
+     */
239
+    private static function setUpRequestParams(array $data): array
240
+    {
241
+        $multipart = [];
242
+        $has_resource = false;
243
+
244
+        $options = [
245
+            'header' => [
246
+                'Content-Type' => 'application/json',
247
+                'Accept' => 'application/json',
248
+                'User-Agent' => 'TelegramBot-PHP/' . Telegram::$VERSION
249
+            ]
250
+        ];
251
+
252
+        foreach ($data as $key => &$item) {
253
+            if (array_key_exists(self::$current_action, self::$input_file_fields) && in_array($key, self::$input_file_fields[self::$current_action], true)) {
254
+
255
+                if (is_string($item) && file_exists($item)) {
256
+                    $has_resource = true;
257
+
258
+                } elseif (filter_var($item, FILTER_VALIDATE_URL)) {
259
+                    $has_resource = false;
260
+                    continue;
261
+
262
+                } else {
263
+                    throw new TelegramException(
264
+                        'Invalid file path or URL: ' . $item . ' for ' . self::$current_action . ' action'
265
+                    );
266
+                }
267
+
268
+                $multipart[$key] = $item;
269
+                continue;
270
+            }
271
+
272
+            if ($item instanceof Entity) {
273
+                $item = $item->getRawData();
274
+            }
275
+
276
+            if (is_array($item)) {
277
+                $item = json_encode($item);
278
+            }
279
+
280
+            $options['query'][$key] = $item;
281
+        }
282
+        unset($item);
283
+
284
+        if ($has_resource) {
285
+            $options['multipart'] = FormData::create($multipart);
286
+        }
287
+
288
+        return $options;
289
+    }
290
+
291
+    /**
292
+     * Create a Http Request
293
+     *
294
+     * @param string $action Action to execute
295
+     * @param array $data Data to attach to the execution
296
+     *
297
+     * @return array An array of the setUpRequestParams and the url
298
+     */
299
+    public static function create(string $action, array $data): array
300
+    {
301
+        return [
302
+            'url' => self::getApiPath() . $action,
303
+            'options' => self::setUpRequestParams($data)
304
+        ];
305
+    }
306
+
307
+    /**
308
+     * Execute HTTP Request
309
+     *
310
+     * @param string $action Action to execute
311
+     * @param array $data Data to attach to the execution
312
+     *
313
+     * @return string Result of the HTTP Request
314
+     */
315
+    private static function execute(string $action, array $data): string
316
+    {
317
+        $request = self::create($action, $data);
318
+
319
+        $response = self::getClient()->get($request['url'], $request['options']);
320
+
321
+        return $response->getBody();
322
+    }
323
+
324
+    /**
325
+     * Send command
326
+     *
327
+     * @param string $action
328
+     * @param array $data
329
+     *
330
+     * @return Response
331
+     * @throws TelegramException
332
+     */
333
+    public static function send(string $action, array $data = []): Response
334
+    {
335
+        self::$current_action = $action;
336
+
337
+        $raw_response = self::execute($action, $data);
338
+
339
+        if (!Common::isJson($raw_response)) {
340
+            TelegramLog::debug($raw_response);
341
+            throw new TelegramException('Invalid response from API');
342
+        }
343
+
344
+        $response = new Response(json_decode($raw_response, true));
345
+
346
+        if (!$response->isOk() && $response->getErrorCode() === 401 && $response->getDescription() === 'Unauthorized') {
347
+            throw new InvalidBotTokenException();
348
+        }
349
+
350
+        self::$current_action = '';
351
+
352
+        return $response;
353
+    }
354
+
355
+    /**
356
+     * Any statically called method should be relayed to the `send` method.
357
+     *
358
+     * @param string $action
359
+     * @param array $data
360
+     *
361
+     * @return Response
362
+     * @throws TelegramException
363
+     */
364
+    public static function __callStatic(string $action, array $data): Response
365
+    {
366
+        return static::send($action, reset($data) ?: []);
367
+    }
368 368
 
369 369
 }
Please login to merge, or discard this patch.
src/lib/Entities/Keyboard.php 1 patch
Indentation   +98 added lines, -98 removed lines patch added patch discarded remove patch
@@ -23,103 +23,103 @@
 block discarded – undo
23 23
 class Keyboard extends Entity
24 24
 {
25 25
 
26
-	public function __construct(array $data = [])
27
-	{
28
-		parent::__construct(array_merge($data, [
29
-			'resize_keyboard' => false,
30
-			'one_time_keyboard' => false,
31
-			'input_field_placeholder' => '',
32
-			'selective' => false
33
-		]));
34
-	}
35
-
36
-	/**
37
-	 * Create a new row in keyboard and add buttons.
38
-	 *
39
-	 * @param array<KeyboardButton> $row
40
-	 * @return Keyboard
41
-	 */
42
-	public function addRow(array $row): Keyboard
43
-	{
44
-		$keyboard_type = self::getType();
45
-
46
-		if (!isset($this->raw_data[$keyboard_type]) || !is_array($this->raw_data[$keyboard_type])) {
47
-			$this->raw_data[$keyboard_type] = [];
48
-		}
49
-
50
-		$new_row = [];
51
-		foreach ($row as $button) {
52
-			$new_row[] = $button->getRawData();
53
-		}
54
-
55
-		$this->raw_data[$keyboard_type][] = $new_row;
56
-
57
-		return $this;
58
-	}
59
-
60
-	/**
61
-	 * Remove the current custom keyboard and display the default letter-keyboard.
62
-	 *
63
-	 * @link https://core.telegram.org/bots/api/#replykeyboardremove
64
-	 *
65
-	 * @param array $data
66
-	 *
67
-	 * @return Keyboard
68
-	 */
69
-	public static function remove(array $data = []): Keyboard
70
-	{
71
-		return new static(array_merge(['keyboard' => [], 'remove_keyboard' => true, 'selective' => false], $data));
72
-	}
73
-
74
-	/**
75
-	 * Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply').
76
-	 *
77
-	 * @link https://core.telegram.org/bots/api#forcereply
78
-	 *
79
-	 * @param array $data
80
-	 *
81
-	 * @return Keyboard
82
-	 */
83
-	public static function forceReply(array $data = []): Keyboard
84
-	{
85
-		return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data));
86
-	}
87
-
88
-	/**
89
-	 * Get keyboard button type
90
-	 *
91
-	 * @return string ["keyboard"|"inline_keyboard"]
92
-	 */
93
-	public function getType(): string
94
-	{
95
-		$reflection = new \ReflectionClass(static::class);
96
-
97
-		$class_name = $reflection->getShortName();
98
-
99
-		return strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', $class_name), '_'));
100
-	}
101
-
102
-	/**
103
-	 * Creates instance of Keyboard
104
-	 *
105
-	 * @return Keyboard
106
-	 */
107
-	public static function make(): Keyboard
108
-	{
109
-		return new self();
110
-	}
111
-
112
-	/**
113
-	 * @param array $rows
114
-	 * @return Keyboard
115
-	 */
116
-	public function setKeyboard(array $rows): Keyboard
117
-	{
118
-		foreach ($rows as $row) {
119
-			$this->addRow($row);
120
-		}
121
-
122
-		return $this;
123
-	}
26
+    public function __construct(array $data = [])
27
+    {
28
+        parent::__construct(array_merge($data, [
29
+            'resize_keyboard' => false,
30
+            'one_time_keyboard' => false,
31
+            'input_field_placeholder' => '',
32
+            'selective' => false
33
+        ]));
34
+    }
35
+
36
+    /**
37
+     * Create a new row in keyboard and add buttons.
38
+     *
39
+     * @param array<KeyboardButton> $row
40
+     * @return Keyboard
41
+     */
42
+    public function addRow(array $row): Keyboard
43
+    {
44
+        $keyboard_type = self::getType();
45
+
46
+        if (!isset($this->raw_data[$keyboard_type]) || !is_array($this->raw_data[$keyboard_type])) {
47
+            $this->raw_data[$keyboard_type] = [];
48
+        }
49
+
50
+        $new_row = [];
51
+        foreach ($row as $button) {
52
+            $new_row[] = $button->getRawData();
53
+        }
54
+
55
+        $this->raw_data[$keyboard_type][] = $new_row;
56
+
57
+        return $this;
58
+    }
59
+
60
+    /**
61
+     * Remove the current custom keyboard and display the default letter-keyboard.
62
+     *
63
+     * @link https://core.telegram.org/bots/api/#replykeyboardremove
64
+     *
65
+     * @param array $data
66
+     *
67
+     * @return Keyboard
68
+     */
69
+    public static function remove(array $data = []): Keyboard
70
+    {
71
+        return new static(array_merge(['keyboard' => [], 'remove_keyboard' => true, 'selective' => false], $data));
72
+    }
73
+
74
+    /**
75
+     * Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply').
76
+     *
77
+     * @link https://core.telegram.org/bots/api#forcereply
78
+     *
79
+     * @param array $data
80
+     *
81
+     * @return Keyboard
82
+     */
83
+    public static function forceReply(array $data = []): Keyboard
84
+    {
85
+        return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data));
86
+    }
87
+
88
+    /**
89
+     * Get keyboard button type
90
+     *
91
+     * @return string ["keyboard"|"inline_keyboard"]
92
+     */
93
+    public function getType(): string
94
+    {
95
+        $reflection = new \ReflectionClass(static::class);
96
+
97
+        $class_name = $reflection->getShortName();
98
+
99
+        return strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', $class_name), '_'));
100
+    }
101
+
102
+    /**
103
+     * Creates instance of Keyboard
104
+     *
105
+     * @return Keyboard
106
+     */
107
+    public static function make(): Keyboard
108
+    {
109
+        return new self();
110
+    }
111
+
112
+    /**
113
+     * @param array $rows
114
+     * @return Keyboard
115
+     */
116
+    public function setKeyboard(array $rows): Keyboard
117
+    {
118
+        foreach ($rows as $row) {
119
+            $this->addRow($row);
120
+        }
121
+
122
+        return $this;
123
+    }
124 124
 
125 125
 }
Please login to merge, or discard this patch.
src/lib/Entities/Update.php 1 patch
Indentation   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -32,70 +32,70 @@
 block discarded – undo
32 32
 class Update extends Entity
33 33
 {
34 34
 
35
-	public const TYPE_MESSAGE = 'message';
36
-	public const TYPE_EDITED_MESSAGE = 'edited_message';
37
-	public const TYPE_CHANNEL_POST = 'channel_post';
38
-	public const TYPE_EDITED_CHANNEL_POST = 'edited_channel_post';
39
-	public const TYPE_INLINE_QUERY = 'inline_query';
40
-	public const TYPE_CHOSEN_INLINE_RESULT = 'chosen_inline_result';
41
-	public const TYPE_CALLBACK_QUERY = 'callback_query';
42
-	public const TYPE_SHIPPING_QUERY = 'shipping_query';
43
-	public const TYPE_PRE_CHECKOUT_QUERY = 'pre_checkout_query';
44
-	public const TYPE_POLL = 'poll';
45
-	public const TYPE_POLL_ANSWER = 'poll_answer';
46
-	public const TYPE_MY_CHAT_MEMBER = 'my_chat_member';
47
-	public const TYPE_CHAT_MEMBER = 'chat_member';
48
-	public const TYPE_CHAT_JOIN_REQUEST = 'chat_join_request';
49
-	public const TYPE_WEB_DATA = 'web_data';
35
+    public const TYPE_MESSAGE = 'message';
36
+    public const TYPE_EDITED_MESSAGE = 'edited_message';
37
+    public const TYPE_CHANNEL_POST = 'channel_post';
38
+    public const TYPE_EDITED_CHANNEL_POST = 'edited_channel_post';
39
+    public const TYPE_INLINE_QUERY = 'inline_query';
40
+    public const TYPE_CHOSEN_INLINE_RESULT = 'chosen_inline_result';
41
+    public const TYPE_CALLBACK_QUERY = 'callback_query';
42
+    public const TYPE_SHIPPING_QUERY = 'shipping_query';
43
+    public const TYPE_PRE_CHECKOUT_QUERY = 'pre_checkout_query';
44
+    public const TYPE_POLL = 'poll';
45
+    public const TYPE_POLL_ANSWER = 'poll_answer';
46
+    public const TYPE_MY_CHAT_MEMBER = 'my_chat_member';
47
+    public const TYPE_CHAT_MEMBER = 'chat_member';
48
+    public const TYPE_CHAT_JOIN_REQUEST = 'chat_join_request';
49
+    public const TYPE_WEB_DATA = 'web_data';
50 50
 
51
-	/**
52
-	 * {@inheritdoc}
53
-	 */
54
-	protected function subEntities(): array
55
-	{
56
-		return [
57
-			self::TYPE_MESSAGE => Message::class,
58
-			self::TYPE_EDITED_MESSAGE => EditedMessage::class,
59
-			self::TYPE_CHANNEL_POST => ChannelPost::class,
60
-			self::TYPE_EDITED_CHANNEL_POST => EditedChannelPost::class,
61
-			self::TYPE_INLINE_QUERY => InlineQuery::class,
62
-			self::TYPE_CHOSEN_INLINE_RESULT => ChosenInlineResult::class,
63
-			self::TYPE_CALLBACK_QUERY => CallbackQuery::class,
64
-			self::TYPE_SHIPPING_QUERY => ShippingQuery::class,
65
-			self::TYPE_PRE_CHECKOUT_QUERY => PreCheckoutQuery::class,
66
-			self::TYPE_POLL => Poll::class,
67
-			self::TYPE_POLL_ANSWER => PollAnswer::class,
68
-			self::TYPE_MY_CHAT_MEMBER => ChatMemberUpdated::class,
69
-			self::TYPE_CHAT_MEMBER => ChatMemberUpdated::class,
70
-			self::TYPE_CHAT_JOIN_REQUEST => ChatJoinRequest::class,
71
-			self::TYPE_WEB_DATA => WebAppData::class,
72
-		];
73
-	}
51
+    /**
52
+     * {@inheritdoc}
53
+     */
54
+    protected function subEntities(): array
55
+    {
56
+        return [
57
+            self::TYPE_MESSAGE => Message::class,
58
+            self::TYPE_EDITED_MESSAGE => EditedMessage::class,
59
+            self::TYPE_CHANNEL_POST => ChannelPost::class,
60
+            self::TYPE_EDITED_CHANNEL_POST => EditedChannelPost::class,
61
+            self::TYPE_INLINE_QUERY => InlineQuery::class,
62
+            self::TYPE_CHOSEN_INLINE_RESULT => ChosenInlineResult::class,
63
+            self::TYPE_CALLBACK_QUERY => CallbackQuery::class,
64
+            self::TYPE_SHIPPING_QUERY => ShippingQuery::class,
65
+            self::TYPE_PRE_CHECKOUT_QUERY => PreCheckoutQuery::class,
66
+            self::TYPE_POLL => Poll::class,
67
+            self::TYPE_POLL_ANSWER => PollAnswer::class,
68
+            self::TYPE_MY_CHAT_MEMBER => ChatMemberUpdated::class,
69
+            self::TYPE_CHAT_MEMBER => ChatMemberUpdated::class,
70
+            self::TYPE_CHAT_JOIN_REQUEST => ChatJoinRequest::class,
71
+            self::TYPE_WEB_DATA => WebAppData::class,
72
+        ];
73
+    }
74 74
 
75
-	/**
76
-	 * Get the list of all available update types
77
-	 *
78
-	 * @return string[]
79
-	 */
80
-	public static function getUpdateTypes(): array
81
-	{
82
-		return array_keys((new self([]))->subEntities());
83
-	}
75
+    /**
76
+     * Get the list of all available update types
77
+     *
78
+     * @return string[]
79
+     */
80
+    public static function getUpdateTypes(): array
81
+    {
82
+        return array_keys((new self([]))->subEntities());
83
+    }
84 84
 
85
-	/**
86
-	 * Get the update type based on the set properties
87
-	 *
88
-	 * @return string|null
89
-	 */
90
-	public function getUpdateType(): ?string
91
-	{
92
-		foreach (self::getUpdateTypes() as $type) {
93
-			if ($this->getProperty($type)) {
94
-				return $type;
95
-			}
96
-		}
85
+    /**
86
+     * Get the update type based on the set properties
87
+     *
88
+     * @return string|null
89
+     */
90
+    public function getUpdateType(): ?string
91
+    {
92
+        foreach (self::getUpdateTypes() as $type) {
93
+            if ($this->getProperty($type)) {
94
+                return $type;
95
+            }
96
+        }
97 97
 
98
-		return null;
99
-	}
98
+        return null;
99
+    }
100 100
 
101 101
 }
Please login to merge, or discard this patch.
src/lib/WebhookHandler.php 2 patches
Indentation   +217 added lines, -217 removed lines patch added patch discarded remove patch
@@ -17,222 +17,222 @@
 block discarded – undo
17 17
 abstract class WebhookHandler extends Telegram
18 18
 {
19 19
 
20
-	/**
21
-	 * @var ?Update
22
-	 */
23
-	protected ?Update $update;
24
-
25
-	/**
26
-	 * @var array<Plugin>
27
-	 */
28
-	private array $plugins = [];
29
-
30
-	/**
31
-	 * @var bool
32
-	 */
33
-	private bool $isActive = false;
34
-
35
-	/**
36
-	 * The default configuration of the webhook.
37
-	 *
38
-	 * @var array
39
-	 */
40
-	private array $config = [
41
-		'autoload_env_file' => false,
42
-		'env_file_path' => null,
43
-	];
44
-
45
-	/**
46
-	 * Webhook constructor.
47
-	 *
48
-	 * @param string $api_key The API key of the bot. Leave it blank for autoload from .env file.
49
-	 */
50
-	public function __construct(string $api_key = '')
51
-	{
52
-		parent::__construct($api_key);
53
-
54
-		if (!Telegram::validateToken(self::getApiKey())) {
55
-			throw new InvalidBotTokenException();
56
-		}
57
-	}
58
-
59
-	/**
60
-	 * Initialize the receiver.
61
-	 *
62
-	 * @return void
63
-	 */
64
-	public function init(): void
65
-	{
66
-		$this->config['env_file_path'] = $_SERVER['DOCUMENT_ROOT'] . '/.env';
67
-	}
68
-
69
-	/**
70
-	 * Add a plugin to the receiver
71
-	 *
72
-	 * @param array<Plugin> $plugins
73
-	 */
74
-	public function addPlugin(array $plugins): void
75
-	{
76
-		foreach ($plugins as $plugin) {
77
-			if (!is_subclass_of($plugin, Plugin::class)) {
78
-				throw new \RuntimeException(
79
-					sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class)
80
-				);
81
-			}
82
-			$this->plugins[] = $plugin;
83
-		}
84
-	}
85
-
86
-	/**
87
-	 * Match the update with the given plugins
88
-	 *
89
-	 * @param array<Plugin> $plugins
90
-	 * @param ?Update $update The update to match
91
-	 * @return void
92
-	 */
93
-	public function loadPluginsWith(array $plugins, Update $update = null): void
94
-	{
95
-		$update = $update ?? ($this->update ?? Telegram::getUpdate());
96
-
97
-		foreach ($plugins as $plugin) {
98
-			if (!is_subclass_of($plugin, Plugin::class)) {
99
-				throw new \InvalidArgumentException(
100
-					sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class)
101
-				);
102
-			}
103
-		}
104
-
105
-		if ($update instanceof Update) {
106
-			$this->spreadUpdateWith($update, $plugins);
107
-		}
108
-	}
109
-
110
-	/**
111
-	 * Match the message to the plugins
112
-	 *
113
-	 * @param ?Update $update The update to match
114
-	 * @return void
115
-	 */
116
-	public function loadPlugins(Update $update = null): void
117
-	{
118
-		$update = $update ?? ($this->update ?? Telegram::getUpdate());
119
-		$this->loadPluginsWith($this->plugins, $update);
120
-	}
121
-
122
-	/**
123
-	 * Load the environment's
124
-	 *
125
-	 * @param string $path
126
-	 * @retrun void
127
-	 */
128
-	public function loadFileOfEnv(string $path): void
129
-	{
130
-		DotEnv::load($path);
131
-	}
132
-
133
-	/**
134
-	 * Update the configuration
135
-	 *
136
-	 * @param array $configuration
137
-	 * @return void
138
-	 */
139
-	public function updateConfiguration(array $configuration): void
140
-	{
141
-		$this->config = array_merge($this->config, $configuration);
142
-	}
143
-
144
-	/**
145
-	 * Resolve the request.
146
-	 *
147
-	 * @param ?Update $update The custom to work with
148
-	 * @param array $config The configuration of the receiver
149
-	 *
150
-	 * @retrun void
151
-	 */
152
-	public function resolve(Update $update = null, array $config = []): void
153
-	{
154
-		$method = '__process';
155
-		if (!method_exists($this, $method)) {
156
-			throw new \RuntimeException(sprintf('The method %s does not exist', $method));
157
-		}
158
-
159
-		if (is_array($config)) $this->updateConfiguration($config);
160
-
161
-		if (!empty($update)) $this->update = $update;
162
-		else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null;
163
-
164
-		if (empty($this->update)) {
165
-			TelegramLog::error(
166
-				'The update is empty, the request is not processed'
167
-			);
168
-			return;
169
-		}
170
-
171
-		try {
172
-
173
-			Common::arrest($this, $method, $this->update);
174
-
175
-		} catch (\RuntimeException $exception) {
176
-
177
-			$e = $exception->getPrevious();
178
-			TelegramLog::error(($message = sprintf(
179
-				'%s(%d): %s\n%s', $e->getFile(), $e->getLine(), $e->getMessage(), $e->getTraceAsString()
180
-			)));
181
-
182
-			if (defined('TG_ADMIN_ID') && TG_ADMIN_ID && defined('DEBUG_MODE') && DEBUG_MODE) {
183
-				file_put_contents(
184
-					($file = getcwd() . '/' . uniqid('error_') . '.log'),
185
-					$message . PHP_EOL . PHP_EOL . $update->getRawData(false)
186
-				);
187
-				Request::sendMessage([
188
-					'chat_id' => TG_ADMIN_ID,
189
-					'text' => $message,
190
-				]);
191
-				Request::sendDocument([
192
-					'chat_id' => TG_ADMIN_ID,
193
-					'document' => $file,
194
-				]);
195
-				unlink($file);
196
-			}
197
-		}
198
-	}
199
-
200
-	/**
201
-	 * This function will get update and spread it to the plugins
202
-	 *
203
-	 * @param Update $update
204
-	 * @param array<Plugin> $plugins
205
-	 * @return void
206
-	 */
207
-	private function spreadUpdateWith(Update $update, array $plugins): void
208
-	{
209
-		$this->isActive = true;
210
-
211
-		foreach ($plugins as $plugin) {
212
-			/** @var Plugin $plugin */
213
-			(new $plugin())->__execute($this, $update);
214
-			if ($this->isActive === false) break;
215
-		}
216
-
217
-		$this->isActive = false;
218
-	}
219
-
220
-	/**
221
-	 * Kill the speeding process
222
-	 *
223
-	 * @return void
224
-	 */
225
-	public function kill(): void
226
-	{
227
-		$this->isActive = false;
228
-	}
229
-
230
-	/**
231
-	 * Use this method on the webhook class to get the updates
232
-	 *
233
-	 * @param Update $update
234
-	 * @return void
235
-	 */
236
-	abstract public function __process(Update $update): void;
20
+    /**
21
+     * @var ?Update
22
+     */
23
+    protected ?Update $update;
24
+
25
+    /**
26
+     * @var array<Plugin>
27
+     */
28
+    private array $plugins = [];
29
+
30
+    /**
31
+     * @var bool
32
+     */
33
+    private bool $isActive = false;
34
+
35
+    /**
36
+     * The default configuration of the webhook.
37
+     *
38
+     * @var array
39
+     */
40
+    private array $config = [
41
+        'autoload_env_file' => false,
42
+        'env_file_path' => null,
43
+    ];
44
+
45
+    /**
46
+     * Webhook constructor.
47
+     *
48
+     * @param string $api_key The API key of the bot. Leave it blank for autoload from .env file.
49
+     */
50
+    public function __construct(string $api_key = '')
51
+    {
52
+        parent::__construct($api_key);
53
+
54
+        if (!Telegram::validateToken(self::getApiKey())) {
55
+            throw new InvalidBotTokenException();
56
+        }
57
+    }
58
+
59
+    /**
60
+     * Initialize the receiver.
61
+     *
62
+     * @return void
63
+     */
64
+    public function init(): void
65
+    {
66
+        $this->config['env_file_path'] = $_SERVER['DOCUMENT_ROOT'] . '/.env';
67
+    }
68
+
69
+    /**
70
+     * Add a plugin to the receiver
71
+     *
72
+     * @param array<Plugin> $plugins
73
+     */
74
+    public function addPlugin(array $plugins): void
75
+    {
76
+        foreach ($plugins as $plugin) {
77
+            if (!is_subclass_of($plugin, Plugin::class)) {
78
+                throw new \RuntimeException(
79
+                    sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class)
80
+                );
81
+            }
82
+            $this->plugins[] = $plugin;
83
+        }
84
+    }
85
+
86
+    /**
87
+     * Match the update with the given plugins
88
+     *
89
+     * @param array<Plugin> $plugins
90
+     * @param ?Update $update The update to match
91
+     * @return void
92
+     */
93
+    public function loadPluginsWith(array $plugins, Update $update = null): void
94
+    {
95
+        $update = $update ?? ($this->update ?? Telegram::getUpdate());
96
+
97
+        foreach ($plugins as $plugin) {
98
+            if (!is_subclass_of($plugin, Plugin::class)) {
99
+                throw new \InvalidArgumentException(
100
+                    sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class)
101
+                );
102
+            }
103
+        }
104
+
105
+        if ($update instanceof Update) {
106
+            $this->spreadUpdateWith($update, $plugins);
107
+        }
108
+    }
109
+
110
+    /**
111
+     * Match the message to the plugins
112
+     *
113
+     * @param ?Update $update The update to match
114
+     * @return void
115
+     */
116
+    public function loadPlugins(Update $update = null): void
117
+    {
118
+        $update = $update ?? ($this->update ?? Telegram::getUpdate());
119
+        $this->loadPluginsWith($this->plugins, $update);
120
+    }
121
+
122
+    /**
123
+     * Load the environment's
124
+     *
125
+     * @param string $path
126
+     * @retrun void
127
+     */
128
+    public function loadFileOfEnv(string $path): void
129
+    {
130
+        DotEnv::load($path);
131
+    }
132
+
133
+    /**
134
+     * Update the configuration
135
+     *
136
+     * @param array $configuration
137
+     * @return void
138
+     */
139
+    public function updateConfiguration(array $configuration): void
140
+    {
141
+        $this->config = array_merge($this->config, $configuration);
142
+    }
143
+
144
+    /**
145
+     * Resolve the request.
146
+     *
147
+     * @param ?Update $update The custom to work with
148
+     * @param array $config The configuration of the receiver
149
+     *
150
+     * @retrun void
151
+     */
152
+    public function resolve(Update $update = null, array $config = []): void
153
+    {
154
+        $method = '__process';
155
+        if (!method_exists($this, $method)) {
156
+            throw new \RuntimeException(sprintf('The method %s does not exist', $method));
157
+        }
158
+
159
+        if (is_array($config)) $this->updateConfiguration($config);
160
+
161
+        if (!empty($update)) $this->update = $update;
162
+        else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null;
163
+
164
+        if (empty($this->update)) {
165
+            TelegramLog::error(
166
+                'The update is empty, the request is not processed'
167
+            );
168
+            return;
169
+        }
170
+
171
+        try {
172
+
173
+            Common::arrest($this, $method, $this->update);
174
+
175
+        } catch (\RuntimeException $exception) {
176
+
177
+            $e = $exception->getPrevious();
178
+            TelegramLog::error(($message = sprintf(
179
+                '%s(%d): %s\n%s', $e->getFile(), $e->getLine(), $e->getMessage(), $e->getTraceAsString()
180
+            )));
181
+
182
+            if (defined('TG_ADMIN_ID') && TG_ADMIN_ID && defined('DEBUG_MODE') && DEBUG_MODE) {
183
+                file_put_contents(
184
+                    ($file = getcwd() . '/' . uniqid('error_') . '.log'),
185
+                    $message . PHP_EOL . PHP_EOL . $update->getRawData(false)
186
+                );
187
+                Request::sendMessage([
188
+                    'chat_id' => TG_ADMIN_ID,
189
+                    'text' => $message,
190
+                ]);
191
+                Request::sendDocument([
192
+                    'chat_id' => TG_ADMIN_ID,
193
+                    'document' => $file,
194
+                ]);
195
+                unlink($file);
196
+            }
197
+        }
198
+    }
199
+
200
+    /**
201
+     * This function will get update and spread it to the plugins
202
+     *
203
+     * @param Update $update
204
+     * @param array<Plugin> $plugins
205
+     * @return void
206
+     */
207
+    private function spreadUpdateWith(Update $update, array $plugins): void
208
+    {
209
+        $this->isActive = true;
210
+
211
+        foreach ($plugins as $plugin) {
212
+            /** @var Plugin $plugin */
213
+            (new $plugin())->__execute($this, $update);
214
+            if ($this->isActive === false) break;
215
+        }
216
+
217
+        $this->isActive = false;
218
+    }
219
+
220
+    /**
221
+     * Kill the speeding process
222
+     *
223
+     * @return void
224
+     */
225
+    public function kill(): void
226
+    {
227
+        $this->isActive = false;
228
+    }
229
+
230
+    /**
231
+     * Use this method on the webhook class to get the updates
232
+     *
233
+     * @param Update $update
234
+     * @return void
235
+     */
236
+    abstract public function __process(Update $update): void;
237 237
 
238 238
 }
239 239
\ No newline at end of file
Please login to merge, or discard this patch.
Braces   +11 added lines, -4 removed lines patch added patch discarded remove patch
@@ -156,10 +156,15 @@  discard block
 block discarded – undo
156 156
 			throw new \RuntimeException(sprintf('The method %s does not exist', $method));
157 157
 		}
158 158
 
159
-		if (is_array($config)) $this->updateConfiguration($config);
159
+		if (is_array($config)) {
160
+		    $this->updateConfiguration($config);
161
+		}
160 162
 
161
-		if (!empty($update)) $this->update = $update;
162
-		else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null;
163
+		if (!empty($update)) {
164
+		    $this->update = $update;
165
+		} else {
166
+		    $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null;
167
+		}
163 168
 
164 169
 		if (empty($this->update)) {
165 170
 			TelegramLog::error(
@@ -211,7 +216,9 @@  discard block
 block discarded – undo
211 216
 		foreach ($plugins as $plugin) {
212 217
 			/** @var Plugin $plugin */
213 218
 			(new $plugin())->__execute($this, $update);
214
-			if ($this->isActive === false) break;
219
+			if ($this->isActive === false) {
220
+			    break;
221
+			}
215 222
 		}
216 223
 
217 224
 		$this->isActive = false;
Please login to merge, or discard this patch.
src/lib/Plugin.php 1 patch
Indentation   +110 added lines, -110 removed lines patch added patch discarded remove patch
@@ -14,115 +14,115 @@
 block discarded – undo
14 14
 abstract class Plugin
15 15
 {
16 16
 
17
-	/**
18
-	 * The Update types
19
-	 *
20
-	 * @var array
21
-	 */
22
-	protected array $update_types = [
23
-		'message',
24
-		'edited_message',
25
-		'channel_post',
26
-		'edited_channel_post',
27
-		'inline_query',
28
-		'chosen_inline_result',
29
-		'callback_query',
30
-		'shipping_query',
31
-		'pre_checkout_query',
32
-	];
33
-
34
-	/**
35
-	 * @var WebhookHandler
36
-	 */
37
-	protected WebhookHandler $hook;
38
-
39
-	/**
40
-	 * @var \Generator
41
-	 */
42
-	protected \Generator $returns;
43
-
44
-	/**
45
-	 * @var bool
46
-	 */
47
-	protected bool $kill_on_yield = false;
48
-
49
-	/**
50
-	 * Check for the exit of the plugin.
51
-	 *
52
-	 * @param \Generator $return
53
-	 * @return void
54
-	 */
55
-	public function __checkExit(\Generator $return): void
56
-	{
57
-		if ($return->valid()) {
58
-			if ($return->current() !== null && $this->kill_on_yield === true) {
59
-				$this->kill();
60
-			}
61
-		}
62
-
63
-		if ($return->valid()) {
64
-			$return->next();
65
-			$this->__checkExit($return);
66
-		}
67
-	}
68
-
69
-	/**
70
-	 * Identify the update type. e.g. Message, EditedMessage, etc.
71
-	 *
72
-	 * @param Update $update
73
-	 * @return string|null
74
-	 */
75
-	public function __identify(Update $update): ?string
76
-	{
77
-		$type = $update->getUpdateType();
78
-
79
-		if ($type === null) {
80
-			return null;
81
-		}
82
-
83
-		return str_replace('_', '', ucwords($type, '_'));
84
-	}
85
-
86
-
87
-	/**
88
-	 * Execute the plugin.
89
-	 *
90
-	 * @param WebhookHandler $receiver
91
-	 * @param Update $update
92
-	 * @return void
93
-	 */
94
-	public function __execute(WebhookHandler $receiver, Update $update): void
95
-	{
96
-		$this->hook = $receiver;
97
-
98
-		if (method_exists($this, 'onReceivedUpdate')) {
99
-			$return = $this->onReceivedUpdate($update);
100
-			$this->__checkExit($return);
101
-		}
102
-
103
-		$type = $this->__identify($update);
104
-		if (method_exists($this, ($method = 'on' . $type)) && $type !== null) {
105
-			$return = $this->$method($update);
106
-			$this->__checkExit($return);
107
-		}
108
-	}
109
-
110
-	/**
111
-	 * Identify the update type and if method of the type is exists, execute it.
112
-	 *
113
-	 * @param Update $update
114
-	 * @return \Generator
115
-	 */
116
-	abstract protected function onReceivedUpdate(Update $update): \Generator;
117
-
118
-	/**
119
-	 * Kill the plugin.
120
-	 *
121
-	 * @return void
122
-	 */
123
-	public function kill(): void
124
-	{
125
-		$this->hook->kill();
126
-	}
17
+    /**
18
+     * The Update types
19
+     *
20
+     * @var array
21
+     */
22
+    protected array $update_types = [
23
+        'message',
24
+        'edited_message',
25
+        'channel_post',
26
+        'edited_channel_post',
27
+        'inline_query',
28
+        'chosen_inline_result',
29
+        'callback_query',
30
+        'shipping_query',
31
+        'pre_checkout_query',
32
+    ];
33
+
34
+    /**
35
+     * @var WebhookHandler
36
+     */
37
+    protected WebhookHandler $hook;
38
+
39
+    /**
40
+     * @var \Generator
41
+     */
42
+    protected \Generator $returns;
43
+
44
+    /**
45
+     * @var bool
46
+     */
47
+    protected bool $kill_on_yield = false;
48
+
49
+    /**
50
+     * Check for the exit of the plugin.
51
+     *
52
+     * @param \Generator $return
53
+     * @return void
54
+     */
55
+    public function __checkExit(\Generator $return): void
56
+    {
57
+        if ($return->valid()) {
58
+            if ($return->current() !== null && $this->kill_on_yield === true) {
59
+                $this->kill();
60
+            }
61
+        }
62
+
63
+        if ($return->valid()) {
64
+            $return->next();
65
+            $this->__checkExit($return);
66
+        }
67
+    }
68
+
69
+    /**
70
+     * Identify the update type. e.g. Message, EditedMessage, etc.
71
+     *
72
+     * @param Update $update
73
+     * @return string|null
74
+     */
75
+    public function __identify(Update $update): ?string
76
+    {
77
+        $type = $update->getUpdateType();
78
+
79
+        if ($type === null) {
80
+            return null;
81
+        }
82
+
83
+        return str_replace('_', '', ucwords($type, '_'));
84
+    }
85
+
86
+
87
+    /**
88
+     * Execute the plugin.
89
+     *
90
+     * @param WebhookHandler $receiver
91
+     * @param Update $update
92
+     * @return void
93
+     */
94
+    public function __execute(WebhookHandler $receiver, Update $update): void
95
+    {
96
+        $this->hook = $receiver;
97
+
98
+        if (method_exists($this, 'onReceivedUpdate')) {
99
+            $return = $this->onReceivedUpdate($update);
100
+            $this->__checkExit($return);
101
+        }
102
+
103
+        $type = $this->__identify($update);
104
+        if (method_exists($this, ($method = 'on' . $type)) && $type !== null) {
105
+            $return = $this->$method($update);
106
+            $this->__checkExit($return);
107
+        }
108
+    }
109
+
110
+    /**
111
+     * Identify the update type and if method of the type is exists, execute it.
112
+     *
113
+     * @param Update $update
114
+     * @return \Generator
115
+     */
116
+    abstract protected function onReceivedUpdate(Update $update): \Generator;
117
+
118
+    /**
119
+     * Kill the plugin.
120
+     *
121
+     * @return void
122
+     */
123
+    public function kill(): void
124
+    {
125
+        $this->hook->kill();
126
+    }
127 127
 
128 128
 }
129 129
\ No newline at end of file
Please login to merge, or discard this patch.