|
- <?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\Factory\User\UserMerchantFactory;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\CaracoleBundle\Repository\Merchant\MerchantRepository;
- use Lc\CaracoleBundle\Definition\SectionSettingDefinitionInterface;
- use Lc\CaracoleBundle\Repository\Section\SectionRepository;
- use Lc\CaracoleBundle\Resolver\MerchantResolver;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\HttpKernel\KernelEvents;
- use Symfony\Component\Security\Core\Security;
-
- 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) {
- $entitySetting = $factory->create(
- [
- $type => $entity,
- 'name' => $setting['name'],
- $setting['field'] => isset($setting['default']) ? $setting['default'] : null,
- ]
- );
-
- $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();
- }
-
- }
|