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.

112 line
4.4KB

  1. <?php
  2. namespace Lc\CaracoleBundle\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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\Security\Core\Security;
  16. class SettingEventSubscriber implements EventSubscriberInterface
  17. {
  18. protected $em;
  19. protected $merchantRepository;
  20. protected $sectionRepository;
  21. protected $merchantSettingDefinition;
  22. protected $sectionSettingDefinition;
  23. protected $merchantSettingFactory;
  24. protected $sectionSettingFactory;
  25. public function __construct(
  26. EntityManagerInterface $em,
  27. MerchantSettingDefinitionInterface $merchantSettingDefinition,
  28. SectionSettingDefinitionInterface $sectionSettingDefinition,
  29. MerchantRepository $merchantRepository,
  30. SectionRepository $sectionRepository,
  31. MerchantSettingFactory $merchantSettingFactory,
  32. SectionSettingFactory $sectionSettingFactory
  33. ) {
  34. $this->em = $em;
  35. $this->merchantRepository = $merchantRepository;
  36. $this->sectionRepository = $sectionRepository;
  37. $this->merchantSettingDefinition = $merchantSettingDefinition;
  38. $this->sectionSettingDefinition = $sectionSettingDefinition;
  39. $this->merchantSettingFactory = $merchantSettingFactory;
  40. $this->sectionSettingFactory = $sectionSettingFactory;
  41. }
  42. public static function getSubscribedEvents()
  43. {
  44. return [
  45. KernelEvents::CONTROLLER => ['initSettings']
  46. ];
  47. }
  48. public function initSettings()
  49. {
  50. $this->initSettingsGeneric(
  51. 'merchant',
  52. $this->merchantSettingDefinition->getSettings(),
  53. $this->merchantRepository->findAll(),
  54. $this->merchantSettingFactory
  55. );
  56. $this->initSettingsGeneric(
  57. 'section',
  58. $this->sectionSettingDefinition->getSettings(),
  59. $this->sectionRepository->findAll(),
  60. $this->sectionSettingFactory
  61. );
  62. }
  63. public function initSettingsGeneric($type, $settings, $entities, $factory)
  64. {
  65. foreach ($entities as $entity) {
  66. foreach ($settings as $category => $settingList) {
  67. foreach ($settingList as $settingName => $setting) {
  68. $entitySetting = $entity->getSetting($settingName);
  69. if (!$entitySetting) {
  70. // gestion du cas des SectionSetting spécifiques à une section
  71. $createEntitySetting = true;
  72. if ($entity instanceof SectionInterface && isset($setting['section']) && $setting['section'] != $entity) {
  73. $createEntitySetting = false;
  74. }
  75. if ($createEntitySetting) {
  76. $entitySetting = $factory->create(
  77. [
  78. $type => $entity,
  79. 'name' => $setting['name'],
  80. $setting['field'] => isset($setting['default']) ? $setting['default'] : null,
  81. ]
  82. );
  83. $this->em->persist($entitySetting);
  84. }
  85. } else {
  86. if ($entitySetting->getValue() === null
  87. && isset($setting['default'])
  88. && $setting['default'] !== null) {
  89. $methodSetValue = 'set' . ucfirst($setting['field']);
  90. $entitySetting->$methodSetValue($setting['default']);
  91. $this->em->update($entitySetting);
  92. }
  93. }
  94. }
  95. }
  96. }
  97. $this->em->flush();
  98. }
  99. }