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.

114 line
4.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\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. {
  35. $this->em = $em;
  36. $this->merchantRepository = $merchantRepository;
  37. $this->sectionRepository = $sectionRepository;
  38. $this->merchantSettingDefinition = $merchantSettingDefinition;
  39. $this->sectionSettingDefinition = $sectionSettingDefinition;
  40. $this->merchantSettingFactory = $merchantSettingFactory;
  41. $this->sectionSettingFactory = $sectionSettingFactory;
  42. }
  43. public static function getSubscribedEvents()
  44. {
  45. return [
  46. KernelEvents::CONTROLLER => ['initSettings']
  47. ];
  48. }
  49. public function initSettings()
  50. {
  51. $this->initSettingsGeneric(
  52. 'merchant',
  53. $this->merchantSettingDefinition->getSettings(),
  54. $this->merchantRepository->findAll(),
  55. $this->merchantSettingFactory
  56. );
  57. $this->initSettingsGeneric(
  58. 'section',
  59. $this->sectionSettingDefinition->getSettings(),
  60. $this->sectionRepository->findAll(),
  61. $this->sectionSettingFactory
  62. );
  63. }
  64. public function initSettingsGeneric($type, $settings, $entities, $factory)
  65. {
  66. foreach ($entities as $entity) {
  67. foreach ($settings as $category => $settingList) {
  68. foreach ($settingList as $settingName => $setting) {
  69. $entitySetting = $entity->getSetting($settingName);
  70. if (!$entitySetting) {
  71. // gestion du cas des SectionSetting spécifiques à une section
  72. $createEntitySetting = true;
  73. if ($entity instanceof SectionInterface && isset($setting['section']) && $setting['section'] != $entity) {
  74. $createEntitySetting = false;
  75. }
  76. if ($createEntitySetting) {
  77. $entitySetting = $factory->create(
  78. [
  79. $type => $entity,
  80. 'name' => $setting['name'],
  81. $setting['field'] => isset($setting['default']) ? $setting['default'] : null,
  82. ]
  83. );
  84. $this->em->persist($entitySetting);
  85. }
  86. } else {
  87. if ($entitySetting->getValue() === null
  88. && isset($setting['default'])
  89. && $setting['default'] !== null) {
  90. $methodSetValue = 'set' . ucfirst($setting['field']);
  91. $entitySetting->$methodSetValue($setting['default']);
  92. $this->em->update($entitySetting);
  93. }
  94. }
  95. }
  96. }
  97. }
  98. $this->em->flush();
  99. }
  100. }