Skip to content

Commit

Permalink
Merge pull request #29 from tanhongit/main
Browse files Browse the repository at this point in the history
Update exceptions
  • Loading branch information
tanhongit authored Nov 7, 2023
2 parents ef2030f + f7439f1 commit a17612b
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 30 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Lbil Technologies
Copyright (c) 2023 CSlant

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
26 changes: 24 additions & 2 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CSlant\TelegramGitNotifier;

use CSlant\TelegramGitNotifier\Constants\EventConstant;
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use CSlant\TelegramGitNotifier\Interfaces\BotInterface;
use CSlant\TelegramGitNotifier\Interfaces\EventInterface;
use CSlant\TelegramGitNotifier\Interfaces\SettingInterface;
Expand All @@ -29,6 +30,17 @@ class Bot implements AppInterface, BotInterface, EventInterface, SettingInterfac

public Setting $setting;

/**
* @param Telegram|null $telegram
* @param string|null $chatBotId
* @param Event|null $event
* @param string|null $platform
* @param string|null $platformFile
* @param Setting|null $setting
* @param string|null $settingFile
*
* @throws ConfigFileException
*/
public function __construct(
Telegram $telegram = null,
?string $chatBotId = null,
Expand All @@ -38,12 +50,22 @@ public function __construct(
Setting $setting = null,
?string $settingFile = null,
) {
$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
$this->setCurrentChatBotId($chatBotId);
$this->event = $event ?? new Event();
$this->setPlatFormForEvent($platform, $platformFile);
$this->validatePlatformFile();

$this->setting = $setting ?? new Setting();
$this->updateSetting($settingFile);
$this->validateSettingFile();

$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
$this->setCurrentChatBotId($chatBotId);
}

public function validateSettingFile(): void
{
if (empty($this->setting->getSettingFile())) {
throw ConfigFileException::settingFile($this->setting->getSettingFile());
}
}
}
16 changes: 16 additions & 0 deletions src/Exceptions/ConfigFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace CSlant\TelegramGitNotifier\Exceptions;

final class ConfigFileException extends TelegramGitNotifierException
{
public static function settingFile(?string $settingFile = null): self
{
return new self('Something went wrong while reading settings file. Check your settings file path: ' . ($settingFile ?? 'null'));
}

public static function platformFile(?string $platform = null, ?string $platformFile = null): self
{
return new self('Something went wrong while reading platform file. Check your platform file path: ' . ($platformFile ?? 'null') . ' for platform: ' . ($platform ?? 'null'));
}
}
4 changes: 2 additions & 2 deletions src/Exceptions/SendNotificationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

final class SendNotificationException extends TelegramGitNotifierException
{
public static function create(): self
public static function create(?string $exception = null): self
{
return new self('Can\'t send notification');
return new self('Can\'t send notification. ' . ($exception ?? ''));
}
}
26 changes: 26 additions & 0 deletions src/Exceptions/WebhookException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace CSlant\TelegramGitNotifier\Exceptions;

final class WebhookException extends TelegramGitNotifierException
{
public static function set(): self
{
return new self('Something went wrong while setting webhook. Check your bot token and app url!');
}

public static function delete(): self
{
return new self('Something went wrong while deleting webhook. Check your bot token and app url!');
}

public static function getUpdates(): self
{
return new self('Something went wrong while getting updates. Check your bot token and app url!');
}

public static function getWebHookInfo(): self
{
return new self('Something went wrong while getting webhook info. Check your bot token and app url!');
}
}
7 changes: 7 additions & 0 deletions src/Interfaces/BotInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CSlant\TelegramGitNotifier\Interfaces;

use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;

interface BotInterface
Expand Down Expand Up @@ -46,4 +47,10 @@ public function isOwner(): bool;
* @return bool
*/
public function isNotifyChat(): bool;

/**
* @return void
* @throws ConfigFileException
*/
public function validateSettingFile(): void;
}
9 changes: 8 additions & 1 deletion src/Interfaces/EventInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CSlant\TelegramGitNotifier\Interfaces;

use CSlant\TelegramGitNotifier\Constants\EventConstant;
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use CSlant\TelegramGitNotifier\Trait\ActionEventTrait;
use Symfony\Component\HttpFoundation\Request;

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

/**
* Set event config and get event name
Expand All @@ -38,4 +39,10 @@ public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_P
* @see EventTrait::handleEventFromRequest()
*/
public function handleEventFromRequest(Request $request): ?string;

/**
* @return void
* @throws ConfigFileException
*/
public function validatePlatformFile(): void;
}
22 changes: 14 additions & 8 deletions src/Interfaces/WebhookInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace CSlant\TelegramGitNotifier\Interfaces;

use CSlant\TelegramGitNotifier\Exceptions\WebhookException;

interface WebhookInterface
{
/**
Expand All @@ -25,28 +27,32 @@ public function setUrl(string $url): void;
/**
* Set webhook for telegram bot
*
* @return false|string
* @return string
* @throws WebhookException
*/
public function setWebhook(): false|string;
public function setWebhook(): string;

/**
* Delete webhook for telegram bot
*
* @return false|string
* @return string
* @throws WebhookException
*/
public function deleteWebHook(): false|string;
public function deleteWebHook(): string;

/**
* Get webhook info
*
* @return false|string
* @return string
* @throws WebhookException
*/
public function getWebHookInfo(): false|string;
public function getWebHookInfo(): string;

/**
* Get webhook update
*
* @return false|string
* @return string
* @throws WebhookException
*/
public function getUpdates(): false|string;
public function getUpdates(): string;
}
18 changes: 15 additions & 3 deletions src/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CSlant\TelegramGitNotifier\Constants\EventConstant;
use CSlant\TelegramGitNotifier\Constants\NotificationConstant;
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use CSlant\TelegramGitNotifier\Interfaces\EventInterface;
use CSlant\TelegramGitNotifier\Interfaces\Structures\AppInterface;
use CSlant\TelegramGitNotifier\Interfaces\Structures\NotificationInterface;
Expand All @@ -25,6 +26,16 @@ class Notifier implements AppInterface, NotificationInterface, EventInterface

public Client $client;

/**
* @param Telegram|null $telegram
* @param string|null $chatBotId
* @param Event|null $event
* @param string|null $platform
* @param string|null $platformFile
* @param Client|null $client
*
* @throws ConfigFileException
*/
public function __construct(
Telegram $telegram = null,
?string $chatBotId = null,
Expand All @@ -33,11 +44,12 @@ public function __construct(
?string $platformFile = null,
Client $client = null,
) {
$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
$this->setCurrentChatBotId($chatBotId);

$this->event = $event ?? new Event();
$this->setPlatFormForEvent($platform, $platformFile);
$this->validatePlatformFile();

$this->telegram = $telegram ?? new Telegram(config('telegram-git-notifier.bot.token'));
$this->setCurrentChatBotId($chatBotId);

$this->client = $client ?? new Client();
}
Expand Down
8 changes: 4 additions & 4 deletions src/Structures/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public function sendNotify(string $message = null, array $options = []): bool
return true;
}

throw SendNotificationException::create();
$body = (string) $response->getBody();

throw SendNotificationException::create($body);
} catch (GuzzleException $e) {
error_log($e->getMessage());
throw SendNotificationException::create($e->getMessage());
}

return false;
}
}
13 changes: 12 additions & 1 deletion src/Trait/EventTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace CSlant\TelegramGitNotifier\Trait;

use CSlant\TelegramGitNotifier\Constants\EventConstant;
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
use Symfony\Component\HttpFoundation\Request;

trait EventTrait
{
use ActionEventTrait;

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

return null;
}

public function validatePlatformFile(): void
{
if (empty($this->event->getEventConfig())) {
throw ConfigFileException::platformFile(
$this->event->platform,
$this->event->getPlatformFile()
);
}
}
}
50 changes: 42 additions & 8 deletions src/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@

namespace CSlant\TelegramGitNotifier;

use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
use CSlant\TelegramGitNotifier\Interfaces\WebhookInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class Webhook implements WebhookInterface
{
private string $token;

private string $url;

private Client $client;

public function __construct()
{
$this->client = new Client();
}

public function setToken(string $token): void
{
$this->token = $token;
Expand All @@ -20,31 +30,55 @@ public function setUrl(string $url): void
$this->url = $url;
}

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

return file_get_contents($url);
try {
$response = $this->client->request('GET', $url);

return $response->getBody()->getContents();
} catch (GuzzleException) {
throw WebhookException::set();
}
}

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

return file_get_contents($url);
try {
$response = $this->client->request('GET', $url);

return $response->getBody()->getContents();
} catch (GuzzleException) {
throw WebhookException::delete();
}
}

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

return file_get_contents($url);
try {
$response = $this->client->request('GET', $url);

return $response->getBody()->getContents();
} catch (GuzzleException) {
throw WebhookException::getWebHookInfo();
}
}

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

return file_get_contents($url);
try {
$response = $this->client->request('GET', $url);

return $response->getBody()->getContents();
} catch (GuzzleException) {
throw WebhookException::getUpdates();
}
}
}
Loading

0 comments on commit a17612b

Please sign in to comment.