|
- <?php
-
- namespace Lc\CaracoleBundle\EventSubscriber;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Definition\MerchantSettingDefinitionInterface;
- use Lc\CaracoleBundle\Factory\Setting\MerchantSettingFactory;
- use Lc\CaracoleBundle\Factory\Setting\SectionSettingFactory;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\CaracoleBundle\Definition\SectionSettingDefinitionInterface;
- use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
- use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
- use Lc\CaracoleBundle\Repository\Section\SectionRepository;
- use Lc\CaracoleBundle\Repository\Section\SectionStore;
- use Lc\SovBundle\Solver\Setting\SettingSolver;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\HttpKernel\KernelEvents;
-
- class SettingEventSubscriber implements EventSubscriberInterface
- {
- protected EntityManagerInterface $entityManager;
- protected MerchantSettingDefinitionInterface $merchantSettingDefinition;
- protected SectionSettingDefinitionInterface $sectionSettingDefinition;
- protected MerchantStore $merchantStore;
- protected SectionStore $sectionStore;
- protected MerchantSettingFactory $merchantSettingFactory;
- protected SectionSettingFactory $sectionSettingFactory;
- protected SettingSolver $settingSolver;
-
- public function __construct(
- EntityManagerInterface $entityManager,
- MerchantSettingDefinitionInterface $merchantSettingDefinition,
- SectionSettingDefinitionInterface $sectionSettingDefinition,
- MerchantStore $merchantStore,
- SectionStore $sectionStore,
- MerchantSettingFactory $merchantSettingFactory,
- SectionSettingFactory $sectionSettingFactory,
- SettingSolver $settingSolver
- ) {
- $this->entityManager = $entityManager;
- $this->merchantStore = $merchantStore;
- $this->sectionStore = $sectionStore;
- $this->merchantSettingDefinition = $merchantSettingDefinition;
- $this->sectionSettingDefinition = $sectionSettingDefinition;
- $this->merchantSettingFactory = $merchantSettingFactory;
- $this->sectionSettingFactory = $sectionSettingFactory;
- $this->settingSolver = $settingSolver;
- }
-
- public static function getSubscribedEvents()
- {
- return [
- KernelEvents::CONTROLLER => ['initSettings']
- ];
- }
-
- public function initSettings()
- {
- $merchants = $this->merchantStore->get();
-
- $this->initSettingsGeneric(
- 'merchant',
- $this->merchantSettingDefinition->getSettings(),
- //TODO vérifier que ce soit bien les online que l'on souhaite
- $merchants,
- $this->merchantSettingFactory
- );
-
- foreach($merchants as $merchant) {
- $this->initSettingsGeneric(
- 'section',
- $this->sectionSettingDefinition->getSettings(),
- $merchant->getSections(),
- $this->sectionSettingFactory
- );
- }
- }
-
- public function initSettingsGeneric($type, $settings, $entities, $factory)
- {
- if($entities) {
- foreach ($entities as $entity) {
- foreach ($settings as $category => $settingList) {
- foreach ($settingList as $settingName => $setting) {
- $entitySetting = $this->settingSolver->getSetting($entity, $settingName);
-
- if (!$entitySetting) {
- // gestion du cas des SectionSetting spécifiques à une section
- $createEntitySetting = true;
- if ($entity instanceof SectionInterface && isset($setting['section']) && $setting['section'] != $entity) {
- $createEntitySetting = false;
- }
-
- if ($createEntitySetting) {
- $text = null;
- $date = null;
- $file = null;
-
- $fieldValue = isset($setting['default']) ? $setting['default'] : null;
-
- if ($setting['field'] == 'text') {
- $text = $fieldValue;
- } elseif ($setting['field'] == 'date') {
- $date = $fieldValue;
- } elseif ($setting['field'] == 'file') {
- $file = $fieldValue;
- }
-
- $entitySetting = $factory->create($entity, $setting['name'], $text, $date, $file);
-
- $this->entityManager->persist($entitySetting);
- }
- } else {
- if ($this->settingSolver->getValue($entitySetting) === null
- && isset($setting['default'])
- && $setting['default'] !== null) {
- $methodSetValue = 'set' . ucfirst($setting['field']);
- $entitySetting->$methodSetValue($setting['default']);
- $this->entityManager->update($entitySetting);
- }
- }
- }
- }
- }
-
- $this->entityManager->flush();
- }
- }
-
- }
|