src/EventSubscriber/cic/legacy/tracker/student/StudentProfileUpdateSubscriber.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\cic\legacy\tracker\student;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\School\Student\StudentProfile;
  5. use App\Event\DepartmentChangeEvent;
  6. use App\Event\School\Student\StudentProfileChangeEvent;
  7. use App\Service\Loader\Legacy\StudentWriterService;
  8. use CIC\DB\envLoader\db;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. class StudentProfileUpdateSubscriber implements EventSubscriberInterface
  15. {
  16.     private StudentWriterService $wtiter;
  17.     private db $db;
  18.     private EntityManagerInterface $em;
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             StudentProfileChangeEvent::class => [
  23.                 ['onChange'],
  24.                 ['onCreate']
  25.             ],
  26.             KernelEvents::VIEW => ['apiChange'EventPriorities::POST_WRITE],
  27.         ];
  28.     }
  29.     public function __construct(StudentWriterService $wtiterdb $db,EntityManagerInterface $em)
  30.     {
  31.         $this->wtiter $wtiter;
  32.         $this->db $db;
  33.         $this->em $em;
  34.     }
  35.     public function onChange(StudentProfileChangeEvent $event)
  36.     {
  37.         $this->updateStudent($event->getStudent());
  38.         //TODO
  39.         // a change was made
  40.         //
  41.     }
  42.     public function onCreate(StudentProfileChangeEvent $event)
  43.     {
  44.         $this->createStudent($event->getStudent());
  45.     }
  46.     public function apiChange(ViewEvent $event) {
  47.         $object=$event->getControllerResult();
  48.         $method=$event->getRequest()->getMethod();
  49.         if(!$object instanceof StudentProfile) {
  50.             return;
  51.         }
  52.         if($method==Request::METHOD_PUT)
  53.             $this->updateStudent($object);
  54.         if($method==Request::METHOD_POST)
  55.             $this->createStudent($object);
  56.         //handle it
  57.     }
  58.     private function updateStudent(StudentProfile $student) {
  59.         $this->transliterate($student);
  60.         $this->wtiter->update($student);
  61. //        $this->updateStudent($student);
  62.     }
  63.     private function createStudent(StudentProfile $student) {
  64.         $this->wtiter->create($student);
  65.         $this->transliterate($student);
  66.         $student->setSinfoId($this->db->insert_id());
  67.         $this->em->persist($student);
  68.         $this->em->flush();
  69. //        $this->createStudent($student);
  70.     }
  71.     public function transliterate(StudentProfile $object)
  72.     {
  73.         $object->setGivenName(
  74.             $this->trans($object->getGivenName())
  75.         );
  76.         $object->setMiddleName(
  77.             $this->trans($object->getMiddleName())
  78.         );
  79.         $object->setLastName(
  80.             $this->trans($object->getLastName())
  81.         );
  82.     }
  83.     private  function trans($string)
  84.     {
  85.         return iconv('UTF-8''ASCII//TRANSLIT'$string);
  86.     }
  87. }