Skip to content

Commit 461f74a

Browse files
authored
Merge pull request #4 from m4n50n/develop
Refactor: Minor code improvements
2 parents 225ebd3 + 2eae9e8 commit 461f74a

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

src/Config/TelegramBotConfig.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,22 @@ public function limiter(): array
126126
{
127127
return $this->limiter;
128128
}
129+
130+
/**
131+
* {@inheritDoc}
132+
*/
133+
public function serialize(): array
134+
{
135+
return [
136+
'key' => $this->key,
137+
'username' => $this->username,
138+
'webhook' => $this->webhook,
139+
'commands' => $this->commands,
140+
'admins' => $this->admins,
141+
'mysql' => $this->mysql,
142+
'log' => $this->log,
143+
'paths' => $this->paths,
144+
'limiter' => $this->limiter,
145+
];
146+
}
129147
}

src/Config/TelegramBotConfigInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,11 @@ public function paths(): array;
9393
* @return array
9494
*/
9595
public function limiter(): array;
96+
97+
/**
98+
* Serialize the object to an array.
99+
*
100+
* @return array
101+
*/
102+
public function serialize(): array;
96103
}

src/Core/TelegramBot.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public function __construct(private TelegramBotConfig $config)
4141
$this->initialize();
4242
}
4343

44+
/**
45+
* {@inheritDoc}
46+
*/
47+
public function getConfig(): array
48+
{
49+
return $this->config->serialize();
50+
}
51+
4452
/**
4553
* {@inheritDoc}
4654
*/
@@ -55,7 +63,7 @@ public function initialize(): Telegram
5563

5664
return $this->bot;
5765
} catch (TelegramException | TelegramLogException | \Exception $exception) {
58-
throw new \Exception(sprintf("'%s'", $exception->getMessage()));
66+
throw new \Exception(sprintf("%s", $exception->getMessage()));
5967
}
6068
}
6169

@@ -153,7 +161,7 @@ public function webhookHandler(): void
153161
try {
154162
$this->bot->handle();
155163
} catch (TelegramException | \Exception $exception) {
156-
throw new \Exception(sprintf("'%s'", $exception->getMessage()));
164+
throw new \Exception(sprintf("%s", $exception->getMessage()));
157165
}
158166
}
159167

@@ -165,7 +173,7 @@ public function setWebhook(): ServerResponse
165173
try {
166174
return $this->bot->setWebhook($this->config->webhook()["url"]);
167175
} catch (TelegramException | \Exception $exception) {
168-
throw new \Exception(sprintf("'%s'", $exception->getMessage()));
176+
throw new \Exception(sprintf("%s", $exception->getMessage()));
169177
}
170178
}
171179

@@ -177,7 +185,7 @@ public function unsetWebhook(): ServerResponse
177185
try {
178186
return $this->bot->deleteWebhook();
179187
} catch (TelegramException | \Exception $exception) {
180-
throw new \Exception(sprintf("'%s'", $exception->getMessage()));
188+
throw new \Exception(sprintf("%s", $exception->getMessage()));
181189
}
182190
}
183191
}

src/Core/TelegramBotInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
*/
2929
interface TelegramBotInterface
3030
{
31+
/**
32+
* Get the configuration array of the specified bot.
33+
*
34+
* @return array
35+
*/
36+
public function getConfig(): array;
37+
3138
/**
3239
* Start of the process of creating a new bot instance.
3340
*

src/Resources/Service/TelegramBotService.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
<?php
22

3+
/**
4+
* This file is part of the TelegramBotBundle package.
5+
*
6+
* (c) Jose Clemente García Rodríguez aka m4n50n <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @author Jose Clemente García Rodríguez <[email protected]>
12+
* @link https://github.com/m4n50n/telegram_bot_bundle
13+
*/
14+
315
namespace M4n50n\TelegramBotBundle\Resources\Service;
416

517
use M4n50n\TelegramBotBundle\Factory\TelegramBotFactory;

0 commit comments

Comments
 (0)