181ac9e94e
API CI/CD / Validate (composer + pint) (push) Successful in 2m8s
API CI/CD / Test (PHPUnit) (push) Failing after 54s
API CI/CD / Build frontend assets (push) Successful in 53s
API CI/CD / Security audit (push) Successful in 33s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
Three changes: 1. Add composer dump-autoload after composer install in all jobs (validate, test, security) — ensures fresh autoloader mapping 2. Replace config:clear + view:clear with optimize:clear in test job — clears bootstrap/cache (services.php, packages.php) which caused 'Class not found' for Sanctum\Guard, 'not instantiable' for AttendanceMailerService, and 'factory() undefined' for User 3. Add explicit $incrementing = true to Configuration model — ensures Eloquent treats id as auto-increment (belt-and-suspenders for SQLite NOT NULL constraint failures)
120 lines
3.1 KiB
PHP
120 lines
3.1 KiB
PHP
<?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 it’s 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);
|
||
}
|
||
}
|