You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.2KB

  1. <?php
  2. namespace Lc\SovBundle\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\SovBundle\Definition\SiteSettingDefinitionInterface;
  5. use Lc\SovBundle\Factory\Setting\SiteSettingFactory;
  6. use Lc\SovBundle\Factory\Site\SiteFactory;
  7. use Lc\SovBundle\Repository\Setting\SiteSettingRepositoryInterface;
  8. use Lc\SovBundle\Repository\Site\SiteRepositoryInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class SiteSettingEventSubscriber implements EventSubscriberInterface
  12. {
  13. protected $em;
  14. protected $siteSettingDefinition;
  15. protected $siteSettingFactory;
  16. protected $siteSettingRepository;
  17. protected $siteRepository;
  18. protected $siteFactory;
  19. public function __construct(
  20. EntityManagerInterface $em,
  21. SiteSettingRepositoryInterface $siteSettingRepository,
  22. SiteRepositoryInterface $siteRepository,
  23. SiteSettingDefinitionInterface $siteSettingDefinition,
  24. SiteSettingFactory $siteSettingFactory,
  25. SiteFactory $siteFactory
  26. ) {
  27. $this->em = $em;
  28. $this->siteSettingDefinition = $siteSettingDefinition;
  29. $this->siteSettingFactory = $siteSettingFactory;
  30. $this->siteSettingRepository = $siteSettingRepository;
  31. $this->siteRepository = $siteRepository;
  32. $this->siteFactory = $siteFactory;
  33. }
  34. public static function getSubscribedEvents()
  35. {
  36. return [
  37. KernelEvents::CONTROLLER => ['initSettings']
  38. ];
  39. }
  40. public function initSettings()
  41. {
  42. $site = $this->getSiteDefault();
  43. if (!$site) {
  44. $site = $this->siteFactory->create(
  45. [
  46. 'devAlias' => 'default'
  47. ]
  48. );
  49. $this->em->persist($site);
  50. $this->em->flush($site);
  51. }
  52. $settings = $this->siteSettingDefinition->getSettings();
  53. $factory = $this->siteSettingFactory;
  54. foreach ($settings as $category => $settingList) {
  55. foreach ($settingList as $settingName => $setting) {
  56. $entitySetting = $site->getSetting($settingName);
  57. if (!$entitySetting) {
  58. $entitySetting = $factory->create(
  59. [
  60. 'site' => $site,
  61. 'name' => $setting['name'],
  62. $setting['field'] => isset($setting['default']) ? $setting['default'] : null,
  63. ]
  64. );
  65. $this->em->persist($entitySetting);
  66. } else {
  67. if ($entitySetting->getValue() === null
  68. && isset($setting['default'])
  69. && $setting['default'] !== null) {
  70. $methodSetValue = 'set' . ucfirst($setting['field']);
  71. $entitySetting->$methodSetValue($setting['default']);
  72. $this->em->update($entitySetting);
  73. }
  74. }
  75. }
  76. }
  77. $this->em->flush();
  78. }
  79. public function getSiteDefault()
  80. {
  81. return $this->siteRepository->findOneByDevAlias('default');
  82. }
  83. }