<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\PitchDisponibility;
use App\Entity\Reservation;
use App\Repository\ReservationRepository;
use App\Service\CinetPayService;
use App\Service\ReservationService;
use App\Service\Sender;
use App\Service\Utils;
use JetBrains\PhpStorm\ArrayShape;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class ReservationSubscriber implements EventSubscriberInterface
{
public function __construct(
private UserPasswordHasherInterface $encoder,
private ReservationRepository $reservationRepository,
private Sender $sender,
private CinetPayService $cinetPayService
) {
}
#[
ArrayShape([KernelEvents::VIEW => "array"])
]
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => [
['prePersiste', EventPriorities::PRE_WRITE],
['postPersiste', EventPriorities::POST_WRITE]
],
];
}
public function prePersiste(ViewEvent $event): void
{
$reservation = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$reservation instanceof Reservation || Request::METHOD_POST !== $method) {
return;
}
if ($reservation->getPayements()->last()->getModePayment()->getLabel() === "Espèce") {
$reservation->getPayements()->last()->setStatut('succes');
}
$montant = $reservation->getQuantityTime()*$reservation->getPrice()->getMontant();
$reservation->setIsSold(ReservationService::isSold($montant, $reservation->getPayements()->toArray()));
}
public function postPersiste(ViewEvent $event): void
{
$reservation = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$reservation instanceof Reservation || Request::METHOD_POST !== $method) {
return;
}
if ($reservation->getPayements()->last()->getModePayment() !== null) {
if ($reservation->getPayements()->last()->getModePayment()->getLabel() !== "Espèce") {
$host = $event->getRequest()->headers->get('host');
$urlNotitfiction = "https://".$host."/payments/notification";
//$urlReturn = "https://".$host."/payments/return_url";
$reservation->urlPayement = $this->cinetPayService->initPayement($reservation->getPayements()->last(), $urlNotitfiction, null);
}
}
if ($event->getRequest()->query->has('mobile')) {
$host = $event->getRequest()->headers->get('host');
$urlNotitfiction = "https://".$host."/payments/notification";
//$urlReturn = "https://".$host."/payments/return_url";
$reservation->urlPayement = $this->cinetPayService->initPayement($reservation->getPayements()->last(), $urlNotitfiction, null);
}
if ($reservation->getPayements()->last()->getModePayment() !== null) {
if ($reservation->getPayements()->last()->getModePayment()->getLabel() === "Espèce") {
$this->sender->sendSms(
$reservation->getCustomer()->getPhone(),
sprintf(
"Nous avons le plaisir de vous confirmer votre réservation du %s à partir de %s .
Merci de choisir le Temple du foot. ",
$reservation->getDate()->format('d-m-Y'),
$reservation->getPitchDisponibilities()->toArray()[0]->getHoure()->getHoureStart()->format('H:i')
)
);
}
}
}
}