49 lines
1.2 KiB
PHP
49 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Repository;
|
|
|
|
use App\Entity\Setting;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
/**
|
|
* SettingRepository provides helper accessors for app configuration storage.
|
|
*/
|
|
class SettingRepository extends ServiceEntityRepository
|
|
{
|
|
/**
|
|
* Injects the Doctrine registry reference.
|
|
*/
|
|
public function __construct(ManagerRegistry $registry)
|
|
{
|
|
parent::__construct($registry, Setting::class);
|
|
}
|
|
|
|
/**
|
|
* Returns a setting value, falling back to the caller's default when missing.
|
|
*/
|
|
public function getValue(string $name, ?string $default = null): ?string
|
|
{
|
|
$setting = $this->findOneBy(['name' => $name]);
|
|
return $setting?->getValue() ?? $default;
|
|
}
|
|
|
|
/**
|
|
* Persists or updates the supplied configuration value.
|
|
*/
|
|
public function setValue(string $name, ?string $value): void
|
|
{
|
|
$em = $this->getEntityManager();
|
|
$setting = $this->findOneBy(['name' => $name]);
|
|
if ($setting === null) {
|
|
$setting = new Setting();
|
|
$setting->setName($name);
|
|
}
|
|
$setting->setValue($value);
|
|
$em->persist($setting);
|
|
$em->flush();
|
|
}
|
|
}
|
|
|
|
|