src/Bundles/SettingsBundle/Service/SettingsProvider.php line 27

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Bundles\SettingsBundle\Service;
  4. use App\Bundles\SettingsBundle\Entity\Setting;
  5. use App\Bundles\SettingsBundle\Enum\SettingsGroupEnum;
  6. use App\Bundles\SettingsBundle\Enum\SettingsNameEnum;
  7. use App\Bundles\SettingsBundle\Repository\SettingsRepository;
  8. class SettingsProvider
  9. {
  10.     public const DEFAULT_SETTINGS_VALUE 'default';
  11.     public function __construct(
  12.         private readonly SettingsRepository $settingRepository,
  13.     ) {
  14.     }
  15.     /** @return Setting[] */
  16.     public function provideSettings(SettingsGroupEnum $group): array
  17.     {
  18.         return $this->settingRepository->findBy(['settingGroup' => $group]);
  19.     }
  20.     public function provideForTwig(SettingsGroupEnum $group): array
  21.     {
  22.         $data = [];
  23.         $settings $this->provideSettings($group);
  24.         $settingsNames SettingsNameEnum::getFieldsByGroup(SettingsGroupEnum::EXTERNAL_RESOURCES);
  25.         if (count($settings) < count($settingsNames) - 1) {
  26.             foreach ($settingsNames as $name) {
  27.                 $data[$name->value] = self::DEFAULT_SETTINGS_VALUE;
  28.             }
  29.             return $data;
  30.         }
  31.         foreach ($settings as $setting) {
  32.             $data[$setting->getName()->value] = $setting->getValue();
  33.         }
  34.         return $data;
  35.     }
  36. }