|
- <?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\Section\SectionRepository;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\HttpKernel\KernelEvents;
-
- class SettingEventSubscriber implements EventSubscriberInterface
- {
- protected $em;
- protected $merchantRepository;
- protected $sectionRepository;
- protected $merchantSettingDefinition;
- protected $sectionSettingDefinition;
- protected $merchantSettingFactory;
- protected $sectionSettingFactory;
-
- public function __construct(
- EntityManagerInterface $em,
- MerchantSettingDefinitionInterface $merchantSettingDefinition,
- SectionSettingDefinitionInterface $sectionSettingDefinition,
- MerchantRepository $merchantRepository,
- SectionRepository $sectionRepository,
- MerchantSettingFactory $merchantSettingFactory,
- SectionSettingFactory $sectionSettingFactory
- ) {
- $this->em = $em;
- $this->merchantRepository = $merchantRepository;
- $this->sectionRepository = $sectionRepository;
- $this->merchantSettingDefinition = $merchantSettingDefinition;
- $this->sectionSettingDefinition = $sectionSettingDefinition;
- $this->merchantSettingFactory = $merchantSettingFactory;
- $this->sectionSettingFactory = $sectionSettingFactory;
- }
-
- public static function getSubscribedEvents()
- {
- return [
- KernelEvents::CONTROLLER => ['initSettings']
- ];
- }
-
- public function initSettings()
- {
- $this->initSettingsGeneric(
- 'merchant',
- $this->merchantSettingDefinition->getSettings(),
- $this->merchantRepository->findAll(),
- $this->merchantSettingFactory
- );
-
- $this->initSettingsGeneric(
- 'section',
- $this->sectionSettingDefinition->getSettings(),
- $this->sectionRepository->findAll(),
- $this->sectionSettingFactory
- );
- }
-
- public function initSettingsGeneric($type, $settings, $entities, $factory)
- {
- foreach ($entities as $entity) {
- foreach ($settings as $category => $settingList) {
- foreach ($settingList as $settingName => $setting) {
- $entitySetting = $entity->getSetting($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->em->persist($entitySetting);
- }
- } else {
- if ($entitySetting->getValue() === null
- && isset($setting['default'])
- && $setting['default'] !== null) {
- $methodSetValue = 'set' . ucfirst($setting['field']);
- $entitySetting->$methodSetValue($setting['default']);
- $this->em->update($entitySetting);
- }
- }
- }
- }
- }
-
- $this->em->flush();
- }
-
- }
|