Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TelegramTypes ChatMember updates #154

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

DJTommek
Copy link
Contributor

@DJTommek DJTommek commented Sep 24, 2023

This updates is mostly related to users. Here is quick overview of changes:

All touched classes were updated to match documentation (types and PHPDocs) as of September 22, 2023 Bot API 6.9.

Copy link
Owner

@unreal4u unreal4u left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's great to see Telegram handling some deprecated use-cases!

However, I do see some inconsistencies which could benefit from another approach which is a bit less elegant, but will not introduce the chance for more BC in the future.

I know I am requesting a lot of changes here for which I'm sorry! But I can also offer you some help with it if needed :)

Comment on lines +25 to +32
private const CHILD_CLASS_REFERENCES = [
ChatMemberOwner::class,
ChatMemberAdministrator::class,
ChatMemberMember::class,
ChatMemberRestricted::class,
ChatMemberLeft::class,
ChatMemberBanned::class,
];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like for the actual Telegram classes to remain as clean as possible from 'external' code for two reasons:

  • If Telegram decides to create a field called created it won't clash with the defined function.
  • It will prevent our users from actually depending on this code, people do crazy stuff and any changes to this will be BC.

I'm thinking that a better option would be to perhaps create a TelegramAPI\Telegram\Custom\ChatMemberFactory factory and make the bindToObject in GetChatMember (and others that return a ChatMemer type) call that dedicated factory instead.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One eternity later

Ok, I would need to take a closer look, but I believe the function mapSubObjects was created for this purpose.

/**
* Create specific ChatMember class based on 'status'. If there is no match,
*/
public static function create(array $data, LoggerInterface $logger): ChatMember
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go into a factory and not in the Telegram class type.

*/
class ChatMemberBanned extends ChatMember
{
public const STATUS = 'kicked';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the same vain of keeping this as close to the source as possible, it would be better not to define constants here (or in any of the other ChatMember classes).

A simpler and more consistent way would be to define the status in the same way we do here:

by just assigning it directly to the public property.

@@ -15,131 +22,27 @@
*/
class ChatMember extends TelegramTypes
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be defined as an abstract method since a ChatMember will always be of a certain type.

'data' => $data,
]);

return new ChatMember($data, $logger);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok to throw an exception here. I would not support creating an object that is invalid to begin with.

Comment on lines +63 to +67
foreach (self::CHILD_CLASS_REFERENCES as $childClassReference) {
if ($status === $childClassReference::STATUS) {
return new $childClassReference($data, $logger);
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is pleasant to look at, I much prefer dropping the constant within each type in order to keep those objects as clean as possible, and make the factory call the child classes directly.

I'm not a big fan of this, but I believe that's the best way, very much as we do here: https://github.com/unreal4u/telegram-api/blob/master/src/Telegram/Types/Message.php#L501-L566 (Yes, I hate that part of the code but it was the only way I could think of at the time).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants