src/EventSubscriber/cic/Legacy/DepartmentChangeSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\cic\Legacy;
  3. use App\Event\DepartmentChangeEvent;
  4. use CIC\DB\envLoader\db;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class DepartmentChangeSubscriber implements EventSubscriberInterface
  9. {
  10.     private db $db;
  11.     private ParameterBagInterface $params;
  12.     private $class;
  13.     private $sync false;
  14.     /**
  15.      * DepartmentChangeSubscriber constructor.
  16.      *
  17.      * @param EntityManagerInterface $em
  18.      */
  19.     public function __construct(db $dbParameterBagInterface $params)
  20.     {
  21.         $this->db $db;
  22.         $this->params $params;
  23.         if ($this->params->get('legacy.sync_departments')) {
  24.             $className $this->params->get('legacy.sync_departments');
  25.             $this->class = new $className($this->db);
  26.             // pass all tests
  27.             $this->sync true;
  28.         }
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             DepartmentChangeEvent::class => [
  34.                 ['onChange'],
  35.                 ['onCreate'],
  36.                 ],
  37.         ];
  38.     }
  39.     /**
  40.      * @return mixed
  41.      */
  42.     public function getEntity(DepartmentChangeEvent $event)
  43.     {
  44.         return $event->getDepartment();
  45.     }
  46.     public function onChange(DepartmentChangeEvent $event)
  47.     {
  48.         if (!$this->sync) {
  49.             return;
  50.         } // Sync is off
  51.         $entity $this->getEntity($event);
  52.         // no ID, not an update
  53.         if (!$entity->getId()) {
  54.             return;
  55.         }
  56.         if (method_exists($this->class'update')) {
  57.             $this->class->update($entity);
  58.         }
  59.     }
  60.     public function onCreate(DepartmentChangeEvent $event)
  61.     {
  62.         if (!$this->sync) {
  63.             return;
  64.         } // sync is off
  65.         $entity $this->getEntity($event);
  66.         // ID, not an insert
  67.         if ($entity->getId()) {
  68.             return;
  69.         }
  70.         if (method_exists($this->class'create')) {
  71.             $this->class->create($entity);
  72.         }
  73.     }
  74. }