lib/composer/LisaBundle/src/VisualMedia/LisaBundle/Service/SettingProvider.php line 44

Open in your IDE?
  1. <?php
  2. /**
  3. * Setting Provider
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6. */
  7. namespace VisualMedia\LisaBundle\Service;
  8. use VisualMedia\LisaBundle\Component\Interfaces\ServiceInterface;
  9. use VisualMedia\LisaBundle\Component\ManagerData;
  10. use VisualMedia\SettingsBundle\Entity\BaseSetting;
  11. use VisualMedia\SettingsBundle\Manager\BaseSettingManager;
  12. use VisualMedia\SettingsBundle\Setting\TypeFactory\StringTypeFactory;
  13. use VisualMedia\TranslationBundle\Service\DomainProvider;
  14. /**
  15. * Setting Provider
  16. */
  17. class SettingProvider implements ServiceInterface
  18. {
  19. public const SERVICE_ID = 'visualmedia.lisa.provider.setting';
  20. /**
  21. * @var DomainProvider
  22. */
  23. protected $domainProvider;
  24. /**
  25. * @var BaseSettingManager
  26. */
  27. protected $manager;
  28. /**
  29. * @var array
  30. */
  31. protected $settings = array();
  32. /**
  33. * @param DomainProvider $domainProvider
  34. * @param BaseSettingManager $manager
  35. */
  36. public function __construct(DomainProvider $domainProvider = null, BaseSettingManager $manager = null)
  37. {
  38. $this->domainProvider = $domainProvider;
  39. $this->manager = $manager;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public static function getServiceId(): string
  45. {
  46. return static::SERVICE_ID;
  47. }
  48. /**
  49. * The first time the SettingProvider builds a cached array with all the settings from the database.
  50. * When retrieving a setting, it will extract the setting from the cached array,
  51. * or persists an empty setting which is set in the cache afterwards.
  52. *
  53. * If the Manager doesn't exists because the Bundle isn't installed, it will return null.
  54. *
  55. * @param string $reference
  56. * @param string $scope
  57. * @param string $factory
  58. * @return BaseSetting|null
  59. */
  60. public function get(string $reference, string $scope = BaseSetting::SETTING_DEFAULT, string $factory = StringTypeFactory::NAME): ?BaseSetting
  61. {
  62. if ($this->manager !== null) {
  63. // Build cache.
  64. if (empty($this->settings)) {
  65. foreach ($result = $this->manager->getIndex() as $setting) {
  66. $this->settings[$setting->getScope()][$setting->getReference()] = $setting;
  67. }
  68. }
  69. // Persist setting if none is found.
  70. if (null === $setting = $this->settings[$scope][$reference] ?? null) {
  71. $domain = $this->domainProvider->getCurrentDomain();
  72. $setting = new BaseSetting();
  73. $setting->setName(ucfirst(str_replace('_', ' ', $reference)));
  74. $setting->setReference($reference);
  75. $setting->setScope($scope);
  76. $setting->setFactory($factory);
  77. $this->manager->persist($setting);
  78. $this->manager->flush();
  79. $this->settings[$scope][$reference] = $setting;
  80. }
  81. return $setting;
  82. }
  83. return null;
  84. }
  85. /**
  86. * @param string $reference
  87. * @param string $scope
  88. * @param string $factory
  89. * @return mixed
  90. */
  91. public function getValue(string $reference, string $scope = BaseSetting::SETTING_DEFAULT, string $factory = StringTypeFactory::NAME)
  92. {
  93. $setting = $this->get($reference, $scope, $factory);
  94. return $setting !== null ? $setting->getValue() : null;
  95. }
  96. }