Skip to content

Commit 5fe979c

Browse files
tanhongitCopilot
andcommitted
refactor: migrate platform constants from EventConstant to Platform enum
- Move DEFAULT_PLATFORM, WEBHOOK_EVENT_HEADER, event separators, and PLATFORM_EVENT_SEPARATOR into Platform enum methods - Add Platform::DEFAULT const for use in default parameter values - Remove platformEnum() static helper (use Platform::from() directly) - EventConstant now only contains callback serialization constants - SettingConstant uses hardcoded separator strings (const context) - All 16 tests passing Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9a091aa commit 5fe979c

11 files changed

Lines changed: 40 additions & 58 deletions

File tree

src/Bot.php

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

33
namespace CSlant\TelegramGitNotifier;
44

5-
use CSlant\TelegramGitNotifier\Constants\EventConstant;
5+
use CSlant\TelegramGitNotifier\Enums\Platform;
66
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
77
use CSlant\TelegramGitNotifier\Interfaces\BotInterface;
88
use CSlant\TelegramGitNotifier\Models\Event;
@@ -42,7 +42,7 @@ public function __construct(
4242
?Telegram $telegram = null,
4343
?string $chatBotId = null,
4444
?Event $event = null,
45-
?string $platform = EventConstant::DEFAULT_PLATFORM,
45+
?string $platform = Platform::DEFAULT,
4646
?string $platformFile = null,
4747
?Setting $setting = null,
4848
?string $settingFile = null,

src/Constants/EventConstant.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,11 @@
22

33
namespace CSlant\TelegramGitNotifier\Constants;
44

5-
use CSlant\TelegramGitNotifier\Enums\Platform;
6-
75
final class EventConstant
86
{
9-
public const DEFAULT_PLATFORM = 'github';
10-
11-
public const WEBHOOK_EVENT_HEADER = [
12-
'github' => 'HTTP_X_GITHUB_EVENT',
13-
'gitlab' => 'HTTP_X_GITLAB_EVENT',
14-
];
15-
167
public const EVENT_PREFIX = SettingConstant::SETTING_CUSTOM_EVENTS . '.evt.';
178

18-
public const GITHUB_EVENT_SEPARATOR = 'gh.';
19-
20-
public const GITLAB_EVENT_SEPARATOR = 'gl.';
21-
229
public const EVENT_HAS_ACTION_SEPARATOR = 'atc.';
2310

2411
public const EVENT_UPDATE_SEPARATOR = '.eus';
25-
26-
public const PLATFORM_EVENT_SEPARATOR = [
27-
'github' => self::GITHUB_EVENT_SEPARATOR,
28-
'gitlab' => self::GITLAB_EVENT_SEPARATOR,
29-
];
30-
31-
public static function platformEnum(string $platform): Platform
32-
{
33-
return Platform::from($platform);
34-
}
3512
}

src/Constants/SettingConstant.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ final class SettingConstant
1616

1717
public const string SETTING_CUSTOM_EVENTS = self::SETTING_PREFIX . 'cus';
1818

19-
public const string SETTING_GITHUB_EVENTS = self::SETTING_CUSTOM_EVENTS . EventConstant::GITHUB_EVENT_SEPARATOR;
19+
public const string SETTING_GITHUB_EVENTS = self::SETTING_CUSTOM_EVENTS . 'gh.';
2020

21-
public const string SETTING_GITLAB_EVENTS = self::SETTING_CUSTOM_EVENTS . EventConstant::GITLAB_EVENT_SEPARATOR;
21+
public const string SETTING_GITLAB_EVENTS = self::SETTING_CUSTOM_EVENTS . 'gl.';
2222

2323
public const int BTN_LINE_ITEM_COUNT = 2;
2424

src/Enums/Platform.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ enum Platform: string
77
case GITHUB = 'github';
88
case GITLAB = 'gitlab';
99

10+
public const DEFAULT = 'github';
11+
1012
public function eventSeparator(): string
1113
{
1214
return match ($this) {

src/Interfaces/EventInterface.php

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

33
namespace CSlant\TelegramGitNotifier\Interfaces;
44

5-
use CSlant\TelegramGitNotifier\Constants\EventConstant;
5+
use CSlant\TelegramGitNotifier\Enums\Platform;
66
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
77
use CSlant\TelegramGitNotifier\Trait\ActionEventTrait;
88
use Symfony\Component\HttpFoundation\Request;
@@ -28,7 +28,7 @@ public function getActionOfEvent(object $payload): string;
2828
* @return void
2929
* @see EventTrait::setPlatFormForEvent()
3030
*/
31-
public function setPlatFormForEvent(?string $platform = EventConstant::DEFAULT_PLATFORM, ?string $platformFile = null): void;
31+
public function setPlatFormForEvent(?string $platform = Platform::DEFAULT, ?string $platformFile = null): void;
3232

3333
/**
3434
* Set event config and get event name

src/Interfaces/SettingInterface.php

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

33
namespace CSlant\TelegramGitNotifier\Interfaces;
44

5-
use CSlant\TelegramGitNotifier\Constants\EventConstant;
5+
use CSlant\TelegramGitNotifier\Enums\Platform;
66
use CSlant\TelegramGitNotifier\Exceptions\BotException;
77
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
88
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
@@ -20,7 +20,7 @@ interface SettingInterface
2020
* @return array<int, array<int, mixed>>
2121
* @see EventSettingTrait::eventMarkup()
2222
*/
23-
public function eventMarkup(?string $parentEvent = null, string $platform = EventConstant::DEFAULT_PLATFORM): array;
23+
public function eventMarkup(?string $parentEvent = null, string $platform = Platform::DEFAULT): array;
2424

2525
/**
2626
* Get callback data for markup

src/Models/Event.php

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

33
namespace CSlant\TelegramGitNotifier\Models;
44

5-
use CSlant\TelegramGitNotifier\Constants\EventConstant;
65
use CSlant\TelegramGitNotifier\Enums\Platform;
76

87
class Event
@@ -14,7 +13,7 @@ class Event
1413

1514
private string $lastLoadedFile = '';
1615

17-
public string $platform = EventConstant::DEFAULT_PLATFORM;
16+
public string $platform = Platform::DEFAULT;
1817

1918
public string $platformFile = '' {
2019
get {
@@ -39,7 +38,7 @@ public function getEventConfig(): array
3938
*/
4039
public function setEventConfig(?string $platform = null): void
4140
{
42-
$newPlatform = $platform ?? EventConstant::DEFAULT_PLATFORM;
41+
$newPlatform = $platform ?? Platform::DEFAULT;
4342
$this->platform = $newPlatform;
4443

4544
// Skip re-reading if same file is already loaded

src/Notifier.php

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

33
namespace CSlant\TelegramGitNotifier;
44

5-
use CSlant\TelegramGitNotifier\Constants\EventConstant;
65
use CSlant\TelegramGitNotifier\DTOs\ChatTarget;
6+
use CSlant\TelegramGitNotifier\Enums\Platform;
77
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
88
use CSlant\TelegramGitNotifier\Interfaces\EventInterface;
99
use CSlant\TelegramGitNotifier\Interfaces\Structures\NotificationInterface;
@@ -32,7 +32,7 @@ public function __construct(
3232
?Telegram $telegram = null,
3333
?string $chatBotId = null,
3434
?Event $event = null,
35-
?string $platform = EventConstant::DEFAULT_PLATFORM,
35+
?string $platform = Platform::DEFAULT,
3636
?string $platformFile = null,
3737
?Client $client = null,
3838
) {

src/Structures/Notification.php

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

33
namespace CSlant\TelegramGitNotifier\Structures;
44

5-
use CSlant\TelegramGitNotifier\Constants\EventConstant;
5+
use CSlant\TelegramGitNotifier\Enums\Platform;
66
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
77
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
88
use GuzzleHttp\Exception\GuzzleException;
@@ -40,7 +40,7 @@ public function setPayload(Request $request, string $event): ?object
4040
{
4141
$content = match ($this->event->platform) {
4242
'gitlab' => $request->getContent(),
43-
EventConstant::DEFAULT_PLATFORM => $request->request->get('payload'),
43+
Platform::DEFAULT => $request->request->get('payload'),
4444
default => null,
4545
};
4646

src/Trait/EventSettingTrait.php

Lines changed: 20 additions & 16 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\SettingConstant;
7+
use CSlant\TelegramGitNotifier\Enums\Platform;
78

89
trait EventSettingTrait
910
{
@@ -12,7 +13,7 @@ trait EventSettingTrait
1213
*/
1314
public function eventMarkup(
1415
?string $parentEvent = null,
15-
string $platform = EventConstant::DEFAULT_PLATFORM,
16+
string $platform = Platform::DEFAULT,
1617
?string $platformFile = null
1718
): array {
1819
$replyMarkup = $replyMarkupItem = [];
@@ -56,9 +57,7 @@ public function getCallbackData(
5657
array|bool $value = false,
5758
?string $parentEvent = null
5859
): string {
59-
$platformSeparator = $platform === EventConstant::DEFAULT_PLATFORM
60-
? EventConstant::GITHUB_EVENT_SEPARATOR
61-
: EventConstant::GITLAB_EVENT_SEPARATOR;
60+
$platformSeparator = Platform::from($platform)->eventSeparator();
6261
$prefix = EventConstant::EVENT_PREFIX . $platformSeparator;
6362

6463
if (is_array($value)) {
@@ -136,14 +135,14 @@ public function getPlatformFromCallback(
136135
}
137136

138137
if ($callback) {
139-
return match (true) {
140-
str_contains($callback, EventConstant::GITHUB_EVENT_SEPARATOR) => 'github',
141-
str_contains($callback, EventConstant::GITLAB_EVENT_SEPARATOR) => 'gitlab',
142-
default => EventConstant::DEFAULT_PLATFORM,
143-
};
138+
foreach (Platform::cases() as $p) {
139+
if (str_contains($callback, $p->eventSeparator())) {
140+
return $p->value;
141+
}
142+
}
144143
}
145144

146-
return EventConstant::DEFAULT_PLATFORM;
145+
return Platform::DEFAULT;
147146
}
148147

149148
public function sendSettingEventMessage(
@@ -175,11 +174,16 @@ public function getEventFromCallback(?string $callback): string
175174
return '';
176175
}
177176

178-
return str_replace([
179-
EventConstant::EVENT_PREFIX,
180-
EventConstant::GITHUB_EVENT_SEPARATOR,
181-
EventConstant::GITLAB_EVENT_SEPARATOR,
182-
], '', $callback);
177+
$separators = array_map(
178+
fn (Platform $p) => $p->eventSeparator(),
179+
Platform::cases(),
180+
);
181+
182+
return str_replace(
183+
[EventConstant::EVENT_PREFIX, ...$separators],
184+
'',
185+
$callback,
186+
);
183187
}
184188

185189
public function handleEventWithActions(
@@ -235,7 +239,7 @@ public function eventUpdateHandle(
235239
$this->event->updateEvent($event, $action);
236240
$this->eventHandle(
237241
$action
238-
? EventConstant::PLATFORM_EVENT_SEPARATOR[$platform]
242+
? Platform::from($platform)->eventSeparator()
239243
. EventConstant::EVENT_HAS_ACTION_SEPARATOR . $event
240244
: null,
241245
$platform

0 commit comments

Comments
 (0)