Files
alrahma_sunday_school_api/app/Models/Configuration.php
T
root e13df69885
API CI/CD / Validate (composer + pint) (push) Successful in 3m6s
API CI/CD / Test (PHPUnit) (push) Failing after 4m53s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Security audit (push) Failing after 59s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
fix unittests issues
2026-07-07 20:56:32 -04:00

117 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Models;
use App\Services\SemesterRangeService;
class Configuration extends BaseModel
{
protected $table = 'configuration';
// The legacy table only has id/config_key/config_value.
public $timestamps = false;
public $incrementing = true;
protected $fillable = ['config_key', 'config_value'];
/* =========================
* legacy method equivalents
* ========================= */
/**
* Get configuration value by key (deterministic in case duplicates exist).
*/
public static function getConfigValueByKey(string $key): ?string
{
try {
$row = static::query()
->where('config_key', $key)
->orderByDesc('id')
->first();
} catch (\Throwable $e) {
if (app()->runningInConsole()) {
return null;
}
throw $e;
}
return $row ? (string) $row->config_value : null;
}
/**
* Set configuration value by key.
* If duplicates exist, update ALL of them to avoid inconsistent reads.
*/
public static function setConfigValueByKey(string $key, string $value): bool
{
$count = static::query()->where('config_key', $key)->count();
if ($count > 0) {
$affected = static::query()
->where('config_key', $key)
->update(['config_value' => $value]);
return $affected >= 0;
}
$row = static::query()->create([
'config_key' => $key,
'config_value' => $value,
]);
return ! empty($row->id);
}
/**
* Retrieve all configuration rows.
*/
public static function getAllConfigs()
{
return static::query()->get();
}
/**
* Update configuration by id.
*/
public static function updateConfig(int $id, array $data): bool
{
return static::query()->whereKey($id)->update($data) >= 0;
}
/**
* Add new configuration row.
*/
public static function addConfig(array $data): int
{
$row = static::query()->create($data);
return (int) $row->id;
}
/**
* Backward-compatible helper (keeps your "semester" special-case).
*
* NOTE: In Laravel its better to call SemesterRangeService directly from services/controllers,
* but this keeps behavior consistent with the legacy model.
*/
public static function getConfig(string $key): ?string
{
$key = (string) $key;
if ($key === 'semester') {
try {
// If you have this service in Laravel, inject/resolve it from container.
// This call assumes the service constructor can accept Configuration model (like legacy did).
$semester = app(SemesterRangeService::class)->getSemesterForDate();
if (is_string($semester) && $semester !== '') {
return $semester;
}
} catch (\Throwable $e) {
// ignore and fall back
}
}
return static::getConfigValueByKey($key);
}
}