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.

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