<?php
namespace App\EventSubscriber\School\Student;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\School\Student\StudentAcademic;
use App\Entity\School\Student\StudentProfile;
use App\Entity\School\Student\StudentWellbeing;
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\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class StudentProfileCreateStudentWellbeingSubscriber implements EventSubscriberInterface
{
private StudentWriterService $wtiter;
private db $db;
private EntityManagerInterface $em;
public static function getSubscribedEvents()
{
return [
StudentProfileChangeEvent::class => [
['onCreate']
],
KernelEvents::VIEW => ['apiChange', EventPriorities::POST_WRITE],
];
}
public function __construct(db $db,EntityManagerInterface $em)
{
$this->db = $db;
$this->em = $em;
}
public function onCreate(StudentProfileChangeEvent $event)
{
$this->addRecord($event->getStudent());
}
public function apiChange(ViewEvent $event) {
$object=$event->getControllerResult();
$method=$event->getRequest()->getMethod();
if(!$object instanceof StudentProfile) {
return;
}
if($method==$event->getRequest()::METHOD_POST)
$this->addRecord($object);
}
function addRecord(StudentProfile $student)
{
if(!$student->getWellbeing()) {
$record=new StudentWellbeing();
$record->setStudent($student);
$this->em->persist($record);
$this->em->flush();
}
}
}