<?phpnamespace App\EventSubscriber\cic\legacy\tracker\student;use ApiPlatform\Core\EventListener\EventPriorities;use App\Entity\School\Student\StudentProfile;use App\Event\DepartmentChangeEvent;use App\Event\School\Student\StudentProfileChangeEvent;use App\Service\Loader\Legacy\StudentWriterService;use CIC\DB\envLoader\db;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpKernel\Event\ViewEvent;use Symfony\Component\HttpKernel\KernelEvents;class StudentProfileUpdateSubscriber implements EventSubscriberInterface{ private StudentWriterService $wtiter; private db $db; private EntityManagerInterface $em; public static function getSubscribedEvents() { return [ StudentProfileChangeEvent::class => [ ['onChange'], ['onCreate'] ], KernelEvents::VIEW => ['apiChange', EventPriorities::POST_WRITE], ]; } public function __construct(StudentWriterService $wtiter, db $db,EntityManagerInterface $em) { $this->wtiter = $wtiter; $this->db = $db; $this->em = $em; } public function onChange(StudentProfileChangeEvent $event) { $this->updateStudent($event->getStudent()); //TODO // a change was made // } public function onCreate(StudentProfileChangeEvent $event) { $this->createStudent($event->getStudent()); } public function apiChange(ViewEvent $event) { $object=$event->getControllerResult(); $method=$event->getRequest()->getMethod(); if(!$object instanceof StudentProfile) { return; } if($method==Request::METHOD_PUT) $this->updateStudent($object); if($method==Request::METHOD_POST) $this->createStudent($object); //handle it } private function updateStudent(StudentProfile $student) { $this->transliterate($student); $this->wtiter->update($student);// $this->updateStudent($student); } private function createStudent(StudentProfile $student) { $this->wtiter->create($student); $this->transliterate($student); $student->setSinfoId($this->db->insert_id()); $this->em->persist($student); $this->em->flush();// $this->createStudent($student); } public function transliterate(StudentProfile $object) { $object->setGivenName( $this->trans($object->getGivenName()) ); $object->setMiddleName( $this->trans($object->getMiddleName()) ); $object->setLastName( $this->trans($object->getLastName()) ); } private function trans($string) { return iconv('UTF-8', 'ASCII//TRANSLIT', $string); }}