src/EventSubscriber/ReservationBedroomSubscriber.php line 176

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use ApiPlatform\Core\Util\RequestAttributesExtractor;
  5. use App\Constant\AkoulaConstant;
  6. use App\Entity\Image;
  7. use App\Entity\OperationHistory;
  8. use App\Entity\ReservationBedroom;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Event\ViewEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Vich\UploaderBundle\Storage\StorageInterface;
  17. class ReservationBedroomSubscriber implements EventSubscriberInterface
  18. {
  19.     private $storage;
  20.     private $managerRegistry;
  21.     private $tokenStorage;
  22.     public function __construct(StorageInterface $storageTokenStorageInterface $tokenStorageManagerRegistry $managerRegistry)
  23.     {
  24.         $this->storage $storage;
  25.         $this->managerRegistry $managerRegistry;
  26.         $this->tokenStorage $tokenStorage;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             KernelEvents::VIEW => [
  32.                 ['onPreSerialize'EventPriorities::PRE_SERIALIZE],
  33.                 ['beforeUpdate'EventPriorities::PRE_WRITE],
  34.                 ['onUpdate'EventPriorities::PRE_WRITE],
  35.                 ['beforeChecking'EventPriorities::POST_VALIDATE],
  36.                 ['onCheckout'EventPriorities::POST_VALIDATE],
  37.             ],
  38.         ];
  39.     }
  40.     public function beforeChecking(ViewEvent $event): void
  41.     {
  42.       $controllerResult $event->getControllerResult();
  43.       $request $event->getRequest();
  44.       if (!$controllerResult instanceof ReservationBedroom && ( $request->getMethod() !== Request::METHOD_PUT || $request->getMethod() !== Request::METHOD_POST)) {
  45.           return;
  46.       }
  47.       if(!$controllerResult->getId()){
  48.         return;
  49.       }
  50.       $existingResa $this->managerRegistry->getRepository(ReservationBedroom::class)->getLastSavedData($controllerResult->getId())[0];
  51.       //var_dump($existingResa);
  52.       //var_dump($controllerResult->getEnterDate());
  53.       //die('icicicic');
  54.       if(!$existingResa['enter_date'] && $controllerResult->getBedroomChamber()){
  55.         $controllerResult->getBedroomChamber()->setIsOccuped(true);
  56.       }
  57.       elseif($existingResa['enter_date'] && $controllerResult->getBedroomChamber() != null &&  $controllerResult->getBedroomChamber()->getId() != $existingResa['bedroom_chamber_id']){
  58.           $controllerResult->getBedroomChamber()->setIsOccuped(false);
  59.       }
  60.       //die('icicicic2');
  61.     }
  62.     public function onCheckout(ViewEvent $event): void
  63.     {
  64.       $controllerResult $event->getControllerResult();
  65.       $request $event->getRequest();
  66.       if (!$controllerResult instanceof ReservationBedroom || $request->getMethod() !== Request::METHOD_PUT) {
  67.           return;
  68.       }
  69.       if(!$controllerResult->getId()){
  70.         return;
  71.       }
  72.       $existingResa $this->managerRegistry->getRepository(ReservationBedroom::class)->getLastSavedData($controllerResult->getId())[0];
  73.       //var_dump($existingResa);
  74.       //var_dump($controllerResult->getEnterDate());
  75.       //die('icicicic');
  76.       if(!$existingResa['exit_date'] && $controllerResult->getBedroomChamber() && $controllerResult->getExitDate()){
  77.         $controllerResult->getBedroomChamber()->setIsOccuped(false);
  78.       }
  79.       //die('icicicic2');
  80.     }
  81.     public function beforeUpdate(ViewEvent $event): void
  82.     {
  83.         $controllerResult $event->getControllerResult();
  84.         $request $event->getRequest();
  85.         if (!$controllerResult instanceof ReservationBedroom || !str_contains($request->getPathInfo(), 'reservation_bedroom/update')) {
  86.             return;
  87.         }
  88.         try {
  89.             $existingResa $this->managerRegistry->getRepository(ReservationBedroom::class)->find($controllerResult->getId());
  90.             $details "Modif:";
  91.             if($existingResa instanceof  ReservationBedroom){
  92.                 $into false;
  93.                 if($existingResa->getStartDate() !== $controllerResult->getStartDate()){
  94.                     $into true;
  95.                     $details.= " Changement de la date d'arrivée.".$existingResa->getStartDate()->format('d/m/Y')."--->"$controllerResult->getStartDate()->format('d/m/Y');
  96.                 }
  97.                 if($existingResa->getEndDate() !== $controllerResult->getEndDate()){
  98.                     $into true;
  99.                     $details.= " Changement de la date de départ.".$existingResa->getEndDate()->format('d/m/Y')."--->"$controllerResult->getEndDate()->format('d/m/Y');
  100.                 }
  101.                 if($existingResa->getBedroom()->getTypeBedroom() !== $controllerResult->getBedroom()->getTypeBedroom()){
  102.                     $into true;
  103.                     $details.= " Changement du type de chambre.".$existingResa->getBedroom()->getTypeBedroom()->getName()."--->"$controllerResult->getBedroom()->getTypeBedroom()->getName();
  104.                 }
  105.                 if($into){
  106.                     $operationHistory = new OperationHistory();
  107.                     $operationHistory->setDetail($details);
  108.                     $operationHistory->setReservationBedroom($controllerResult);
  109.                     $operationHistory->setUser($this->tokenStorage->getToken()->getUser());
  110.                     $operationHistory->setAction(AkoulaConstant::ACTION_MODIFICATION);
  111.                     $this->managerRegistry->getManagerForClass(OperationHistory::class)->persist($operationHistory);
  112.                 }
  113.             }
  114.         } catch (\Exception $e){
  115.         }
  116.     }
  117.     public function onUpdate(ViewEvent $event): void {
  118.         $controllerResult $event->getControllerResult();
  119.         $request $event->getRequest();
  120.         if (!$controllerResult instanceof ReservationBedroom || $request->getMethod() !== Request::METHOD_PUT) {
  121.             return;
  122.         }
  123.         try {
  124.             $existingResa $this->managerRegistry->getRepository(ReservationBedroom::class)->find($controllerResult->getId());
  125.             if($existingResa instanceof  ReservationBedroom){
  126.                 if($existingResa->getBedroomChamber() === null && $controllerResult->getBedroomChamber() !== null){
  127.                     $details"Attribution .".$controllerResult->getBedroomChamber()->getName();
  128.                     $operationHistory = new OperationHistory();
  129.                     $operationHistory->setDetail($details);
  130.                     $operationHistory->setAction(AkoulaConstant::ACTION_ATTRIBUTION);
  131.                     $operationHistory->setReservationBedroom($controllerResult);
  132.                     $operationHistory->setUser($this->tokenStorage->getToken()->getUser());
  133.                 }
  134.                 if($existingResa->getIsCanceled() === null && $controllerResult->getIsCanceled() !== null){
  135.                     $details"ANNULATION RESA.CREATIONA RRHES";
  136.                     //TODO add arrhes client
  137.                     $operationHistory = new OperationHistory();
  138.                     $operationHistory->setDetail($details);
  139.                     $operationHistory->setAction(AkoulaConstant::ACTION_CANCELLATION);
  140.                     $operationHistory->setReservationBedroom($controllerResult);
  141.                     $operationHistory->setUser($this->tokenStorage->getToken()->getUser());
  142.                 }
  143.                 $this->managerRegistry->getManagerForClass(OperationHistory::class)->persist($operationHistory);
  144.             }
  145.         } catch (\Exception $e){
  146.         }
  147.     }
  148.     public function onPreSerialize(ViewEvent $event): void
  149.     {
  150.         $controllerResult $event->getControllerResult();
  151.         $request $event->getRequest();
  152.         $mediaObjects $controllerResult;
  153.         if ($controllerResult instanceof Response || strpos($request->getPathInfo(), "statistique") || !$request->attributes->getBoolean('_api_respond'true)) {
  154.             return;
  155.         }
  156.         if ($request->getMethod() == Request::METHOD_DELETE) {
  157.             return;
  158.         }
  159.         if (($attributes RequestAttributesExtractor::extractAttributes($request)) && \is_a($attributes['resource_class'], ReservationBedroom::class, true)) {
  160.             foreach ($mediaObjects as $mediaObject) {
  161.                 if (!$mediaObject instanceof ReservationBedroom) {
  162.                     continue;
  163.                 }
  164.                 foreach ($mediaObject->getBedroom()->getImages() as $img) {
  165.                     $url $_SERVER['HTTP_HOST'];
  166.                     if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
  167.                         $schema "https://";
  168.                     } else {
  169.                         $schema "http://";
  170.                     }
  171.                     $img->contentUrl $schema.$url.$this->storage->resolveUri($img'file');
  172.                 }
  173.             }
  174.         }
  175.     }
  176. }