Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SiteSettingEventSubscriber.php 3.8KB

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