2024-08-26 23:45:30 +02:00
< ? php
namespace App\MessageHandler ;
use App\Entity\Domain ;
use App\Entity\WatchList ;
use App\Message\OrderDomain ;
use App\Notifier\DomainOrderErrorNotification ;
use App\Notifier\DomainOrderNotification ;
use App\Repository\DomainRepository ;
use App\Repository\WatchListRepository ;
2024-08-27 00:09:25 +02:00
use App\Service\ChatNotificationService ;
2024-09-18 13:37:07 +02:00
use App\Service\Connector\AbstractProvider ;
2024-08-26 23:45:30 +02:00
use App\Service\StatService ;
use Psr\Cache\CacheItemPoolInterface ;
use Psr\Log\LoggerInterface ;
use Symfony\Component\HttpKernel\KernelInterface ;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface ;
use Symfony\Component\Mailer\MailerInterface ;
use Symfony\Component\Messenger\Attribute\AsMessageHandler ;
use Symfony\Component\Mime\Address ;
use Symfony\Component\Notifier\Recipient\Recipient ;
use Symfony\Contracts\HttpClient\HttpClientInterface ;
#[AsMessageHandler]
final readonly class OrderDomainHandler
{
private Address $sender ;
public function __construct (
string $mailerSenderEmail ,
string $mailerSenderName ,
private WatchListRepository $watchListRepository ,
private DomainRepository $domainRepository ,
private KernelInterface $kernel ,
private HttpClientInterface $client ,
private CacheItemPoolInterface $cacheItemPool ,
private MailerInterface $mailer ,
private LoggerInterface $logger ,
private StatService $statService ,
2024-09-18 13:37:07 +02:00
private ChatNotificationService $chatNotificationService ,
#[Autowire(service: 'service_container')]
private ContainerInterface $locator
2024-08-26 23:45:30 +02:00
) {
$this -> sender = new Address ( $mailerSenderEmail , $mailerSenderName );
}
/**
* @ throws TransportExceptionInterface
* @ throws \Symfony\Component\Notifier\Exception\TransportExceptionInterface
* @ throws \Throwable
*/
public function __invoke ( OrderDomain $message ) : void
{
/** @var WatchList $watchList */
$watchList = $this -> watchListRepository -> findOneBy ([ 'token' => $message -> watchListToken ]);
/** @var Domain $domain */
$domain = $this -> domainRepository -> findOneBy ([ 'ldhName' => $message -> ldhName ]);
$connector = $watchList -> getConnector ();
if ( null !== $connector && $domain -> getDeleted ()) {
$this -> logger -> notice ( 'Watchlist {watchlist} is linked to connector {connector}. A purchase attempt will be made for domain name {ldhName} with provider {provider}.' , [
'watchlist' => $message -> watchListToken ,
'connector' => $connector -> getId (),
'ldhName' => $message -> ldhName ,
'provider' => $connector -> getProvider () -> value ,
]);
try {
$provider = $connector -> getProvider ();
if ( null === $provider ) {
2024-08-30 12:54:42 +02:00
throw new \InvalidArgumentException ( 'Provider not found' );
2024-08-26 23:45:30 +02:00
}
$connectorProviderClass = $provider -> getConnectorProvider ();
/** @var AbstractProvider $connectorProvider */
2024-09-18 13:37:07 +02:00
$connectorProvider = $this -> locator -> get ( $connectorProviderClass );
$connectorProvider -> authenticate ( $connector -> getAuthData ());
2024-08-26 23:45:30 +02:00
$connectorProvider -> orderDomain ( $domain , $this -> kernel -> isDebug ());
2024-08-28 17:35:32 +02:00
$this -> statService -> incrementStat ( 'stats.domain.purchased' );
2024-08-26 23:45:30 +02:00
$notification = ( new DomainOrderNotification ( $this -> sender , $domain , $connector ));
$this -> mailer -> send ( $notification -> asEmailMessage ( new Recipient ( $watchList -> getUser () -> getEmail ())) -> getMessage ());
2024-08-27 00:09:25 +02:00
$this -> chatNotificationService -> sendChatNotification ( $watchList , $notification );
2024-08-26 23:45:30 +02:00
} catch ( \Throwable $exception ) {
$this -> logger -> warning ( 'Unable to complete purchase. An error message is sent to user {username}.' , [
'username' => $watchList -> getUser () -> getUserIdentifier (),
]);
$notification = ( new DomainOrderErrorNotification ( $this -> sender , $domain ));
$this -> mailer -> send ( $notification -> asEmailMessage ( new Recipient ( $watchList -> getUser () -> getEmail ())) -> getMessage ());
2024-08-27 00:09:25 +02:00
$this -> chatNotificationService -> sendChatNotification ( $watchList , $notification );
2024-08-26 23:45:30 +02:00
$this -> statService -> incrementStat ( 'stats.domain.purchase.failed' );
throw $exception ;
}
}
}
}