diff --git a/src/MessageHandler/OrderDomainHandler.php b/src/MessageHandler/OrderDomainHandler.php index 3cb1b5c..cd792a3 100644 --- a/src/MessageHandler/OrderDomainHandler.php +++ b/src/MessageHandler/OrderDomainHandler.php @@ -94,8 +94,6 @@ final readonly class OrderDomainHandler * If no errors occur, the purchase is attempted. */ - $connectorProvider->authenticate($connector->getAuthData()); - $connectorProvider->orderDomain($domain, $this->kernel->isDebug()); /* diff --git a/src/MessageHandler/UpdateDomainsFromWatchlistHandler.php b/src/MessageHandler/UpdateDomainsFromWatchlistHandler.php index 978f019..7faf492 100644 --- a/src/MessageHandler/UpdateDomainsFromWatchlistHandler.php +++ b/src/MessageHandler/UpdateDomainsFromWatchlistHandler.php @@ -8,12 +8,23 @@ use App\Message\OrderDomain; use App\Message\SendDomainEventNotif; use App\Message\UpdateDomainsFromWatchlist; use App\Notifier\DomainDeletedNotification; +use App\Notifier\DomainOrderErrorNotification; +use App\Notifier\DomainOrderNotification; use App\Notifier\DomainUpdateErrorNotification; use App\Repository\WatchListRepository; use App\Service\ChatNotificationService; +use App\Service\Connector\AbstractProvider; +use App\Service\Connector\CheckDomainProviderInterface; +use App\Service\Connector\EppClientProvider; +use App\Service\InfluxdbService; use App\Service\RDAPService; +use App\Service\StatService; +use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; +use Symfony\Component\DependencyInjection\Attribute\Autowire; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Messenger\Attribute\AsMessageHandler; @@ -40,6 +51,14 @@ final readonly class UpdateDomainsFromWatchlistHandler private MessageBusInterface $bus, private WatchListRepository $watchListRepository, private LoggerInterface $logger, + #[Autowire(service: 'service_container')] + private ContainerInterface $locator, + private EntityManagerInterface $em, + private KernelInterface $kernel, + private StatService $statService, + private InfluxdbService $influxdbService, + #[Autowire(param: 'influxdb_enabled')] + private bool $influxdbEnabled, ) { $this->sender = new Address($mailerSenderEmail, $mailerSenderName); } @@ -63,6 +82,73 @@ final readonly class UpdateDomainsFromWatchlistHandler 'token' => $message->watchListToken, ]); + $connector = $watchList->getConnector(); + + if (null !== $connector && null !== $connector->getProvider()) { + $provider = $connector->getProvider(); + $connectorProviderClass = $provider->getConnectorProvider(); + /** @var AbstractProvider $connectorProvider */ + /** @var EppClientProvider $connectorProvider */ + $connectorProvider = $this->locator->get($connectorProviderClass); + } + + if (isset($connectorProvider) && $connectorProvider instanceof CheckDomainProviderInterface) { + $this->logger->notice('Watchlist {watchlist} is linked to connector {connector}.', [ + 'watchlist' => $message->watchListToken, + 'connector' => $connector->getId(), + ]); + + try { + $checkedDomains = $connectorProvider->checkDomains( + ...array_unique(array_map(fn (Domain $d) => $d->getLdhName(), $watchList->getDomains()->toArray())) + ); + } catch (\Throwable $exception) { + $this->logger->warning('Unable to check domain names availability with connector {connector}.', [ + 'connector' => $connector->getId(), + ]); + + throw $exception; + } + + foreach ($checkedDomains as $domain) { + try { + $connectorProvider->orderDomain($this->em->getReference(Domain::class, $domain), $this->kernel->isDebug()); + + $this->logger->notice('Watchlist {watchlist} is linked to connector {connector}. A purchase was successfully made for domain {ldhName}.', [ + 'watchlist' => $message->watchListToken, + 'connector' => $connector->getId(), + 'ldhName' => $domain, + ]); + + // TODO : remove dupcode + $this->statService->incrementStat('stats.domain.purchased'); + if ($this->influxdbEnabled) { + $this->influxdbService->addDomainOrderPoint($connector, $domain, true); + } + $notification = (new DomainOrderNotification($this->sender, $domain, $connector)); + $this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage()); + $this->chatNotificationService->sendChatNotification($watchList, $notification); + } catch (\Throwable $exception) { + // TODO : remove dupcode + $this->logger->warning('Unable to complete purchase. An error message is sent to user {username}.', [ + 'username' => $watchList->getUser()->getUserIdentifier(), + ]); + + $this->statService->incrementStat('stats.domain.purchase.failed'); + if ($this->influxdbEnabled) { + $this->influxdbService->addDomainOrderPoint($connector, $domain, false); + } + $notification = (new DomainOrderErrorNotification($this->sender, $domain)); + $this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage()); + $this->chatNotificationService->sendChatNotification($watchList, $notification); + + throw $exception; + } + } + + return; + } + /* * A domain name is updated if one or more of these conditions are met: * - was updated more than 7 days ago @@ -89,7 +175,7 @@ final readonly class UpdateDomainsFromWatchlistHandler $this->chatNotificationService->sendChatNotification($watchList, $notification); } - if (null !== $watchList->getConnector()) { + if (null !== $connector) { /* * If the domain name no longer appears in the WHOIS AND a connector is associated with this Watchlist, * this connector is used to purchase the domain name. diff --git a/src/Service/Connector/EppClientProviderInterface.php b/src/Service/Connector/CheckDomainProviderInterface.php similarity index 73% rename from src/Service/Connector/EppClientProviderInterface.php rename to src/Service/Connector/CheckDomainProviderInterface.php index e2540ec..f0bc649 100644 --- a/src/Service/Connector/EppClientProviderInterface.php +++ b/src/Service/Connector/CheckDomainProviderInterface.php @@ -2,7 +2,7 @@ namespace App\Service\Connector; -interface EppClientProviderInterface +interface CheckDomainProviderInterface { public function checkDomains(string ...$domains): array; } diff --git a/src/Service/Connector/EppClientProvider.php b/src/Service/Connector/EppClientProvider.php index 0b4c857..d9a3363 100644 --- a/src/Service/Connector/EppClientProvider.php +++ b/src/Service/Connector/EppClientProvider.php @@ -15,7 +15,7 @@ use Psr\Cache\CacheItemInterface; use Psr\Cache\CacheItemPoolInterface; use Psr\Cache\InvalidArgumentException; -class EppClientProvider extends AbstractProvider implements EppClientProviderInterface +class EppClientProvider extends AbstractProvider implements CheckDomainProviderInterface { private eppConnection $eppClient; @@ -62,7 +62,9 @@ class EppClientProvider extends AbstractProvider implements EppClientProviderInt $d->addContact(new eppContactHandle($contact, $type)); } - $this->eppClient->request(new eppCreateDomainRequest($d)); + if (!$dryRun) { + $this->eppClient->request(new eppCreateDomainRequest($d)); + } $this->eppClient->logout(); $this->eppClient->disconnect();