Skip to content

Commit d569163

Browse files
committed
Optimise setBotExtras method, to reduce complexity.
Some minor formatting corrections.
1 parent 9cf8932 commit d569163

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

src/BotManager.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,22 @@ private function handleOutput($output)
205205
*/
206206
public function setBotExtras()
207207
{
208-
($v = $this->params->getBotParam('admins')) && $this->telegram->enableAdmins($v);
209-
($v = $this->params->getBotParam('mysql')) && $this->telegram->enableMySql($v);
210-
($v = $this->params->getBotParam('botan_token')) && $this->telegram->enableBotan($v);
211-
($v = $this->params->getBotParam('commands_paths')) && $this->telegram->addCommandsPaths($v);
212-
($v = $this->params->getBotParam('custom_input')) && $this->telegram->setCustomInput($v);
213-
($v = $this->params->getBotParam('download_path')) && $this->telegram->setDownloadPath($v);
214-
($v = $this->params->getBotParam('upload_path')) && $this->telegram->setUploadPath($v);
208+
$simple_extras = [
209+
'admins' => 'enableAdmins',
210+
'mysql' => 'enableMySql',
211+
'botan_token' => 'enableBotan',
212+
'commands_paths' => 'addCommandsPaths',
213+
'custom_input' => 'setCustomInput',
214+
'download_path' => 'setDownloadPath',
215+
'upload_path' => 'setUploadPath',
216+
];
217+
// For simple extras, just pass the single param value to the Telegram method.
218+
foreach ($simple_extras as $param_key => $method) {
219+
$param = $this->params->getBotParam($param_key);
220+
if (null !== $param) {
221+
$this->telegram->$method($param);
222+
}
223+
}
215224

216225
$command_configs = $this->params->getBotParam('command_configs');
217226
if (is_array($command_configs)) {
@@ -349,8 +358,9 @@ public function handleWebhook()
349358
*/
350359
public function getOutput()
351360
{
352-
$output = $this->output;
361+
$output = $this->output;
353362
$this->output = '';
363+
354364
return $output;
355365
}
356366
}

tests/BotManagerTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public function testValidateAndSetWebhookSuccess()
140140
);
141141

142142
$telegram = $botManager->getTelegram();
143+
144+
/** @var \PHPUnit_Framework_MockObject_MockObject $telegram */
143145
$telegram->expects(static::any())
144146
->method('setWebHook')
145147
->with('https://web/hook.php?a=handle&s=secret_12345')
@@ -150,14 +152,11 @@ public function testValidateAndSetWebhookSuccess()
150152
$telegram->expects(static::any())
151153
->method('getDescription')
152154
->will(static::onConsecutiveCalls(
153-
// set
154-
'Webhook was set',
155+
'Webhook was set', // set
155156
'Webhook is already set',
156-
// reset
157-
'Webhook was deleted',
157+
'Webhook was deleted', // reset
158158
'Webhook was set',
159-
// unset
160-
'Webhook was deleted',
159+
'Webhook was deleted', //unset
161160
'Webhook is already deleted'
162161
));
163162

@@ -321,16 +320,16 @@ public function testGetOutput()
321320
public function testGetUpdatesLiveBot()
322321
{
323322
$botManager = new BotManager(ParamsTest::$live_params);
324-
$output = $botManager->run()->getOutput();
323+
$output = $botManager->run()->getOutput();
325324
self::assertContains('Updates processed: 0', $output);
326325
}
327326

328327
public function testGetUpdatesLoopLiveBot()
329328
{
330329
// Looping for 5 seconds should be enough to get a result.
331-
$_GET = ['l' => 5];
330+
$_GET = ['l' => 5];
332331
$botManager = new BotManager(ParamsTest::$live_params);
333-
$output = $botManager->run()->getOutput();
332+
$output = $botManager->run()->getOutput();
334333
self::assertContains('Looping getUpdates until', $output);
335334
self::assertContains('Updates processed: 0', $output);
336335
}

0 commit comments

Comments
 (0)