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.

125 lines
5.3KB

  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\Model\Section\SectionInterface;
  8. use Lc\CaracoleBundle\Definition\SectionSettingDefinitionInterface;
  9. use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
  10. use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
  11. use Lc\CaracoleBundle\Repository\Section\SectionRepository;
  12. use Lc\CaracoleBundle\Repository\Section\SectionStore;
  13. use Lc\SovBundle\Solver\Setting\SettingSolver;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. class SettingEventSubscriber implements EventSubscriberInterface
  17. {
  18. protected EntityManagerInterface $entityManager;
  19. protected MerchantSettingDefinitionInterface $merchantSettingDefinition;
  20. protected SectionSettingDefinitionInterface $sectionSettingDefinition;
  21. protected MerchantStore $merchantStore;
  22. protected SectionStore $sectionStore;
  23. protected MerchantSettingFactory $merchantSettingFactory;
  24. protected SectionSettingFactory $sectionSettingFactory;
  25. protected SettingSolver $settingSolver;
  26. public function __construct(
  27. EntityManagerInterface $entityManager,
  28. MerchantSettingDefinitionInterface $merchantSettingDefinition,
  29. SectionSettingDefinitionInterface $sectionSettingDefinition,
  30. MerchantStore $merchantStore,
  31. SectionStore $sectionStore,
  32. MerchantSettingFactory $merchantSettingFactory,
  33. SectionSettingFactory $sectionSettingFactory,
  34. SettingSolver $settingSolver
  35. ) {
  36. $this->entityManager = $entityManager;
  37. $this->merchantStore = $merchantStore;
  38. $this->sectionStore = $sectionStore;
  39. $this->merchantSettingDefinition = $merchantSettingDefinition;
  40. $this->sectionSettingDefinition = $sectionSettingDefinition;
  41. $this->merchantSettingFactory = $merchantSettingFactory;
  42. $this->sectionSettingFactory = $sectionSettingFactory;
  43. $this->settingSolver = $settingSolver;
  44. }
  45. public static function getSubscribedEvents()
  46. {
  47. return [
  48. KernelEvents::CONTROLLER => ['initSettings']
  49. ];
  50. }
  51. public function initSettings()
  52. {
  53. $this->initSettingsGeneric(
  54. 'merchant',
  55. $this->merchantSettingDefinition->getSettings(),
  56. //TODO vérifier que ce soit bien les online que l'on souhaite
  57. $this->merchantStore->getOnline(),
  58. $this->merchantSettingFactory
  59. );
  60. $this->initSettingsGeneric(
  61. 'section',
  62. $this->sectionSettingDefinition->getSettings(),
  63. //TODOJ'en suis là !!! Et je sais pas ce que je dois mettre, tout les sections ? et les sections d'un merchant ?
  64. $this->sectionStore->getOnline(),
  65. $this->sectionSettingFactory
  66. );
  67. }
  68. public function initSettingsGeneric($type, $settings, $entities, $factory)
  69. {
  70. foreach ($entities as $entity) {
  71. foreach ($settings as $category => $settingList) {
  72. foreach ($settingList as $settingName => $setting) {
  73. $entitySetting = $entity->getSetting($settingName);
  74. if (!$entitySetting) {
  75. // gestion du cas des SectionSetting spécifiques à une section
  76. $createEntitySetting = true;
  77. if ($entity instanceof SectionInterface && isset($setting['section']) && $setting['section'] != $entity) {
  78. $createEntitySetting = false;
  79. }
  80. if ($createEntitySetting) {
  81. $text = null;
  82. $date = null;
  83. $file = null;
  84. $fieldValue = isset($setting['default']) ? $setting['default'] : null;
  85. if ($setting['field'] == 'text') {
  86. $text = $fieldValue;
  87. } elseif ($setting['field'] == 'date') {
  88. $date = $fieldValue;
  89. } elseif ($setting['field'] == 'file') {
  90. $file = $fieldValue;
  91. }
  92. $entitySetting = $factory->create($entity, $setting['name'], $text, $date, $file);
  93. $this->entityManager->persist($entitySetting);
  94. }
  95. } else {
  96. if ($this->settingSolver->getValue($entitySetting) === null
  97. && isset($setting['default'])
  98. && $setting['default'] !== null) {
  99. $methodSetValue = 'set' . ucfirst($setting['field']);
  100. $entitySetting->$methodSetValue($setting['default']);
  101. $this->entityManager->update($entitySetting);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. $this->entityManager->flush();
  108. }
  109. }