Skip to content

Commit 8365717

Browse files
committed
Issue #1205
Optimize addCommandsClass method Add constants for auth names Update tests
1 parent 2eeb333 commit 8365717

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

src/Telegram.php

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@
3030

3131
class Telegram
3232
{
33+
/**
34+
* Auth name for user commands
35+
*/
36+
const AUTH_USER = 'User';
37+
/**
38+
* Auth name tof system commands
39+
*/
40+
const AUTH_SYSTEM = 'System';
41+
/**
42+
* Auth name for admin commands
43+
*/
44+
const AUTH_ADMIN = 'Admin';
45+
3346
/**
3447
* Version
3548
*
@@ -86,9 +99,9 @@ class Telegram
8699
* @var array
87100
*/
88101
protected $commandsClasses = [
89-
'User' => [],
90-
'Admin' => [],
91-
'System' => [],
102+
self::AUTH_USER => [],
103+
self::AUTH_ADMIN => [],
104+
self::AUTH_SYSTEM => [],
92105
];
93106

94107
/**
@@ -340,9 +353,9 @@ public function getCommandObject(string $command, string $filepath = ''): ?Comma
340353
return $this->commands_objects[$command];
341354
}
342355

343-
$which = ['System'];
344-
$this->isAdmin() && $which[] = 'Admin';
345-
$which[] = 'User';
356+
$which = [self::AUTH_SYSTEM];
357+
$this->isAdmin() && $which[] = self::AUTH_ADMIN;
358+
$which[] = self::AUTH_USER;
346359

347360
foreach ($which as $auth)
348361
{
@@ -360,19 +373,19 @@ public function getCommandObject(string $command, string $filepath = ''): ?Comma
360373
$command_obj = new $command_class($this, $this->update);
361374

362375
switch ($auth) {
363-
case 'System':
376+
case self::AUTH_SYSTEM:
364377
if ($command_obj instanceof SystemCommand) {
365378
return $command_obj;
366379
}
367380
break;
368381

369-
case 'Admin':
382+
case self::AUTH_ADMIN:
370383
if ($command_obj instanceof AdminCommand) {
371384
return $command_obj;
372385
}
373386
break;
374387

375-
case 'User':
388+
case self::AUTH_USER:
376389
if ($command_obj instanceof UserCommand) {
377390
return $command_obj;
378391
}
@@ -766,36 +779,48 @@ public function isDbEnabled(): bool
766779
/**
767780
* Add a single custom commands class
768781
*
769-
* @param string $command Set command name
770782
* @param string $className Set full classname
771-
* @param string $auth Set Auth for command. Default is User
772-
*
773783
* @return Telegram
774784
*/
775-
public function addCommandsClass(string $command, string $className, $auth = 'User'): Telegram
785+
public function addCommandsClass(string $className): Telegram
776786
{
777-
if (!class_exists($className))
787+
if (!$className || !class_exists($className))
778788
{
779789
$error = 'Command class name: "' . $className . '" does not exist.';
780790
TelegramLog::error($error);
781791
throw new InvalidArgumentException($error);
782792
}
783-
if (empty($command))
784-
{
785-
$error = 'Command Name "' . $command . '" not set.';
786-
TelegramLog::error($error);
787-
throw new InvalidArgumentException($error);
788-
}
789793

790794
if (!is_array($this->commandsClasses))
791795
{
792796
$this->commandsClasses = [];
793797
}
794798

795-
$command = mb_strtolower($command);
796-
$auth = $this->ucFirstUnicode($auth);
799+
if (!is_a($className, Command::class, true)) {
800+
$error = 'Command class is not a base command class';
801+
TelegramLog::error($error);
802+
throw new InvalidArgumentException($error);
803+
}
797804

798-
$this->commandsClasses[$auth][$command] = $className;
805+
$commandObject = new $className($this);
806+
807+
$command = $commandObject->getName();
808+
$auth = null;
809+
switch (true) {
810+
case $commandObject->isSystemCommand():
811+
$auth = self::AUTH_SYSTEM;
812+
break;
813+
case $commandObject->isAdminCommand():
814+
$auth = self::AUTH_ADMIN;
815+
break;
816+
case $commandObject->isUserCommand():
817+
$auth = self::AUTH_USER;
818+
break;
819+
}
820+
821+
if ($auth) {
822+
$this->commandsClasses[$auth][$command] = $className;
823+
}
799824

800825
return $this;
801826
}

tests/Unit/TelegramTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
1515
use Exception;
16+
use Longman\TelegramBot\Commands\AdminCommands\WhoisCommand;
1617
use Longman\TelegramBot\Commands\UserCommands\StartCommand;
1718
use Longman\TelegramBot\Entities\Update;
1819
use Longman\TelegramBot\Exception\TelegramException;
@@ -139,25 +140,26 @@ public function testAddCustomCommandsClass(): void
139140
{
140141
$tg = $this->telegram;
141142
$class = StartCommand::class;
143+
$aClass = WhoisCommand::class;
142144

143145
self::assertCount(3, $tg->getCommandsClasses());
144146

145147
try {
146-
$tg->addCommandsClass('test', 'not\exist\Class', 'user');
148+
$tg->addCommandsClass('not\exist\Class');
147149
}
148150
catch (\Exception $ex){}
149151
self::assertCount(0, $tg->getCommandsClasses()['User']);
150152

151153
try {
152-
$tg->addCommandsClass('', $class, 'user');
154+
$tg->addCommandsClass('');
153155
}
154156
catch (\Exception $ex){}
155157
self::assertCount(0, $tg->getCommandsClasses()['User']);
156158

157-
$tg->addCommandsClass('test', $class, 'user');
159+
$tg->addCommandsClass($class);
158160
self::assertCount(1, $tg->getCommandsClasses()['User']);
159161

160-
$tg->addCommandsClass('testadmin', $class, 'admin');
162+
$tg->addCommandsClass($aClass);
161163
self::assertCount(1, $tg->getCommandsClasses()['Admin']);
162164

163165
}
@@ -191,9 +193,10 @@ public function testGetCommandClass(): void
191193
$class = $this->telegram->getCommandClassName('user', 'notexist');
192194
self::assertNull($class);
193195

194-
$this->telegram->addCommandsClass('test', $className, 'user');
195-
$class = $this->telegram->getCommandClassName('user', 'test');
196+
$this->telegram->addCommandsClass($className);
197+
$class = $this->telegram->getCommandClassName('user', 'start');
196198
self::assertNotNull($class);
199+
197200
self::assertSame($className, $class);
198201

199202
}

0 commit comments

Comments
 (0)