🆙 Fix code in somephp files an refactored and deleted deprecated code 🆙

This commit is contained in:
Remco
2026-01-19 17:25:31 +01:00
parent 3238cdb8e7
commit 6736e8fe60
9 changed files with 145 additions and 92 deletions
@@ -12,17 +12,34 @@ class PermissionsService
public function __construct()
{
Cache::remember('website_permissions', now()->addMinutes(30), fn () => WebsitePermission::all()->pluck('min_rank', 'permission'));
$this->permissions = Cache::get('website_permissions');
$this->permissions = Cache::remember(
'website_permissions',
now()->addMinutes(30),
fn () => WebsitePermission::all()->pluck('min_rank', 'permission')
);
}
public function getOrDefault(string $permissionName, bool $default = false): bool
{
if (! array_key_exists($permissionName, $this->permissions->toArray())) {
if (! $this->permissions instanceof Collection || ! $this->permissions->has($permissionName)) {
return $default;
}
return auth()->check() && auth()->user()->rank >= (int) $this->permissions->get($permissionName);
if (! auth()->check()) {
return false;
}
return auth()->user()->rank >= (int) $this->permissions->get($permissionName);
}
public function refresh(): void
{
Cache::forget('website_permissions');
$this->permissions = Cache::remember(
'website_permissions',
now()->addMinutes(30),
fn () => WebsitePermission::all()->pluck('min_rank', 'permission')
);
}
}
+17 -14
View File
@@ -32,16 +32,19 @@ class RconService
if (! $this->socket) {
$error = socket_strerror(socket_last_error());
Log::error("RCON initialization failed: $error");
Log::error("RCON initialization failed: {$error}");
$this->closeConnection();
return;
}
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 5, 'usec' => 0]);
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => 5, 'usec' => 0]);
if (! @socket_connect($this->socket, $this->config['ip'], $this->config['port'])) {
$error = socket_strerror(socket_last_error());
Log::error("RCON connection failed: $error");
Log::error("RCON connection failed: {$error}");
$this->closeConnection();
@@ -53,7 +56,7 @@ class RconService
private function closeConnection(): void
{
if ($this->socket instanceof \Socket) {
if ($this->socket instanceof Socket) {
socket_close($this->socket);
}
@@ -70,29 +73,24 @@ class RconService
* @throws RconConnectionException
* @throws JsonException
*/
public function sendCommand(string $command, ?array $data = null)
public function sendCommand(string $command, ?array $data = null): bool
{
if (! $this->isConnected) {
$error = 'RCON command failed: Not connected';
Log::error($error);
Log::error('RCON command failed: Not connected');
$this->closeConnection();
return $this->isConnected;
return false;
}
$payload = json_encode(['key' => $command, 'data' => $data], JSON_THROW_ON_ERROR);
if (! @socket_write($this->socket, $payload, strlen($payload))) {
$error = socket_strerror(socket_last_error($this->socket));
Log::error("RCON command ($command) failed: $error");
Log::error("RCON command ({$command}) failed: {$error}");
$this->closeConnection();
return $this->isConnected;
return false;
}
return $this->isConnected;
return true;
}
/**
@@ -254,4 +252,9 @@ class RconService
'command' => $command,
]);
}
public function __destruct()
{
$this->closeConnection();
}
}
+21 -4
View File
@@ -15,9 +15,11 @@ class SettingsService
public function __construct()
{
try {
Cache::remember('website_settings', now()->addMinutes(5), fn () => Schema::hasTable('website_settings') ? WebsiteSetting::all()->pluck('value', 'key') : collect());
$this->settings = Cache::get('website_settings');
$this->settings = Cache::remember(
'website_settings',
now()->addMinutes(5),
fn () => Schema::hasTable('website_settings') ? WebsiteSetting::all()->pluck('value', 'key') : collect()
);
} catch (Throwable) {
$this->settings = collect();
}
@@ -25,10 +27,25 @@ class SettingsService
public function getOrDefault(string $settingName, ?string $default = null): string
{
if (! $this->settings instanceof \Illuminate\Support\Collection) {
if (! $this->settings instanceof Collection) {
return (string) $default;
}
return (string) $this->settings->get($settingName, $default);
}
public function refresh(): void
{
Cache::forget('website_settings');
try {
$this->settings = Cache::remember(
'website_settings',
now()->addMinutes(5),
fn () => Schema::hasTable('website_settings') ? WebsiteSetting::all()->pluck('value', 'key') : collect()
);
} catch (Throwable) {
$this->settings = collect();
}
}
}