src/EventSubscriber/School/Student/StudentProfileCreateStudentWellbeingSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\School\Student;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\School\Student\StudentAcademic;
  5. use App\Entity\School\Student\StudentProfile;
  6. use App\Entity\School\Student\StudentWellbeing;
  7. use App\Event\DepartmentChangeEvent;
  8. use App\Event\School\Student\StudentProfileChangeEvent;
  9. use App\Service\Loader\Legacy\StudentWriterService;
  10. use CIC\DB\envLoader\db;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\ViewEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. class StudentProfileCreateStudentWellbeingSubscriber implements EventSubscriberInterface
  16. {
  17.     private StudentWriterService $wtiter;
  18.     private db $db;
  19.     private EntityManagerInterface $em;
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             StudentProfileChangeEvent::class => [
  24.                 ['onCreate']
  25.             ],
  26.             KernelEvents::VIEW => ['apiChange'EventPriorities::POST_WRITE],
  27.         ];
  28.     }
  29.     public function __construct(db $db,EntityManagerInterface $em)
  30.     {
  31.         $this->db $db;
  32.         $this->em $em;
  33.     }
  34.     public function onCreate(StudentProfileChangeEvent $event)
  35.     {
  36.         $this->addRecord($event->getStudent());
  37.     }
  38.     public function apiChange(ViewEvent $event) {
  39.         $object=$event->getControllerResult();
  40.         $method=$event->getRequest()->getMethod();
  41.         if(!$object instanceof StudentProfile) {
  42.             return;
  43.         }
  44.         if($method==$event->getRequest()::METHOD_POST)
  45.             $this->addRecord($object);
  46.     }
  47.     function addRecord(StudentProfile $student)
  48.     {
  49.         if(!$student->getWellbeing()) {
  50.             $record=new StudentWellbeing();
  51.             $record->setStudent($student);
  52.             $this->em->persist($record);
  53.             $this->em->flush();
  54.         }
  55.     }
  56. }