feat: connector on watchlist

This commit is contained in:
Maël Gangloff
2024-07-30 21:34:48 +02:00
parent 73a47c7879
commit 6f89317edc
7 changed files with 114 additions and 82 deletions

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240730193422 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE watch_list ADD connector_id UUID DEFAULT NULL');
$this->addSql('COMMENT ON COLUMN watch_list.connector_id IS \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE watch_list ADD CONSTRAINT FK_152B584B4D085745 FOREIGN KEY (connector_id) REFERENCES connector (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX IDX_152B584B4D085745 ON watch_list (connector_id)');
$this->addSql('ALTER TABLE watch_list_trigger DROP CONSTRAINT fk_cf857a4c4d085745');
$this->addSql('DROP INDEX idx_cf857a4c4d085745');
$this->addSql('ALTER TABLE watch_list_trigger DROP connector_id');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE watch_list_trigger ADD connector_id UUID DEFAULT NULL');
$this->addSql('COMMENT ON COLUMN watch_list_trigger.connector_id IS \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE watch_list_trigger ADD CONSTRAINT fk_cf857a4c4d085745 FOREIGN KEY (connector_id) REFERENCES connector (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX idx_cf857a4c4d085745 ON watch_list_trigger (connector_id)');
$this->addSql('ALTER TABLE watch_list DROP CONSTRAINT FK_152B584B4D085745');
$this->addSql('DROP INDEX IDX_152B584B4D085745');
$this->addSql('ALTER TABLE watch_list DROP connector_id');
}
}

View File

@@ -6,5 +6,4 @@ namespace App\Config;
enum TriggerAction: string
{
case SendEmail = 'email';
case BuyDomain = 'buy';
}

View File

@@ -2,10 +2,8 @@
namespace App\Controller;
use App\Config\TriggerAction;
use App\Entity\User;
use App\Entity\WatchList;
use App\Entity\WatchListTrigger;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
@@ -55,13 +53,6 @@ class WatchListController extends AbstractController
{
$watchList = $this->serializer->deserialize($request->getContent(), WatchList::class, 'json', ['groups' => 'watchlist:create']);
$watchList->setUser($this->getUser());
/** @var WatchListTrigger $trigger */
foreach ($watchList->getWatchListTriggers()->toArray() as $trigger) {
if ($trigger->getAction() === TriggerAction::SendEmail && $trigger->getConnector() !== null)
throw new Exception('No connector needed to send email');
if ($trigger->getAction() === TriggerAction::BuyDomain && $trigger->getConnector() === null)
throw new Exception('Unable to order a domain name without a Connector');
}
$this->em->persist($watchList);
$this->em->flush();

View File

@@ -56,16 +56,16 @@ class Connector
private array $authData = [];
/**
* @var Collection<int, WatchListTrigger>
* @var Collection<int, WatchList>
*/
#[ORM\OneToMany(targetEntity: WatchListTrigger::class, mappedBy: 'connector')]
private Collection $watchListTriggers;
#[ORM\OneToMany(targetEntity: WatchList::class, mappedBy: 'connector')]
private Collection $watchLists;
public function __construct()
{
$this->id = Uuid::v4();
$this->watchListTriggers = new ArrayCollection();
$this->watchLists = new ArrayCollection();
}
public function getId(): ?string
@@ -97,36 +97,6 @@ class Connector
return $this;
}
/**
* @return Collection<int, WatchListTrigger>
*/
public function getWatchListTriggers(): Collection
{
return $this->watchListTriggers;
}
public function addWatchListTrigger(WatchListTrigger $watchListTrigger): static
{
if (!$this->watchListTriggers->contains($watchListTrigger)) {
$this->watchListTriggers->add($watchListTrigger);
$watchListTrigger->setConnector($this);
}
return $this;
}
public function removeWatchListTrigger(WatchListTrigger $watchListTrigger): static
{
if ($this->watchListTriggers->removeElement($watchListTrigger)) {
// set the owning side to null (unless already changed)
if ($watchListTrigger->getConnector() === $this) {
$watchListTrigger->setConnector(null);
}
}
return $this;
}
public function getProvider(): ?ConnectorProvider
{
return $this->provider;
@@ -139,4 +109,34 @@ class Connector
return $this;
}
/**
* @return Collection<int, WatchList>
*/
public function getWatchLists(): Collection
{
return $this->watchLists;
}
public function addWatchList(WatchList $watchList): static
{
if (!$this->watchLists->contains($watchList)) {
$this->watchLists->add($watchList);
$watchList->setConnector($this);
}
return $this;
}
public function removeWatchList(WatchList $watchList): static
{
if ($this->watchLists->removeElement($watchList)) {
// set the owning side to null (unless already changed)
if ($watchList->getConnector() === $this) {
$watchList->setConnector(null);
}
}
return $this;
}
}

View File

@@ -69,6 +69,11 @@ class WatchList
#[SerializedName("triggers")]
private Collection $watchListTriggers;
#[ORM\ManyToOne(inversedBy: 'watchLists')]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create', 'watchlist:update'])]
private ?Connector $connector = null;
public function __construct()
{
$this->token = Uuid::v4();
@@ -146,4 +151,16 @@ class WatchList
return $this;
}
public function getConnector(): ?Connector
{
return $this->connector;
}
public function setConnector(?Connector $connector): static
{
$this->connector = $connector;
return $this;
}
}

View File

@@ -25,10 +25,6 @@ class WatchListTrigger
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create', 'watchlist:update'])]
private ?TriggerAction $action = null;
#[ORM\ManyToOne(inversedBy: 'watchListTriggers')]
#[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create', 'watchlist:update'])]
private ?Connector $connector = null;
public function getEvent(): ?string
{
return $this->event;
@@ -64,16 +60,4 @@ class WatchListTrigger
return $this;
}
public function getConnector(): ?Connector
{
return $this->connector;
}
public function setConnector(?Connector $connector): static
{
$this->connector = $connector;
return $this;
}
}

View File

@@ -48,31 +48,28 @@ final readonly class ProcessDomainTriggerHandler
/** @var Domain $domain */
$domain = $this->domainRepository->findOneBy(["ldhName" => $message->ldhName]);
$watchListTriggers = $watchList->getWatchListTriggers();
$connector = $watchList->getConnector();
if (null !== $connector && $domain->getDeleted()) {
try {
if ($connector->getProvider() === ConnectorProvider::OVH) {
$ovh = new OVHConnector($connector->getAuthData());
$isDebug = $this->kernel->isDebug();
/** @var WatchListTrigger $watchListTrigger */
foreach ($watchListTriggers->getIterator() as $watchListTrigger) {
if ($watchListTrigger->getAction() === TriggerAction::BuyDomain) {
try {
if ($watchListTrigger->getConnector() === null) throw new Exception('Connector is missing');
$connector = $watchListTrigger->getConnector();
if ($connector->getProvider() === ConnectorProvider::OVH) {
$ovh = new OVHConnector($connector->getAuthData());
$isDebug = $this->kernel->isDebug();
$ovh->orderDomain($domain, $isDebug);
$this->sendEmailDomainOrdered($domain, $connector, $watchList->getUser());
} else throw new Exception("Unknown provider");
} catch (Throwable) {
$this->sendEmailDomainOrderError($domain, $watchList->getUser());
}
$ovh->orderDomain($domain, $isDebug);
$this->sendEmailDomainOrdered($domain, $connector, $watchList->getUser());
} else throw new Exception("Unknown provider");
} catch (Throwable) {
$this->sendEmailDomainOrderError($domain, $watchList->getUser());
}
}
/** @var DomainEvent $event */
foreach ($domain->getEvents()->filter(fn($event) => $message->updatedAt < $event->getDate()) as $event) {
/** @var DomainEvent $event */
foreach ($domain->getEvents()->filter(fn($event) => $message->updatedAt < $event->getDate()) as $event) {
$watchListTriggers = $watchList->getWatchListTriggers()
->filter(fn($trigger) => $trigger->getEvent() === $event->getAction());
/** @var WatchListTrigger $watchListTrigger */
foreach ($watchListTriggers->getIterator() as $watchListTrigger) {
if ($watchListTrigger->getAction() == TriggerAction::SendEmail) {
$this->sendEmailDomainUpdated($event, $watchList->getUser());
}