Files
boursecu_server/app/Services/SettingsRuntimeCanEditSync.php
T

76 lines
2.0 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
use App\Models\Setting;
use Illuminate\Support\Facades\DB;
/**
* تثبیت can_edit=1 برای کلیدهای runtime tenant (Montzar، up/down_price، …).
* بعد از deploy_settings_const.sql هم اجرا می‌شود تا حتی با SQL قدیمی روی سرور درست بماند.
*/
class SettingsRuntimeCanEditSync
{
/** @return list<string> */
public function keys(): array
{
return SuperAdminProgramSettingsOverlay::RUNTIME_KEYS;
}
public function sync(): int
{
$keys = $this->keys();
if ($keys === []) {
return 0;
}
return Setting::query()
->whereIn('key', $keys)
->where(function ($query) {
$query->where('can_edit', '!=', 1)
->orWhereNull('can_edit');
})
->update(['can_edit' => 1]);
}
/**
* @return array<string, mixed> key => can_edit for rows still not 1
*/
public function violations(): array
{
$keys = $this->keys();
if ($keys === []) {
return [];
}
return Setting::query()
->whereIn('key', $keys)
->where(function ($query) {
$query->where('can_edit', '!=', 1)
->orWhereNull('can_edit');
})
->orderBy('key')
->pluck('can_edit', 'key')
->all();
}
/**
* @return array<string, mixed> key => can_edit for all runtime keys present in DB
*/
public function statusMap(): array
{
return Setting::query()
->whereIn('key', $this->keys())
->orderBy('key')
->pluck('can_edit', 'key')
->all();
}
public function useDatabase(string $database): void
{
$connection = (string) config('database.default', 'mysql');
config(["database.connections.{$connection}.database" => $database]);
DB::purge($connection);
}
}