Skip to content

Commit

Permalink
Merge pull request #48 from maelgangloff/refactor/domain-message
Browse files Browse the repository at this point in the history
refactor: split ProcessDomain message
  • Loading branch information
maelgangloff authored Aug 26, 2024
2 parents 142be13 + b5b958d commit 63deca2
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 180 deletions.
8 changes: 5 additions & 3 deletions config/packages/messenger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ framework:
Symfony\Component\Mailer\Messenger\SendEmailMessage: async
Symfony\Component\Notifier\Message\ChatMessage: async
Symfony\Component\Notifier\Message\SmsMessage: async
App\Message\UpdateRdapServers: async

App\Message\OrderDomain: async
App\Message\ProcessWatchListsTrigger: async
App\Message\ProcessWatchListTrigger: async
App\Message\ProcessDomainTrigger: async
App\Message\SendDomainEventNotif: async
App\Message\UpdateDomainsFromWatchlist: async
App\Message\UpdateRdapServers: async

# Route your messages to the transports
# 'App\Message\YourMessage': async
4 changes: 2 additions & 2 deletions src/Controller/DomainRefreshController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Entity\Domain;
use App\Entity\WatchList;
use App\Message\ProcessDomainTrigger;
use App\Message\SendDomainEventNotif;
use App\Repository\DomainRepository;
use App\Service\RDAPService;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -80,7 +80,7 @@ public function __invoke(string $ldhName, KernelInterface $kernel): ?Domain

/** @var WatchList $watchList */
foreach ($watchLists as $watchList) {
$this->bus->dispatch(new ProcessDomainTrigger($watchList->getToken(), $domain->getLdhName(), $updatedAt));
$this->bus->dispatch(new SendDomainEventNotif($watchList->getToken(), $domain->getLdhName(), $updatedAt));
}

return $domain;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Message;

final class ProcessDomainTrigger
final class OrderDomain
{
public function __construct(
public string $watchListToken,
Expand Down
13 changes: 13 additions & 0 deletions src/Message/SendDomainEventNotif.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Message;

final class SendDomainEventNotif
{
public function __construct(
public string $watchListToken,
public string $ldhName,
public \DateTimeImmutable $updatedAt
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Message;

final readonly class ProcessWatchListTrigger
final readonly class UpdateDomainsFromWatchlist
{
public function __construct(
public string $watchListToken,
Expand Down
99 changes: 99 additions & 0 deletions src/MessageHandler/OrderDomainHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace App\MessageHandler;

use App\Config\Provider\AbstractProvider;
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;
use App\Service\ChatNotificationService;
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,
private ChatNotificationService $chatNotificationService
) {
$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) {
throw new \Exception('Provider not found');
}

$connectorProviderClass = $provider->getConnectorProvider();

/** @var AbstractProvider $connectorProvider */
$connectorProvider = new $connectorProviderClass($connector->getAuthData(), $this->client, $this->cacheItemPool, $this->kernel);

$connectorProvider->orderDomain($domain, $this->kernel->isDebug());

$notification = (new DomainOrderNotification($this->sender, $domain, $connector));
$this->mailer->send($notification->asEmailMessage(new Recipient($watchList->getUser()->getEmail()))->getMessage());
$this->chatNotificationService->sendChatNotification($watchList, $notification);

$this->statService->incrementStat('stats.domain.purchased');
} 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());
$this->chatNotificationService->sendChatNotification($watchList, $notification);

$this->statService->incrementStat('stats.domain.purchase.failed');

throw $exception;
}
}
}
}
166 changes: 0 additions & 166 deletions src/MessageHandler/ProcessDomainTriggerHandler.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/MessageHandler/ProcessWatchListsTriggerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Entity\WatchList;
use App\Message\ProcessWatchListsTrigger;
use App\Message\ProcessWatchListTrigger;
use App\Message\UpdateDomainsFromWatchlist;
use App\Repository\WatchListRepository;
use Random\Randomizer;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
Expand All @@ -30,7 +30,7 @@ public function __invoke(ProcessWatchListsTrigger $message): void

/** @var WatchList $watchList */
foreach ($watchLists as $watchList) {
$this->bus->dispatch(new ProcessWatchListTrigger($watchList->getToken()));
$this->bus->dispatch(new UpdateDomainsFromWatchlist($watchList->getToken()));
}
}
}
Loading

0 comments on commit 63deca2

Please sign in to comment.