src/Flexy/ShopBundle/EventSubscriber/EventsApiSubscriber.php line 46

  1. <?php
  2. namespace App\Flexy\ShopBundle\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::POST_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 Order and Request::METHOD_PUT === $method) {
  46.             
  47.             if($entityInstance->getStatus()=="waiting" and !$entityInstance->getIsChecked()){
  48.                 if($this->security->getUser()){
  49.                     if(in_array("ROLE_CUSTOMER",$this->security->getUser()->getRoles())){
  50.     
  51.                         
  52.                         $customer $this->doctrine->getManager()->getRepository(Customer::class)->findOneBy(["user"=>$this->security->getUser()]);
  53.                         
  54.                         if($customer){
  55.                             
  56.                             $entityInstance->setCustomer($customer);
  57.                             if(!$entityInstance->getFirstName()){
  58.                                 $entityInstance->setFirstName($customer->getFirstName());
  59.                             }
  60.                             if(!$entityInstance->getLastName()){
  61.                                 $entityInstance->getLastName();
  62.                             }
  63.                             if(!$entityInstance->getEmail()){
  64.                                 $entityInstance->getEmail();
  65.                             }
  66.                             if(!$entityInstance->getTel()){
  67.                                 $entityInstance->getEmail();
  68.                             }
  69.         
  70.         
  71.                             $this->doctrine->getManager()->persist($entityInstance);
  72.                             $this->doctrine->getManager()->flush();
  73.                             
  74.                             
  75.                             
  76.                         }
  77.                     }
  78.                 }
  79.                 $update = new Update(
  80.                     'https://hello.com/books/1',
  81.                     json_encode(
  82.                         [
  83.                             'status' => 'success',
  84.                             'entity'=>'Order',
  85.                             'data'=>$entityInstance,
  86.                         ]
  87.                         )
  88.                 );
  89.                 try {
  90.                     if($entityInstance->getSource() != "ADMIN")
  91.                     $this->hub->publish($update);
  92.                 } catch (Exception) {
  93.                    //dd($e);
  94.                 }
  95.             }
  96.             
  97.             
  98.         }
  99.     }
  100. }