<?php
declare(strict_types=1);
namespace App\Bundles\SettingsBundle\Service;
use App\Bundles\SettingsBundle\Entity\Setting;
use App\Bundles\SettingsBundle\Enum\SettingsGroupEnum;
use App\Bundles\SettingsBundle\Enum\SettingsNameEnum;
use App\Bundles\SettingsBundle\Repository\SettingsRepository;
class SettingsProvider
{
public const DEFAULT_SETTINGS_VALUE = 'default';
public function __construct(
private readonly SettingsRepository $settingRepository,
) {
}
/** @return Setting[] */
public function provideSettings(SettingsGroupEnum $group): array
{
return $this->settingRepository->findBy(['settingGroup' => $group]);
}
public function provideForTwig(SettingsGroupEnum $group): array
{
$data = [];
$settings = $this->provideSettings($group);
$settingsNames = SettingsNameEnum::getFieldsByGroup(SettingsGroupEnum::EXTERNAL_RESOURCES);
if (count($settings) < count($settingsNames) - 1) {
foreach ($settingsNames as $name) {
$data[$name->value] = self::DEFAULT_SETTINGS_VALUE;
}
return $data;
}
foreach ($settings as $setting) {
$data[$setting->getName()->value] = $setting->getValue();
}
return $data;
}
}