| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace CSlant\TelegramGitNotifier\Trait; |
||||||
| 4 | |||||||
| 5 | use CSlant\TelegramGitNotifier\Constants\EventConstant; |
||||||
| 6 | use CSlant\TelegramGitNotifier\Constants\SettingConstant; |
||||||
| 7 | |||||||
| 8 | trait EventSettingTrait |
||||||
| 9 | { |
||||||
| 10 | public function eventMarkup( |
||||||
| 11 | ?string $parentEvent = null, |
||||||
| 12 | string $platform = EventConstant::DEFAULT_PLATFORM, |
||||||
| 13 | string $platformFile = null |
||||||
| 14 | ): array { |
||||||
| 15 | $replyMarkup = $replyMarkupItem = []; |
||||||
| 16 | |||||||
| 17 | $this->setPlatFormForEvent($platform, $platformFile); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 18 | |||||||
| 19 | $events = $parentEvent === null ? $this->event->getEventConfig() |
||||||
| 20 | : $this->event->getEventConfig()[$parentEvent]; |
||||||
| 21 | |||||||
| 22 | foreach ($events as $key => $value) { |
||||||
| 23 | if (count($replyMarkupItem) === SettingConstant::BTN_LINE_ITEM_COUNT) { |
||||||
| 24 | $replyMarkup[] = $replyMarkupItem; |
||||||
| 25 | $replyMarkupItem = []; |
||||||
| 26 | } |
||||||
| 27 | |||||||
| 28 | $callbackData = $this->getCallbackData($key, $platform, $value, $parentEvent); |
||||||
| 29 | $eventName = $this->getEventName($key, $value); |
||||||
| 30 | |||||||
| 31 | $replyMarkupItem[] = $this->telegram->buildInlineKeyBoardButton( |
||||||
| 32 | $eventName, |
||||||
| 33 | '', |
||||||
| 34 | $callbackData |
||||||
| 35 | ); |
||||||
| 36 | } |
||||||
| 37 | |||||||
| 38 | if (count($replyMarkupItem) > 0) { |
||||||
| 39 | $replyMarkup[] = $replyMarkupItem; |
||||||
| 40 | } |
||||||
| 41 | |||||||
| 42 | $replyMarkup[] = $this->getEndKeyboard($platform, $parentEvent); |
||||||
| 43 | |||||||
| 44 | return $replyMarkup; |
||||||
| 45 | } |
||||||
| 46 | |||||||
| 47 | public function getCallbackData( |
||||||
| 48 | string $event, |
||||||
| 49 | string $platform, |
||||||
| 50 | array|bool $value = false, |
||||||
| 51 | ?string $parentEvent = null |
||||||
| 52 | ): string { |
||||||
| 53 | $platformSeparator = $platform === EventConstant::DEFAULT_PLATFORM |
||||||
| 54 | ? EventConstant::GITHUB_EVENT_SEPARATOR |
||||||
| 55 | : EventConstant::GITLAB_EVENT_SEPARATOR; |
||||||
| 56 | $prefix = EventConstant::EVENT_PREFIX . $platformSeparator; |
||||||
| 57 | |||||||
| 58 | if (is_array($value)) { |
||||||
| 59 | return $prefix . EventConstant::EVENT_HAS_ACTION_SEPARATOR . $event; |
||||||
| 60 | } elseif ($parentEvent) { |
||||||
| 61 | return $prefix . $parentEvent . '.' . $event |
||||||
| 62 | . EventConstant::EVENT_UPDATE_SEPARATOR; |
||||||
| 63 | } |
||||||
| 64 | |||||||
| 65 | return $prefix . $event . EventConstant::EVENT_UPDATE_SEPARATOR; |
||||||
| 66 | } |
||||||
| 67 | |||||||
| 68 | public function getEventName(string $event, bool|array $value = false): string |
||||||
| 69 | { |
||||||
| 70 | if (is_array($value)) { |
||||||
| 71 | return '⚙ ' . $event; |
||||||
| 72 | } elseif ($value) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 73 | return '✅ ' . $event; |
||||||
| 74 | } |
||||||
| 75 | |||||||
| 76 | return '❌ ' . $event; |
||||||
| 77 | } |
||||||
| 78 | |||||||
| 79 | public function getEndKeyboard( |
||||||
| 80 | string $platform, |
||||||
| 81 | ?string $parentEvent = null |
||||||
| 82 | ): array { |
||||||
| 83 | $back = SettingConstant::SETTING_BACK_TO_SETTINGS_MENU; |
||||||
| 84 | |||||||
| 85 | if ($parentEvent) { |
||||||
| 86 | $back = SettingConstant::SETTING_BACK_TO_EVENTS_MENU . ".$platform"; |
||||||
| 87 | } |
||||||
| 88 | |||||||
| 89 | return [ |
||||||
| 90 | $this->telegram->buildInlineKeyBoardButton('🔙 Back', '', $back), |
||||||
| 91 | $this->telegram->buildInlineKeyBoardButton( |
||||||
| 92 | '📚 Menu', |
||||||
| 93 | '', |
||||||
| 94 | SettingConstant::SETTING_BACK_TO_MAIN_MENU |
||||||
| 95 | ), |
||||||
| 96 | ]; |
||||||
| 97 | } |
||||||
| 98 | |||||||
| 99 | public function eventHandle( |
||||||
| 100 | ?string $callback = null, |
||||||
| 101 | ?string $platform = null |
||||||
| 102 | ): void { |
||||||
| 103 | $platform = $this->getPlatformFromCallback($callback, $platform); |
||||||
| 104 | |||||||
| 105 | if ($this->sendSettingEventMessage($platform, $callback)) { |
||||||
| 106 | return; |
||||||
| 107 | } |
||||||
| 108 | |||||||
| 109 | $event = $this->getEventFromCallback($callback); |
||||||
| 110 | |||||||
| 111 | if ($this->handleEventWithActions($event, $platform)) { |
||||||
| 112 | return; |
||||||
| 113 | } |
||||||
| 114 | |||||||
| 115 | $this->handleEventUpdate($event, $platform); |
||||||
| 116 | } |
||||||
| 117 | |||||||
| 118 | public function getPlatformFromCallback( |
||||||
| 119 | ?string $callback, |
||||||
| 120 | ?string $platform |
||||||
| 121 | ): string { |
||||||
| 122 | if ($platform) { |
||||||
| 123 | return $platform; |
||||||
| 124 | } |
||||||
| 125 | |||||||
| 126 | if ($callback && str_contains($callback, EventConstant::GITHUB_EVENT_SEPARATOR)) { |
||||||
| 127 | return 'github'; |
||||||
| 128 | } elseif ($callback && str_contains($callback, EventConstant::GITLAB_EVENT_SEPARATOR)) { |
||||||
| 129 | return 'gitlab'; |
||||||
| 130 | } |
||||||
| 131 | |||||||
| 132 | return EventConstant::DEFAULT_PLATFORM; |
||||||
| 133 | } |
||||||
| 134 | |||||||
| 135 | public function sendSettingEventMessage( |
||||||
| 136 | string $platform, |
||||||
| 137 | ?string $callback = null, |
||||||
| 138 | ?string $view = null |
||||||
| 139 | ): bool { |
||||||
| 140 | if (SettingConstant::SETTING_GITHUB_EVENTS === $callback |
||||||
| 141 | || SettingConstant::SETTING_GITLAB_EVENTS === $callback |
||||||
| 142 | || !$callback |
||||||
| 143 | ) { |
||||||
| 144 | $this->editMessageText( |
||||||
|
0 ignored issues
–
show
It seems like
editMessageText() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 145 | tgn_view( |
||||||
| 146 | $view ?? config('telegram-git-notifier.view.tools.custom_event'), |
||||||
| 147 | compact('platform') |
||||||
| 148 | ), |
||||||
| 149 | ['reply_markup' => $this->eventMarkup(null, $platform)] |
||||||
| 150 | ); |
||||||
| 151 | |||||||
| 152 | return true; |
||||||
| 153 | } |
||||||
| 154 | |||||||
| 155 | return false; |
||||||
| 156 | } |
||||||
| 157 | |||||||
| 158 | public function getEventFromCallback(?string $callback): string |
||||||
| 159 | { |
||||||
| 160 | if (!$callback) { |
||||||
| 161 | return ''; |
||||||
| 162 | } |
||||||
| 163 | |||||||
| 164 | return str_replace([ |
||||||
| 165 | EventConstant::EVENT_PREFIX, |
||||||
| 166 | EventConstant::GITHUB_EVENT_SEPARATOR, |
||||||
| 167 | EventConstant::GITLAB_EVENT_SEPARATOR, |
||||||
| 168 | ], '', $callback); |
||||||
| 169 | } |
||||||
| 170 | |||||||
| 171 | public function handleEventWithActions( |
||||||
| 172 | string $event, |
||||||
| 173 | string $platform, |
||||||
| 174 | ?string $view = null |
||||||
| 175 | ): bool { |
||||||
| 176 | if (str_contains($event, EventConstant::EVENT_HAS_ACTION_SEPARATOR)) { |
||||||
| 177 | $event = str_replace( |
||||||
| 178 | EventConstant::EVENT_HAS_ACTION_SEPARATOR, |
||||||
| 179 | '', |
||||||
| 180 | $event |
||||||
| 181 | ); |
||||||
| 182 | $this->editMessageText( |
||||||
| 183 | tgn_view( |
||||||
| 184 | $view ?? config('telegram-git-notifier.view.tools.custom_event_action'), |
||||||
| 185 | compact('event', 'platform') |
||||||
| 186 | ), |
||||||
| 187 | ['reply_markup' => $this->eventMarkup($event, $platform)] |
||||||
| 188 | ); |
||||||
| 189 | |||||||
| 190 | return true; |
||||||
| 191 | } |
||||||
| 192 | |||||||
| 193 | return false; |
||||||
| 194 | } |
||||||
| 195 | |||||||
| 196 | public function handleEventUpdate( |
||||||
| 197 | string $event, |
||||||
| 198 | string $platform |
||||||
| 199 | ): void { |
||||||
| 200 | if (str_contains($event, EventConstant::EVENT_UPDATE_SEPARATOR)) { |
||||||
| 201 | $event = str_replace( |
||||||
| 202 | EventConstant::EVENT_UPDATE_SEPARATOR, |
||||||
| 203 | '', |
||||||
| 204 | $event |
||||||
| 205 | ); |
||||||
| 206 | $this->eventUpdateHandle($event, $platform); |
||||||
| 207 | } |
||||||
| 208 | } |
||||||
| 209 | |||||||
| 210 | public function eventUpdateHandle( |
||||||
| 211 | string $event, |
||||||
| 212 | string $platform |
||||||
| 213 | ): void { |
||||||
| 214 | if (str_contains($event, '.')) { |
||||||
| 215 | [$event, $action] = explode('.', $event); |
||||||
| 216 | } else { |
||||||
| 217 | $action = null; |
||||||
| 218 | } |
||||||
| 219 | |||||||
| 220 | $this->setPlatFormForEvent($platform); |
||||||
| 221 | $this->event->updateEvent($event, $action); |
||||||
| 222 | $this->eventHandle( |
||||||
| 223 | $action |
||||||
| 224 | ? EventConstant::PLATFORM_EVENT_SEPARATOR[$platform] |
||||||
| 225 | . EventConstant::EVENT_HAS_ACTION_SEPARATOR . $event |
||||||
| 226 | : null, |
||||||
| 227 | $platform |
||||||
| 228 | ); |
||||||
| 229 | } |
||||||
| 230 | } |
||||||
| 231 |