src/Flexy/ShopBundle/EventSubscriber/EventsApiSubscriber.php line 46
<?php
namespace App\Flexy\ShopBundle\EventSubscriber;
use ApiPlatform\Symfony\EventListener\EventPriorities;
use App\Entity\Book;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\MailerInterface;
use App\Entity\User;
use App\Flexy\ShopBundle\Entity\Customer\Customer;
use App\Flexy\ShopBundle\Entity\Order\Order;
use App\Flexy\ShopBundle\Entity\Shipping\ShippingMethod;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Exception;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
use App\Security\AppAuthenticator;
final class EventsApiSubscriber implements EventSubscriberInterface
{
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)
{
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['sendMail', EventPriorities::POST_WRITE],
];
}
public function sendMail(ViewEvent $event): void
{
$entityInstance = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if ($entityInstance instanceof Order and Request::METHOD_PUT === $method) {
if($entityInstance->getStatus()=="waiting" and !$entityInstance->getIsChecked()){
if($this->security->getUser()){
if(in_array("ROLE_CUSTOMER",$this->security->getUser()->getRoles())){
$customer = $this->doctrine->getManager()->getRepository(Customer::class)->findOneBy(["user"=>$this->security->getUser()]);
if($customer){
$entityInstance->setCustomer($customer);
if(!$entityInstance->getFirstName()){
$entityInstance->setFirstName($customer->getFirstName());
}
if(!$entityInstance->getLastName()){
$entityInstance->getLastName();
}
if(!$entityInstance->getEmail()){
$entityInstance->getEmail();
}
if(!$entityInstance->getTel()){
$entityInstance->getEmail();
}
$this->doctrine->getManager()->persist($entityInstance);
$this->doctrine->getManager()->flush();
}
}
}
$update = new Update(
'https://hello.com/books/1',
json_encode(
[
'status' => 'success',
'entity'=>'Order',
'data'=>$entityInstance,
]
)
);
try {
if($entityInstance->getSource() != "ADMIN")
$this->hub->publish($update);
} catch (Exception) {
//dd($e);
}
}
}
}
}