Skip to content

Commit a889275

Browse files
authored
Merge pull request #11 from mohammadqasemi/refactor/get-topic-id-from-command-job-and-route
feat(TelegramBotHandler): Separate logic for command, job, and route …
2 parents 892638a + 89ba330 commit a889275

File tree

1 file changed

+57
-15
lines changed

1 file changed

+57
-15
lines changed

src/TelegramBotHandler.php

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace TheCoder\MonologTelegram;
44

5-
use GuzzleHttp\Client;
65
use GuzzleHttp\Exception\GuzzleException;
76
use Illuminate\Routing\Router;
87
use Illuminate\Support\Facades\Route;
@@ -71,7 +70,7 @@ public function __construct(
7170
string $chat_id,
7271
?string $topic_id = null,
7372
?string $queue = null,
74-
$topics_level = [],
73+
$topics_level = [],
7574
$level = Logger::DEBUG,
7675
bool $bubble = true,
7776
$bot_api = 'https://api.telegram.org/bot',
@@ -97,7 +96,7 @@ public function __construct(
9796
*/
9897
protected function write($record): void
9998
{
100-
$topicId = $this->getTopicByAttribute();
99+
$topicId = $this->getTopicByAttribute($record);
101100
$token = $record['context']['token'] ?? null;
102101
$chatId = $record['context']['chat_id'] ?? null;
103102
$topicId = $topicId ?? $record['context']['topic_id'] ?? null;
@@ -141,35 +140,78 @@ protected function send(string $message, $token = null, $chatId = null, $topicId
141140
}
142141
}
143142

144-
protected function getTopicByAttribute(): string|null
143+
protected function getTopicByAttribute($record): string|null
145144
{
146-
$route = Route::current();
147-
if ($route == null) {
145+
if (isset($record['context']['exception'])) {
146+
$trace = $record['context']['exception']->getTrace();
147+
148+
$commandClass = $this->getClassForCommand($trace);
149+
if ($commandClass) {
150+
return $this->getTopicIdByReflection($commandClass, 'handle');
151+
}
152+
153+
$jobClass = $this->getClassForJob($trace);
154+
if ($jobClass) {
155+
return $this->getTopicIdByReflection($jobClass, 'handle');
156+
}
157+
}
158+
159+
return $this->getTopicByRoute();
160+
}
161+
162+
protected function getClassForCommand(array $trace): ?string
163+
{
164+
if (!app()->runningInConsole()) {
148165
return null;
149166
}
150167

151-
$action = $route->getAction();
168+
foreach ($trace as $frame) {
169+
if ($frame['function'] === 'handle' && isset($frame['class']) && str_contains($frame['class'], 'Console\Commands')) {
170+
return $frame['class'];
171+
}
172+
}
173+
174+
return null;
175+
}
152176

153-
if (!isset($action['controller'])) {
177+
protected function getClassForJob(array $trace): ?string
178+
{
179+
if (!app()->bound('queue.worker')) {
180+
return null;
181+
}
182+
183+
foreach ($trace as $frame) {
184+
if ($frame['function'] === 'handle' && isset($frame['class']) && str_contains($frame['class'], 'App\Jobs')) {
185+
return $frame['class'];
186+
}
187+
}
188+
189+
return null;
190+
}
191+
192+
protected function getTopicByRoute(): string|null
193+
{
194+
$route = Route::current();
195+
if (!$route || !isset($route->getAction()['controller'])) {
154196
return null;
155197
}
156198

157-
$topicId = $this->getTopicIdByReflection($action);
199+
[$controller, $method] = explode('@', $route->getAction()['controller']);
200+
201+
$topicId = $this->getTopicIdByReflection($controller, $method);
158202
if ($topicId === false) {
159-
$topicId = $this->getTopicIdByRegex($action);
203+
$topicId = $this->getTopicIdByRegex($route->getAction());
160204
}
161205

162206
return $topicId;
163207
}
164208

165-
protected function getTopicIdByReflection($action): bool|string|null
209+
protected function getTopicIdByReflection($class, $method): bool|string|null
166210
{
167211
try {
168-
[$controller, $method] = explode('@', $action['controller']);
169-
$reflectionMethod = new ReflectionMethod($controller, $method);
212+
$reflectionMethod = new ReflectionMethod($class, $method);
170213

171214
$attributes = $reflectionMethod->getAttributes();
172-
$attributes[0]?->newInstance() ?? null;
173215

174216
if ($attributes[0] !== null) {
175217
/** @var TopicLogInterface $notifyException */
@@ -186,7 +228,7 @@ protected function getTopicIdByReflection($action): bool|string|null
186228
protected function getTopicIdByRegex($action)
187229
{
188230
try {
189-
// if reflection coud not get attribute use reagex instead
231+
// if reflection could not get attribute use regex instead
190232
[$controller, $method] = explode('@', $action['controller']);
191233

192234
$filePath = base_path(str_replace('App', 'app', $controller) . '.php');

0 commit comments

Comments
 (0)