Last active
December 8, 2017 18:23
-
-
Save kefzce/28bb14674a40b54aed6c4702eaf8dc18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| try { | |
| /** @var \Telegram\Bot\Objects\User $userUpdate */ | |
| $userUpdate = $update->getFrom(); | |
| /** @var \Telegram\Bot\Objects\Chat $chatUpdate */ | |
| $chatUpdate = $update->getChat(); | |
| $userExist = User::where('user_id', '=', $userUpdate->getId())->first(); | |
| $chatExist = Chat::where('chat_id', '=', $chatUpdate->getId())->first(); | |
| if ($userExist && ! $chatExist) { | |
| $chat = Chat::create([ | |
| 'chat_id' => $chatUpdate->getId(), | |
| 'title' => $chatUpdate->getTitle(), | |
| 'type' => $chatUpdate->getType(), | |
| ]); | |
| $userExist->chat()->save($chat); | |
| $message = Message::create([ | |
| 'message_id' => $update->getMessage()->getMessageId() ?? $update->getEditedMessage()->getMessageId(), | |
| 'user_id' => $messageUpdate->getFrom()->getId(), | |
| 'chat_id' => $messageUpdate->getChat()->getId(), | |
| 'text' => $messageUpdate->getText(), | |
| ]); | |
| $userExist->messages()->save($message); | |
| } elseif (! $userExist && ! $chatExist) { | |
| $user = User::create([ | |
| 'user_id' => $userUpdate->getId(), | |
| 'first_name' => $userUpdate->getFirstName(), | |
| 'last_name' => $userUpdate->getLastName(), | |
| 'username' => $userUpdate->getUsername(), | |
| 'language_code' => $userUpdate->getLanguageCode(), | |
| ]); | |
| $chat = Chat::create([ | |
| 'chat_id' => $chatUpdate->getId(), | |
| 'title' => $chatUpdate->getTitle(), | |
| 'type' => $chatUpdate->getType(), | |
| ]); | |
| $user->chat()->save($chat); | |
| $message = Message::create([ | |
| 'message_id' => $update->getMessage()->getMessageId() ?? $update->getEditedMessage()->getMessageId(), | |
| 'user_id' => $messageUpdate->getFrom()->getId(), | |
| 'chat_id' => $messageUpdate->getChat()->getId(), | |
| 'text' => $messageUpdate->getText(), | |
| ]); | |
| $chat->messages()->save($message); | |
| $user->messages()->save($message); | |
| } elseif (! $userExist && $chatExist) { | |
| $user = User::create([ | |
| 'user_id' => $userUpdate->getId(), | |
| 'first_name' => $userUpdate->getFirstName(), | |
| 'last_name' => $userUpdate->getLastName(), | |
| 'username' => $userUpdate->getUsername(), | |
| 'language_code' => $userUpdate->getLanguageCode(), | |
| ]); | |
| $message = Message::create([ | |
| 'message_id' => $update->getMessage()->getMessageId() ?? $update->getEditedMessage()->getMessageId(), | |
| 'user_id' => $messageUpdate->getFrom()->getId(), | |
| 'chat_id' => $messageUpdate->getChat()->getId(), | |
| 'text' => $messageUpdate->getText(), | |
| ]); | |
| $chatExist->messages()->save($message); | |
| $user->chat()->save($chatExist); | |
| $user->messages()->save($message); | |
| } elseif ($userExist && $chatExist) { | |
| $message = Message::create([ | |
| 'message_id' => $update->getMessage()->getMessageId() ?? $update->getEditedMessage()->getMessageId(), | |
| 'user_id' => $messageUpdate->getFrom()->getId(), | |
| 'chat_id' => $messageUpdate->getChat()->getId(), | |
| 'text' => $messageUpdate->getText(), | |
| ]); | |
| $chatExist->messages()->save($message); | |
| $userExist->messages()->save($message); | |
| } | |
| } catch (FatalThrowableError $exception) { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <? | |
| User(users table) && chat(chats table) linked through pivot_table chat_user: | |
| Schema::create('chat_user', function (Blueprint $table) { | |
| $table->increments('id'); | |
| $table->integer('user_id')->unsigned(); | |
| $table->foreign('user_id')->references('id')->on('users'); | |
| $table->integer('chat_id')->unsigned(); | |
| $table->foreign('chat_id')->references('id')->on('chats'); | |
| }); | |
| Message(messages table) && chat(chats table) linked throught pivot_table chat_message: | |
| Schema::create('chat_message', function (Blueprint $table) { | |
| $table->increments('id'); | |
| $table->integer('message_id')->unsigned(); | |
| $table->integer('chat_id')->unsigned(); | |
| $table->foreign('message_id')->references('id')->on('messages')->onDelete('cascade'); | |
| $table->foreign('chat_id')->references('id')->on('chats')->onDelete('cascade'); | |
| $table->timestamps(); | |
| }); | |
| /** | |
| *Нужно разделить лапшу в слушателе таким образом, что бы избавиться от веток условий if elseif,сохранить SRP и сейвить все в моделях, | |
| *причем вариант с дополнительными методами для каждой сущности, вроде $this->createMessageFromUpdate не нравится | |
| *интересует что то вроде дополнительной прослойки между моделью и апдейтом, которая и разрулила бы это все, по возможности, | |
| *если есть более true way варианты, буду рад. | |
| **/ | |
| /** | |
| *Апдейт, это некая post информация, которая прилетела по ендпоинту мне на сервер. | |
| **/ | |
| Route::post('/webhook', 'Bots\TelegramController@webhook'); | |
| TelegramController@webhook: | |
| $update = $this->telegram->getWebhookUpdate(); | |
| event(new UpdateWasReceived($update)); //Простая реализация Observer pattern | |
| 'App\Events\Bots\UpdateWasReceived' => [ //Событие | |
| 'App\Listeners\Bots\ProcessingUpdates', //Слушатели | |
| 'App\Listeners\Bots\DoSomeOtherStuff', | |
| ], |
Author
Появился дополнительный вариант с репозиториями, в кратце его можно изобразить как то так:
$chat = $chatRep->create()
$user = $userRep->create();
$message = $messRep->create($chat,$user);
$message = $messRep->create($chat,$user);
в данном случае это ничего не меняет и не имеет особого смысла
message model - activerecod. доп логики при сохранение не требуется.
если всё выносить и "отказаться" от логики activerecord то кол-ва кода резко увеличится в противоречие фреймворку
ну и вообще форвардинг и редактирование по разному можно обрабатывать
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ох... сча... для начала попробуем представить этот код как чуть менее грамоздкие действия (дабы понять почему у нас там столько условий и столько дублирования):
на лицо - не оптимизирована логика. Например стэп "создаем сообщение" присутствует во всех ветках. Можем спокойно вынести это последним пунктом в условии.
смотрим дальше. У нас все еще есть 3 ветки условий. Пользователя мы создаем в двух ветках и общее тут то что пользователя нет. Аналогично с чатами. Итого - можем упростить до двух веток которые независимы друг от друга.
Это то что тебе @bagart и предложил.
в этом нет смысла.