|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace CSlant\TelegramGitNotifier\Models; |
|
6
|
|
|
|
|
7
|
|
|
use CSlant\TelegramGitNotifier\Constants\EventConstant; |
|
8
|
|
|
use JsonException; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Event |
|
13
|
|
|
* |
|
14
|
|
|
* Handles event configuration management for different platforms. |
|
15
|
|
|
* Manages loading, updating, and persisting event configurations. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Event |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var array<string, mixed> Event configuration data */ |
|
20
|
|
|
private array $eventConfig = []; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string The current platform (e.g., 'github', 'gitlab') */ |
|
23
|
|
|
public string $platform = EventConstant::DEFAULT_PLATFORM; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string Path to the platform configuration file */ |
|
26
|
|
|
private string $platformFile = ''; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Get the platform configuration file path |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getPlatformFile(): string |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->platformFile; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Set the platform configuration file path |
|
38
|
|
|
* |
|
39
|
|
|
* @throws RuntimeException If the file doesn't exist or is not readable |
|
40
|
|
|
*/ |
|
41
|
|
|
public function setPlatformFile(string $platformFile): void |
|
42
|
|
|
{ |
|
43
|
|
|
if (!file_exists($platformFile) || !is_readable($platformFile)) { |
|
44
|
|
|
throw new RuntimeException( |
|
45
|
|
|
"Platform configuration file not found or not readable: {$platformFile}" |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->platformFile = $platformFile; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the current event configuration |
|
54
|
|
|
* |
|
55
|
|
|
* @return array<string, mixed> |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getEventConfig(): array |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->eventConfig; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Load and set event configuration from platform file |
|
64
|
|
|
* |
|
65
|
|
|
* @param string|null $platform The platform to load configuration for |
|
66
|
|
|
* @throws RuntimeException If the configuration file cannot be read or parsed |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setEventConfig(?string $platform = null): void |
|
69
|
|
|
{ |
|
70
|
|
|
$this->platform = $platform ?? EventConstant::DEFAULT_PLATFORM; |
|
71
|
|
|
|
|
72
|
|
|
if (empty($this->platformFile)) { |
|
73
|
|
|
throw new RuntimeException('Platform file path is not set'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$json = file_get_contents($this->platformFile); |
|
77
|
|
|
if ($json === false) { |
|
78
|
|
|
throw new RuntimeException("Failed to read platform file: {$this->platformFile}"); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (empty($json)) { |
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
try { |
|
86
|
|
|
$config = json_decode($json, true, 512, JSON_THROW_ON_ERROR); |
|
87
|
|
|
$this->eventConfig = is_array($config) ? $config : []; |
|
88
|
|
|
} catch (JsonException $e) { |
|
89
|
|
|
throw new RuntimeException("Invalid JSON in platform file: {$e->getMessage()}", 0, $e); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Toggle an event or event action configuration |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $event The event name |
|
97
|
|
|
* @param string|null $action The specific action within the event (optional) |
|
98
|
|
|
* @throws RuntimeException If unable to save the updated configuration |
|
99
|
|
|
*/ |
|
100
|
|
|
public function updateEvent(string $event, ?string $action = null): void |
|
101
|
|
|
{ |
|
102
|
|
|
if ($action !== null && $action !== '') { |
|
103
|
|
|
if (!isset($this->eventConfig[$event][$action])) { |
|
104
|
|
|
$this->eventConfig[$event][$action] = false; |
|
105
|
|
|
} |
|
106
|
|
|
$this->eventConfig[$event][$action] = !$this->eventConfig[$event][$action]; |
|
107
|
|
|
} else { |
|
108
|
|
|
if (!isset($this->eventConfig[$event])) { |
|
109
|
|
|
$this->eventConfig[$event] = false; |
|
110
|
|
|
} elseif (is_array($this->eventConfig[$event])) { |
|
111
|
|
|
// Toggle all actions if the event is an array of actions |
|
112
|
|
|
foreach ($this->eventConfig[$event] as &$value) { |
|
113
|
|
|
$value = !$value; |
|
114
|
|
|
} |
|
115
|
|
|
unset($value); |
|
116
|
|
|
} else { |
|
117
|
|
|
$this->eventConfig[$event] = !$this->eventConfig[$event]; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$this->saveEventConfig(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Save the current event configuration to the platform file |
|
126
|
|
|
* |
|
127
|
|
|
* @throws RuntimeException If unable to write to the configuration file |
|
128
|
|
|
*/ |
|
129
|
|
|
private function saveEventConfig(): void |
|
130
|
|
|
{ |
|
131
|
|
|
if (empty($this->platformFile)) { |
|
132
|
|
|
throw new RuntimeException('Cannot save event config: Platform file path is not set'); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (!is_writable($this->platformFile)) { |
|
136
|
|
|
throw new RuntimeException("Cannot write to platform file: {$this->platformFile}"); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
try { |
|
140
|
|
|
$json = json_encode($this->eventConfig, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); |
|
141
|
|
|
$result = file_put_contents($this->platformFile, $json, LOCK_EX); |
|
142
|
|
|
|
|
143
|
|
|
if ($result === false) { |
|
144
|
|
|
throw new RuntimeException("Failed to write to platform file: {$this->platformFile}"); |
|
145
|
|
|
} |
|
146
|
|
|
} catch (JsonException $e) { |
|
147
|
|
|
throw new RuntimeException("Failed to encode event config: {$e->getMessage()}", 0, $e); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|