src/EventListener/JobAlertSettingListener.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\JobAlertSetting;
  4. use App\Entity\Option;
  5. use App\Event\SettingPrePersistEvent;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. class JobAlertSettingListener
  8. {
  9.     /**
  10.      * @var EntityManagerInterface
  11.      */
  12.     private $manager;
  13.     public function __construct(EntityManagerInterface $manager)
  14.     {
  15.         $this->manager $manager;
  16.     }
  17.     public function onPrePersist(SettingPrePersistEvent $event)
  18.     {
  19.         $setting $event->getSetting();
  20.         if (!$setting instanceof JobAlertSetting) {
  21.             return;
  22.         }
  23.         // remove existing option JobAlertSettings
  24.         $optionGroups $this->manager->getRepository(Option::class)->findAll();
  25.         foreach ($optionGroups as $optionGroup) {
  26.             $optionGroup->setJobAlertSetting(null);
  27.             $this->manager->persist($optionGroup);
  28.         }
  29.         $this->manager->flush();
  30.         // add option JobAlertSettings
  31.         foreach ($setting->getOptionGroups() as &$optionGroup) {
  32.             $optionGroup->setJobAlertSetting($setting);
  33.         }
  34.     }
  35. }