Skip to content

Commit f956cce

Browse files
authored
Merge f7439f1 into ef2030f
2 parents ef2030f + f7439f1 commit f956cce

13 files changed

+185
-30
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 Lbil Technologies
3+
Copyright (c) 2023 CSlant
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

src/Bot.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CSlant\TelegramGitNotifier;
44

55
use CSlant\TelegramGitNotifier\Constants\EventConstant;
6+
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
67
use CSlant\TelegramGitNotifier\Interfaces\BotInterface;
78
use CSlant\TelegramGitNotifier\Interfaces\EventInterface;
89
use CSlant\TelegramGitNotifier\Interfaces\SettingInterface;
@@ -29,6 +30,17 @@ class Bot implements AppInterface, BotInterface, EventInterface, SettingInterfac
2930

3031
public Setting $setting;
3132

33+
/**
34+
* @param Telegram|null $telegram
35+
* @param string|null $chatBotId
36+
* @param Event|null $event
37+
* @param string|null $platform
38+
* @param string|null $platformFile
39+
* @param Setting|null $setting
40+
* @param string|null $settingFile
41+
*
42+
* @throws ConfigFileException
43+
*/
3244
public function __construct(
3345
Telegram $telegram = null,
3446
?string $chatBotId = null,
@@ -38,12 +50,22 @@ public function __construct(
3850
Setting $setting = null,
3951
?string $settingFile = null,
4052
) {
41-
$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
42-
$this->setCurrentChatBotId($chatBotId);
4353
$this->event = $event ?? new Event();
4454
$this->setPlatFormForEvent($platform, $platformFile);
55+
$this->validatePlatformFile();
4556

4657
$this->setting = $setting ?? new Setting();
4758
$this->updateSetting($settingFile);
59+
$this->validateSettingFile();
60+
61+
$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
62+
$this->setCurrentChatBotId($chatBotId);
63+
}
64+
65+
public function validateSettingFile(): void
66+
{
67+
if (empty($this->setting->getSettingFile())) {
68+
throw ConfigFileException::settingFile($this->setting->getSettingFile());
69+
}
4870
}
4971
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace CSlant\TelegramGitNotifier\Exceptions;
4+
5+
final class ConfigFileException extends TelegramGitNotifierException
6+
{
7+
public static function settingFile(?string $settingFile = null): self
8+
{
9+
return new self('Something went wrong while reading settings file. Check your settings file path: ' . ($settingFile ?? 'null'));
10+
}
11+
12+
public static function platformFile(?string $platform = null, ?string $platformFile = null): self
13+
{
14+
return new self('Something went wrong while reading platform file. Check your platform file path: ' . ($platformFile ?? 'null') . ' for platform: ' . ($platform ?? 'null'));
15+
}
16+
}

src/Exceptions/SendNotificationException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
final class SendNotificationException extends TelegramGitNotifierException
66
{
7-
public static function create(): self
7+
public static function create(?string $exception = null): self
88
{
9-
return new self('Can\'t send notification');
9+
return new self('Can\'t send notification. ' . ($exception ?? ''));
1010
}
1111
}

src/Exceptions/WebhookException.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace CSlant\TelegramGitNotifier\Exceptions;
4+
5+
final class WebhookException extends TelegramGitNotifierException
6+
{
7+
public static function set(): self
8+
{
9+
return new self('Something went wrong while setting webhook. Check your bot token and app url!');
10+
}
11+
12+
public static function delete(): self
13+
{
14+
return new self('Something went wrong while deleting webhook. Check your bot token and app url!');
15+
}
16+
17+
public static function getUpdates(): self
18+
{
19+
return new self('Something went wrong while getting updates. Check your bot token and app url!');
20+
}
21+
22+
public static function getWebHookInfo(): self
23+
{
24+
return new self('Something went wrong while getting webhook info. Check your bot token and app url!');
25+
}
26+
}

src/Interfaces/BotInterface.php

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

33
namespace CSlant\TelegramGitNotifier\Interfaces;
44

5+
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
56
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
67

78
interface BotInterface
@@ -46,4 +47,10 @@ public function isOwner(): bool;
4647
* @return bool
4748
*/
4849
public function isNotifyChat(): bool;
50+
51+
/**
52+
* @return void
53+
* @throws ConfigFileException
54+
*/
55+
public function validateSettingFile(): void;
4956
}

src/Interfaces/EventInterface.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CSlant\TelegramGitNotifier\Interfaces;
44

55
use CSlant\TelegramGitNotifier\Constants\EventConstant;
6+
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
67
use CSlant\TelegramGitNotifier\Trait\ActionEventTrait;
78
use Symfony\Component\HttpFoundation\Request;
89

@@ -27,7 +28,7 @@ public function getActionOfEvent(object $payload): string;
2728
* @return void
2829
* @see EventTrait::setPlatFormForEvent()
2930
*/
30-
public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, string $platformFile = null): void;
31+
public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, ?string $platformFile = null): void;
3132

3233
/**
3334
* Set event config and get event name
@@ -38,4 +39,10 @@ public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_P
3839
* @see EventTrait::handleEventFromRequest()
3940
*/
4041
public function handleEventFromRequest(Request $request): ?string;
42+
43+
/**
44+
* @return void
45+
* @throws ConfigFileException
46+
*/
47+
public function validatePlatformFile(): void;
4148
}

src/Interfaces/WebhookInterface.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace CSlant\TelegramGitNotifier\Interfaces;
44

5+
use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
6+
57
interface WebhookInterface
68
{
79
/**
@@ -25,28 +27,32 @@ public function setUrl(string $url): void;
2527
/**
2628
* Set webhook for telegram bot
2729
*
28-
* @return false|string
30+
* @return string
31+
* @throws WebhookException
2932
*/
30-
public function setWebhook(): false|string;
33+
public function setWebhook(): string;
3134

3235
/**
3336
* Delete webhook for telegram bot
3437
*
35-
* @return false|string
38+
* @return string
39+
* @throws WebhookException
3640
*/
37-
public function deleteWebHook(): false|string;
41+
public function deleteWebHook(): string;
3842

3943
/**
4044
* Get webhook info
4145
*
42-
* @return false|string
46+
* @return string
47+
* @throws WebhookException
4348
*/
44-
public function getWebHookInfo(): false|string;
49+
public function getWebHookInfo(): string;
4550

4651
/**
4752
* Get webhook update
4853
*
49-
* @return false|string
54+
* @return string
55+
* @throws WebhookException
5056
*/
51-
public function getUpdates(): false|string;
57+
public function getUpdates(): string;
5258
}

src/Notifier.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CSlant\TelegramGitNotifier\Constants\EventConstant;
66
use CSlant\TelegramGitNotifier\Constants\NotificationConstant;
7+
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
78
use CSlant\TelegramGitNotifier\Interfaces\EventInterface;
89
use CSlant\TelegramGitNotifier\Interfaces\Structures\AppInterface;
910
use CSlant\TelegramGitNotifier\Interfaces\Structures\NotificationInterface;
@@ -25,6 +26,16 @@ class Notifier implements AppInterface, NotificationInterface, EventInterface
2526

2627
public Client $client;
2728

29+
/**
30+
* @param Telegram|null $telegram
31+
* @param string|null $chatBotId
32+
* @param Event|null $event
33+
* @param string|null $platform
34+
* @param string|null $platformFile
35+
* @param Client|null $client
36+
*
37+
* @throws ConfigFileException
38+
*/
2839
public function __construct(
2940
Telegram $telegram = null,
3041
?string $chatBotId = null,
@@ -33,11 +44,12 @@ public function __construct(
3344
?string $platformFile = null,
3445
Client $client = null,
3546
) {
36-
$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
37-
$this->setCurrentChatBotId($chatBotId);
38-
3947
$this->event = $event ?? new Event();
4048
$this->setPlatFormForEvent($platform, $platformFile);
49+
$this->validatePlatformFile();
50+
51+
$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
52+
$this->setCurrentChatBotId($chatBotId);
4153

4254
$this->client = $client ?? new Client();
4355
}

src/Structures/Notification.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ public function sendNotify(string $message = null, array $options = []): bool
9494
return true;
9595
}
9696

97-
throw SendNotificationException::create();
97+
$body = (string) $response->getBody();
98+
99+
throw SendNotificationException::create($body);
98100
} catch (GuzzleException $e) {
99-
error_log($e->getMessage());
101+
throw SendNotificationException::create($e->getMessage());
100102
}
101-
102-
return false;
103103
}
104104
}

src/Trait/EventTrait.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
namespace CSlant\TelegramGitNotifier\Trait;
44

55
use CSlant\TelegramGitNotifier\Constants\EventConstant;
6+
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
67
use Symfony\Component\HttpFoundation\Request;
78

89
trait EventTrait
910
{
1011
use ActionEventTrait;
1112

12-
public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, string $platformFile = null): void
13+
public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, ?string $platformFile = null): void
1314
{
1415
/** @var array $platformFileDefaults<platform, platformFile> */
1516
$platformFileDefaults = config('telegram-git-notifier.data_file.platform');
@@ -31,4 +32,14 @@ public function handleEventFromRequest(Request $request): ?string
3132

3233
return null;
3334
}
35+
36+
public function validatePlatformFile(): void
37+
{
38+
if (empty($this->event->getEventConfig())) {
39+
throw ConfigFileException::platformFile(
40+
$this->event->platform,
41+
$this->event->getPlatformFile()
42+
);
43+
}
44+
}
3445
}

src/Webhook.php

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22

33
namespace CSlant\TelegramGitNotifier;
44

5+
use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
56
use CSlant\TelegramGitNotifier\Interfaces\WebhookInterface;
7+
use GuzzleHttp\Client;
8+
use GuzzleHttp\Exception\GuzzleException;
69

710
class Webhook implements WebhookInterface
811
{
912
private string $token;
1013

1114
private string $url;
1215

16+
private Client $client;
17+
18+
public function __construct()
19+
{
20+
$this->client = new Client();
21+
}
22+
1323
public function setToken(string $token): void
1424
{
1525
$this->token = $token;
@@ -20,31 +30,55 @@ public function setUrl(string $url): void
2030
$this->url = $url;
2131
}
2232

23-
public function setWebhook(): false|string
33+
public function setWebhook(): string
2434
{
2535
$url = "https://api.telegram.org/bot{$this->token}/setWebhook?url={$this->url}";
2636

27-
return file_get_contents($url);
37+
try {
38+
$response = $this->client->request('GET', $url);
39+
40+
return $response->getBody()->getContents();
41+
} catch (GuzzleException) {
42+
throw WebhookException::set();
43+
}
2844
}
2945

30-
public function deleteWebHook(): false|string
46+
public function deleteWebHook(): string
3147
{
3248
$url = "https://api.telegram.org/bot{$this->token}/deleteWebhook";
3349

34-
return file_get_contents($url);
50+
try {
51+
$response = $this->client->request('GET', $url);
52+
53+
return $response->getBody()->getContents();
54+
} catch (GuzzleException) {
55+
throw WebhookException::delete();
56+
}
3557
}
3658

37-
public function getWebHookInfo(): false|string
59+
public function getWebHookInfo(): string
3860
{
3961
$url = "https://api.telegram.org/bot{$this->token}/getWebhookInfo";
4062

41-
return file_get_contents($url);
63+
try {
64+
$response = $this->client->request('GET', $url);
65+
66+
return $response->getBody()->getContents();
67+
} catch (GuzzleException) {
68+
throw WebhookException::getWebHookInfo();
69+
}
4270
}
4371

44-
public function getUpdates(): false|string
72+
public function getUpdates(): string
4573
{
4674
$url = "https://api.telegram.org/bot{$this->token}/getUpdates";
4775

48-
return file_get_contents($url);
76+
try {
77+
$response = $this->client->request('GET', $url);
78+
79+
return $response->getBody()->getContents();
80+
} catch (GuzzleException) {
81+
throw WebhookException::getUpdates();
82+
}
4983
}
5084
}

0 commit comments

Comments
 (0)