src/EventSubscriber/CinetpaySubscriber.php line 63

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Constant\AkoulaConstant;
  5. use App\Dto\ReservationEventInput;
  6. use App\Entity\Reservation;
  7. use App\Entity\ReservationBedroom;
  8. use App\Entity\ReservationEvent;
  9. use App\Entity\ReservationPayment;
  10. use App\Entity\User;
  11. use App\Repository\CustomerReservationEventFormulatRepository;
  12. use App\Repository\CustomerReservationEventRepository;
  13. use App\Repository\ReservationBedroomRepository;
  14. use App\Repository\ReservationPaymentRepository;
  15. use App\Service\NotificationService;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpKernel\Event\RequestEvent;
  20. use Symfony\Component\HttpKernel\Event\ViewEvent;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  23. use Symfony\Component\Security\Core\Security;
  24. final class CinetpaySubscriber implements EventSubscriberInterface
  25. {
  26.     private $reservationPaymentRepo;
  27.     private $reservationBedroomRepository;
  28.     private $customerReservationEventRepo;
  29.     private $reservationEvFomulaRepo;
  30.     private $entityManager;
  31.     private $tokenStorage;
  32.     private $security;
  33.     public function __construct(ReservationPaymentRepository $repositoryReservationBedroomRepository $reservationBedroomRepository,
  34.                                 EntityManagerInterface $entityManager,TokenStorageInterface $tokenStorage,Security $security,
  35.                                 CustomerReservationEventRepository $customerReservationEventRepo,
  36.                                 CustomerReservationEventFormulatRepository $customerReservationEventFormulatRepository
  37.     )
  38.     {
  39.         $this->reservationPaymentRepo $repository;
  40.         $this->reservationBedroomRepository $reservationBedroomRepository;
  41.         $this->entityManager $entityManager;
  42.         $this->tokenStorage $tokenStorage;
  43.         $this->security $security;
  44.         $this->customerReservationEventRepo $customerReservationEventRepo;
  45.         $this->reservationEvFomulaRepo $customerReservationEventFormulatRepository;
  46.     }
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             KernelEvents::VIEW => [['changeStatus'EventPriorities::POST_WRITE]],
  51.         ];
  52.     }
  53.     public function changeStatus(ViewEvent $event)
  54.     {
  55.         $reservation $event->getControllerResult();
  56.         $method $event->getRequest()->getMethod();
  57.         if (!$reservation instanceof Reservation || Request::METHOD_POST !== $method) {
  58.             return;
  59.         }
  60.         $findResaBedrooms $this->reservationBedroomRepository->findBy(['reservation'=>$reservation]);
  61.         if (!is_null($findResaBedrooms)) {
  62.             foreach ($findResaBedrooms as $bedroom){
  63.                 $totalDirectPayElement $this->reservationPaymentRepo->getTotalPayByBedroom($bedroom->getId());
  64.             }
  65.             $totalPay 0;
  66.             if (!is_null($totalDirectPayElement)) {
  67.                 $totalPay $totalDirectPayElement['total'];
  68.             }
  69.             $totalCinetPayElement $this->reservationBedroomRepository->getTotalPayByBedroom($bedroom->getId());
  70.             $totalCinetPay 0;
  71.             if (!is_null($totalCinetPayElement)) {
  72.                 $totalCinetPay $totalCinetPayElement['total'];
  73.             }
  74.             if ($bedroom->getAmount() <= ($totalPay $totalCinetPay)) {
  75.                 $bedroom->setPaymentStatus(AkoulaConstant::MODE_PAYED);
  76.             }
  77.             $this->entityManager->flush();
  78.         }else{
  79.             $findCustomerReaseEvents$this->customerReservationEventRepo->findBy(['reservation'=>$reservation]);
  80.             if(!is_null($findCustomerReaseEvents)){
  81.                 foreach ($findCustomerReaseEvents as $customerReaseEvent){
  82.                     $totalDirectPayElement $this->reservationPaymentRepo->getTotalPayByResaEvent($customerReaseEvent->getReservationEvent()->getId());
  83.                     $totalPay 0;
  84.                     if (!is_null($totalDirectPayElement)) {
  85.                         $totalPay $totalDirectPayElement['total'];
  86.                     }
  87.                     $totalCinetPayElement $this->customerReservationEventRepo->getTotalPayByReservationEvent($customerReaseEvent->getReservationEvent()->getId());
  88.                     $totalCinetPay 0;
  89.                     if (!is_null($totalCinetPayElement)) {
  90.                         $totalCinetPay $totalCinetPayElement['total'];
  91.                     }
  92.                     $totalToPayElement $this->reservationEvFomulaRepo->getTotalToPayByResaEvent($customerReaseEvent->getReservationEvent());
  93.                     $totalToPay 0;
  94.                     if(!is_null($totalToPayElement)){
  95.                         $totalToPay $totalToPayElement['total'];
  96.                     }
  97.                     if ($totalToPay <= ($totalPay $totalCinetPay)) {
  98.                         $customerReaseEvent->getReservationEvent()->setPaymentStatus(AkoulaConstant::MODE_PAYED);
  99.                     }
  100.                     $this->entityManager->flush();
  101.                 }
  102.             }
  103.         }
  104.     }
  105. }