src/EventSubscriber/cic/residence/BookingSubscriber.php line 95

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\cic\residence;
  3. use App\Event\Residence\BookingResidenceUpdatedEvent;
  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 BookingSubscriber implements EventSubscriberInterface
  9. {
  10.     private db $db;
  11.     private ParameterBagInterface $params;
  12.     private $exporter;
  13.     private $sync false;
  14.     private EntityManagerInterface $em;
  15.     /**
  16.      * DepartmentChangeSubscriber constructor.
  17.      */
  18.     public function __construct(db $dbEntityManagerInterface $emParameterBagInterface $params)
  19.     {
  20.         $this->db $db;
  21.         $this->params $params;
  22.         $this->em $em;
  23.     }
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             BookingResidenceUpdatedEvent::class => [
  28.                 ['onCheckIn'],
  29.                 ['onCheckOut'],
  30.                 ['onCreate'],
  31.                 ['onChange'],
  32.                 ],
  33.         ];
  34.     }
  35.     public function onCheckIn(BookingResidenceUpdatedEvent $event)
  36.     {
  37.         if (!$event->getBookings()->getCheckInDate()) {
  38.             return;
  39.         }
  40.         $now = new \DateTime();
  41.         dump($event->getBookings()->getCheckInDate()->format('Ymd'));
  42.         dump($now->format('Ymd'));
  43.         if ($event->getBookings()->getCheckInDate()->format('Ymd') != $now->format('Ymd')) {
  44.             return;
  45.         }
  46.         // set the room as occupied
  47.         $bed $event->getBookings()->getBedSpace();
  48.         $bed->setOccupancyStatus('Occuipied');
  49.         $bed->setOccupant($event->getBookings());
  50.         $this->em->persist($bed);
  51.         $this->em->flush();
  52.         $this->updateStudent('check-in'$event);
  53.         return;
  54.     }
  55.     public function onCheckOut(BookingResidenceUpdatedEvent $event)
  56.     {
  57.         if (!$event->getBookings()->getCheckOutDate()) {
  58.             return;
  59.         }
  60.         $now = new \DateTime();
  61.         if ($event->getBookings()->getCheckOutDate()->format('Ymd') != $now->format('Ymd')) {
  62.             return;
  63.         }
  64.         // set the room as occupied
  65.         $bed $event->getBookings()->getBedSpace();
  66.         $bed->setOccupancyStatus('Available');
  67.         $bed->setOccupant(null);
  68.         $this->em->persist($bed);
  69.         $this->em->flush();
  70.         $this->updateStudent('check-out'$event);
  71.         return;
  72.     }
  73.     public function onCreate(BookingResidenceUpdatedEvent $event)
  74.     {
  75.     }
  76.     public function onChange(BookingResidenceUpdatedEvent $event)
  77.     {
  78.     }
  79.     public function updateStudent(string $typeBookingResidenceUpdatedEvent $event)
  80.     {
  81.         $supports = ['check-in''check-out'];
  82.         if (!in_array($type$supports)) {
  83.             return;
  84.         }
  85.         if ($type == 'check-in') {
  86.             $address1 $event->getBookings()->getBedSpace()->getRoom()->getBuilding()->getAddress1();
  87.             $address2 $event->getBookings()->getBedSpace()->getName();
  88.         }
  89.         if ($type == 'check-out') {
  90.             $address1 'Moved out of Residence';
  91.             $address2 'Requires Update';
  92.         }
  93.         $student $event->getBookings()->getStudent();
  94.         $student->setAddress($address1);
  95.         $student->setAddress2($address2);
  96.         $this->em->persist($student);
  97.         $this->em->flush();
  98.     }
  99. }