<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use ApiPlatform\Core\Util\RequestAttributesExtractor;
use App\Constant\AkoulaConstant;
use App\Entity\Image;
use App\Entity\OperationHistory;
use App\Entity\ReservationBedroom;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Vich\UploaderBundle\Storage\StorageInterface;
class ReservationBedroomSubscriber implements EventSubscriberInterface
{
private $storage;
private $managerRegistry;
private $tokenStorage;
public function __construct(StorageInterface $storage, TokenStorageInterface $tokenStorage, ManagerRegistry $managerRegistry)
{
$this->storage = $storage;
$this->managerRegistry = $managerRegistry;
$this->tokenStorage = $tokenStorage;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => [
['onPreSerialize', EventPriorities::PRE_SERIALIZE],
['beforeUpdate', EventPriorities::PRE_WRITE],
['onUpdate', EventPriorities::PRE_WRITE],
['beforeChecking', EventPriorities::POST_VALIDATE],
['onCheckout', EventPriorities::POST_VALIDATE],
],
];
}
public function beforeChecking(ViewEvent $event): void
{
$controllerResult = $event->getControllerResult();
$request = $event->getRequest();
if (!$controllerResult instanceof ReservationBedroom && ( $request->getMethod() !== Request::METHOD_PUT || $request->getMethod() !== Request::METHOD_POST)) {
return;
}
if(!$controllerResult->getId()){
return;
}
$existingResa = $this->managerRegistry->getRepository(ReservationBedroom::class)->getLastSavedData($controllerResult->getId())[0];
//var_dump($existingResa);
//var_dump($controllerResult->getEnterDate());
//die('icicicic');
if(!$existingResa['enter_date'] && $controllerResult->getBedroomChamber()){
$controllerResult->getBedroomChamber()->setIsOccuped(true);
}
elseif($existingResa['enter_date'] && $controllerResult->getBedroomChamber() != null && $controllerResult->getBedroomChamber()->getId() != $existingResa['bedroom_chamber_id']){
$controllerResult->getBedroomChamber()->setIsOccuped(false);
}
//die('icicicic2');
}
public function onCheckout(ViewEvent $event): void
{
$controllerResult = $event->getControllerResult();
$request = $event->getRequest();
if (!$controllerResult instanceof ReservationBedroom || $request->getMethod() !== Request::METHOD_PUT) {
return;
}
if(!$controllerResult->getId()){
return;
}
$existingResa = $this->managerRegistry->getRepository(ReservationBedroom::class)->getLastSavedData($controllerResult->getId())[0];
//var_dump($existingResa);
//var_dump($controllerResult->getEnterDate());
//die('icicicic');
if(!$existingResa['exit_date'] && $controllerResult->getBedroomChamber() && $controllerResult->getExitDate()){
$controllerResult->getBedroomChamber()->setIsOccuped(false);
}
//die('icicicic2');
}
public function beforeUpdate(ViewEvent $event): void
{
$controllerResult = $event->getControllerResult();
$request = $event->getRequest();
if (!$controllerResult instanceof ReservationBedroom || !str_contains($request->getPathInfo(), 'reservation_bedroom/update')) {
return;
}
try {
$existingResa = $this->managerRegistry->getRepository(ReservationBedroom::class)->find($controllerResult->getId());
$details = "Modif:";
if($existingResa instanceof ReservationBedroom){
$into = false;
if($existingResa->getStartDate() !== $controllerResult->getStartDate()){
$into = true;
$details.= " Changement de la date d'arrivée.".$existingResa->getStartDate()->format('d/m/Y')."--->". $controllerResult->getStartDate()->format('d/m/Y');
}
if($existingResa->getEndDate() !== $controllerResult->getEndDate()){
$into = true;
$details.= " Changement de la date de départ.".$existingResa->getEndDate()->format('d/m/Y')."--->". $controllerResult->getEndDate()->format('d/m/Y');
}
if($existingResa->getBedroom()->getTypeBedroom() !== $controllerResult->getBedroom()->getTypeBedroom()){
$into = true;
$details.= " Changement du type de chambre.".$existingResa->getBedroom()->getTypeBedroom()->getName()."--->". $controllerResult->getBedroom()->getTypeBedroom()->getName();
}
if($into){
$operationHistory = new OperationHistory();
$operationHistory->setDetail($details);
$operationHistory->setReservationBedroom($controllerResult);
$operationHistory->setUser($this->tokenStorage->getToken()->getUser());
$operationHistory->setAction(AkoulaConstant::ACTION_MODIFICATION);
$this->managerRegistry->getManagerForClass(OperationHistory::class)->persist($operationHistory);
}
}
} catch (\Exception $e){
}
}
public function onUpdate(ViewEvent $event): void {
$controllerResult = $event->getControllerResult();
$request = $event->getRequest();
if (!$controllerResult instanceof ReservationBedroom || $request->getMethod() !== Request::METHOD_PUT) {
return;
}
try {
$existingResa = $this->managerRegistry->getRepository(ReservationBedroom::class)->find($controllerResult->getId());
if($existingResa instanceof ReservationBedroom){
if($existingResa->getBedroomChamber() === null && $controllerResult->getBedroomChamber() !== null){
$details= "Attribution .".$controllerResult->getBedroomChamber()->getName();
$operationHistory = new OperationHistory();
$operationHistory->setDetail($details);
$operationHistory->setAction(AkoulaConstant::ACTION_ATTRIBUTION);
$operationHistory->setReservationBedroom($controllerResult);
$operationHistory->setUser($this->tokenStorage->getToken()->getUser());
}
if($existingResa->getIsCanceled() === null && $controllerResult->getIsCanceled() !== null){
$details= "ANNULATION RESA.CREATIONA RRHES";
//TODO add arrhes client
$operationHistory = new OperationHistory();
$operationHistory->setDetail($details);
$operationHistory->setAction(AkoulaConstant::ACTION_CANCELLATION);
$operationHistory->setReservationBedroom($controllerResult);
$operationHistory->setUser($this->tokenStorage->getToken()->getUser());
}
$this->managerRegistry->getManagerForClass(OperationHistory::class)->persist($operationHistory);
}
} catch (\Exception $e){
}
}
public function onPreSerialize(ViewEvent $event): void
{
$controllerResult = $event->getControllerResult();
$request = $event->getRequest();
$mediaObjects = $controllerResult;
if ($controllerResult instanceof Response || strpos($request->getPathInfo(), "statistique") || !$request->attributes->getBoolean('_api_respond', true)) {
return;
}
if ($request->getMethod() == Request::METHOD_DELETE) {
return;
}
if (($attributes = RequestAttributesExtractor::extractAttributes($request)) && \is_a($attributes['resource_class'], ReservationBedroom::class, true)) {
foreach ($mediaObjects as $mediaObject) {
if (!$mediaObject instanceof ReservationBedroom) {
continue;
}
foreach ($mediaObject->getBedroom()->getImages() as $img) {
$url = $_SERVER['HTTP_HOST'];
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
$schema = "https://";
} else {
$schema = "http://";
}
$img->contentUrl = $schema.$url.$this->storage->resolveUri($img, 'file');
}
}
}
}
}