src/Flexy/FrontBundle/Themes/IlaveU/EventSubscriber/EventsApiSubscriber.php line 46

  1. <?php
  2. namespace App\Flexy\FrontBundle\Themes\IlaveU\EventSubscriber;
  3. use ApiPlatform\Symfony\EventListener\EventPriorities;
  4. use App\Entity\Book;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Mime\Email;
  10. use Symfony\Component\Mailer\MailerInterface;
  11. use App\Entity\User;
  12. use App\Flexy\ShopBundle\Entity\Customer\Customer;
  13. use App\Flexy\ShopBundle\Entity\Order\Order;
  14. use App\Flexy\ShopBundle\Entity\Shipping\ShippingMethod;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Doctrine\ORM\Events;
  17. use Doctrine\Persistence\Event\LifecycleEventArgs;
  18. use Exception;
  19. use Doctrine\Persistence\ManagerRegistry;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. use Symfony\Component\Mercure\HubInterface;
  22. use Symfony\Component\Mercure\Update;
  23. use Symfony\Component\Security\Core\Security;
  24. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  25. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  26. use App\Security\AppAuthenticator;
  27. final class EventsApiSubscriber implements EventSubscriberInterface
  28. {
  29.     
  30.     public function __construct(private readonly Security $security, protected RequestStack $requestStack, private readonly HubInterface $hub, private readonly UserPasswordHasherInterface $passwordEncoder, private readonly ManagerRegistry $doctrine, private readonly UserAuthenticatorInterface $userAuthenticator, private readonly AppAuthenticator $appAuthenticator)
  31.     {
  32.     }
  33.     
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             KernelEvents::VIEW => ['sendMail'EventPriorities::PRE_WRITE],
  38.         ];
  39.     }
  40.     public function sendMail(ViewEvent $event): void
  41.     {
  42.         $entityInstance $event->getControllerResult();
  43.         $method $event->getRequest()->getMethod();
  44.         
  45.         if ($entityInstance instanceof User and Request::METHOD_PUT === $method) {
  46.             
  47.             
  48.             
  49.             
  50.             
  51.         }
  52.         if ($entityInstance instanceof Customer and Request::METHOD_PATCH === $method) {
  53.            
  54.             $user $entityInstance->getUser();
  55.             
  56.             $user->setPassword(
  57.                 $this->passwordEncoder->hashPassword(
  58.                             $user,
  59.                             (string)$user->getPassword()
  60.                          ));
  61.             
  62.             $this->doctrine->getManager()->persist($user);
  63.             $this->doctrine->getManager()->flush();
  64.             
  65.             
  66.         }
  67.         
  68.         
  69.         if ($entityInstance instanceof Customer and Request::METHOD_PUT === $method) {
  70.                 $sponsorshipCustomer $this->doctrine->getManager()->getRepository(Customer::class)->findOneBy(["email"=>$entityInstance->getSponsorshipCode()]);
  71.                 if(!$sponsorshipCustomer){
  72.                     $entityInstance->setSponsorshipCode(null);
  73.                     $this->doctrine->getManager()->persist($entityInstance);
  74.                     $this->doctrine->getManager()->flush();
  75.                 }
  76.         }
  77.         
  78.         if ($entityInstance instanceof Order and Request::METHOD_PUT === $method) {
  79.             
  80.             if($entityInstance->getStatus()=="waiting" and !$entityInstance->getIsChecked()){
  81.                 if($this->security->getUser()){
  82.                     if(in_array("ROLE_CUSTOMER",$this->security->getUser()->getRoles())){
  83.     
  84.                         
  85.                         $customer $this->doctrine->getManager()->getRepository(Customer::class)->findOneBy(["user"=>$this->security->getUser()]);
  86.                         
  87.                         if($customer){
  88.                             
  89.                             $entityInstance->setCustomer($customer);
  90.                             if(!$entityInstance->getFirstName()){
  91.                                 $entityInstance->setFirstName($customer->getFirstName());
  92.                             }
  93.                             if(!$entityInstance->getLastName()){
  94.                                 $entityInstance->getLastName();
  95.                             }
  96.                             if(!$entityInstance->getEmail()){
  97.                                 $entityInstance->getEmail();
  98.                             }
  99.                             if(!$entityInstance->getTel()){
  100.                                 $entityInstance->getEmail();
  101.                             }
  102.         
  103.         
  104.                             $this->doctrine->getManager()->persist($entityInstance);
  105.                             $this->doctrine->getManager()->flush();
  106.                             
  107.                             
  108.                             
  109.                         }
  110.                     }
  111.                 }
  112.                 $update = new Update(
  113.                     'https://hello.com/books/1',
  114.                     json_encode(
  115.                         [
  116.                             'status' => 'success',
  117.                             'entity'=>'Order',
  118.                             'data'=>$entityInstance,
  119.                         ]
  120.                         )
  121.                 );
  122.                 try {
  123.                     if($entityInstance->getSource() != "ADMIN")
  124.                     $this->hub->publish($update);
  125.                 } catch (Exception) {
  126.                    //dd($e);
  127.                 }
  128.             }
  129.             
  130.             
  131.         }
  132.     }
  133. }