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.

111 lines
4.2KB

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