Skip to content

Commit ee3a443

Browse files
committed
Cleaned up ChatMember\Factory code
1 parent ba6cb0f commit ee3a443

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/Entities/ChatMember/Factory.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,24 @@ class Factory extends \Longman\TelegramBot\Entities\Factory
1111
{
1212
public function make(array $data, string $bot_username): Entity
1313
{
14-
switch ($data['status'] ?? '') {
15-
case 'creator':
16-
return new ChatMemberOwner($data, $bot_username);
17-
case 'administrator':
18-
return new ChatMemberAdministrator($data, $bot_username);
19-
case 'member':
20-
return new ChatMemberMember($data, $bot_username);
21-
case 'restricted':
22-
return new ChatMemberRestricted($data, $bot_username);
23-
case 'left':
24-
return new ChatMemberLeft($data, $bot_username);
25-
case 'kicked':
26-
return new ChatMemberBanned($data, $bot_username);
14+
if (! isset($data['status'])) {
15+
throw new TelegramException('Missing ChatMember status');
2716
}
2817

29-
throw new TelegramException('Unexpected ChatMember type');
18+
$type = [
19+
'creator' => ChatMemberOwner::class,
20+
'administrator' => ChatMemberAdministrator::class,
21+
'member' => ChatMemberMember::class,
22+
'restricted' => ChatMemberRestricted::class,
23+
'left' => ChatMemberLeft::class,
24+
'kicked' => ChatMemberBanned::class,
25+
];
26+
27+
if (! isset($type[$data['status']])) {
28+
throw new TelegramException('Unexpected ChatMember status');
29+
}
30+
31+
$class = $type[$data['status']];
32+
return new $class($data, $bot_username);
3033
}
3134
}

0 commit comments

Comments
 (0)