diff --git a/migrations/Version20240730193422.php b/migrations/Version20240730193422.php new file mode 100644 index 0000000..8d6c367 --- /dev/null +++ b/migrations/Version20240730193422.php @@ -0,0 +1,44 @@ +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'); + } +} diff --git a/src/Config/TriggerAction.php b/src/Config/TriggerAction.php index 229fcc6..c2b29fe 100644 --- a/src/Config/TriggerAction.php +++ b/src/Config/TriggerAction.php @@ -6,5 +6,4 @@ namespace App\Config; enum TriggerAction: string { case SendEmail = 'email'; - case BuyDomain = 'buy'; } diff --git a/src/Controller/WatchListController.php b/src/Controller/WatchListController.php index 39c9e29..f10d0e9 100644 --- a/src/Controller/WatchListController.php +++ b/src/Controller/WatchListController.php @@ -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(); diff --git a/src/Entity/Connector.php b/src/Entity/Connector.php index 0d9fbb6..071ec40 100644 --- a/src/Entity/Connector.php +++ b/src/Entity/Connector.php @@ -56,16 +56,16 @@ class Connector private array $authData = []; /** - * @var Collection + * @var Collection */ - #[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 - */ - 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 + */ + 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; + } + } diff --git a/src/Entity/WatchList.php b/src/Entity/WatchList.php index d5be416..9528340 100644 --- a/src/Entity/WatchList.php +++ b/src/Entity/WatchList.php @@ -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; + } } diff --git a/src/Entity/WatchListTrigger.php b/src/Entity/WatchListTrigger.php index a038a3a..477669d 100644 --- a/src/Entity/WatchListTrigger.php +++ b/src/Entity/WatchListTrigger.php @@ -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; - } } diff --git a/src/MessageHandler/ProcessDomainTriggerHandler.php b/src/MessageHandler/ProcessDomainTriggerHandler.php index 34e257a..f606d58 100644 --- a/src/MessageHandler/ProcessDomainTriggerHandler.php +++ b/src/MessageHandler/ProcessDomainTriggerHandler.php @@ -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()); }