From f73aabea88958c8334b4ed76abc4f828d5635ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gangloff?= Date: Mon, 29 Jul 2024 03:27:55 +0200 Subject: [PATCH] feat: start ovh API implement --- src/Config/ConnectorInterface.php | 4 +- src/Entity/Connector.php | 43 ------------ src/Entity/OVHConnector.php | 83 ++++++++++++++++++++++- src/Entity/WatchListTrigger.php | 16 ----- src/Repository/OVHConnectorRepository.php | 21 ++++++ 5 files changed, 106 insertions(+), 61 deletions(-) create mode 100644 src/Repository/OVHConnectorRepository.php diff --git a/src/Config/ConnectorInterface.php b/src/Config/ConnectorInterface.php index 3458e2b..47acd03 100644 --- a/src/Config/ConnectorInterface.php +++ b/src/Config/ConnectorInterface.php @@ -2,7 +2,9 @@ namespace App\Config; +use App\Entity\Domain; + interface ConnectorInterface { - + public function orderDomain(Domain $domain, bool $acceptConditions, bool $ownerLegalAge, bool $waiveRetractationPeriod): void; } \ No newline at end of file diff --git a/src/Entity/Connector.php b/src/Entity/Connector.php index aee9571..dedfd5d 100644 --- a/src/Entity/Connector.php +++ b/src/Entity/Connector.php @@ -4,8 +4,6 @@ namespace App\Entity; use App\Config\ConnectorProvider; use App\Repository\ConnectorRepository; -use Doctrine\Common\Collections\ArrayCollection; -use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping\DiscriminatorColumn; use Doctrine\ORM\Mapping\DiscriminatorMap; @@ -31,17 +29,6 @@ class Connector #[ORM\Column] private array $authData = []; - /** - * @var Collection - */ - #[ORM\OneToMany(targetEntity: WatchListTrigger::class, mappedBy: 'connector')] - private Collection $watchListTriggers; - - public function __construct() - { - $this->watchListTriggers = new ArrayCollection(); - } - public function getId(): ?int { return $this->id; @@ -83,34 +70,4 @@ 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; - } - } diff --git a/src/Entity/OVHConnector.php b/src/Entity/OVHConnector.php index f63eee6..b369962 100644 --- a/src/Entity/OVHConnector.php +++ b/src/Entity/OVHConnector.php @@ -3,10 +3,91 @@ namespace App\Entity; use App\Config\ConnectorInterface; +use App\Repository\OVHConnectorRepository; use Doctrine\ORM\Mapping\Entity; +use Exception; +use Ovh\Api; -#[Entity] +#[Entity(repositoryClass: OVHConnectorRepository::class)] class OVHConnector extends Connector implements ConnectorInterface { + /** + * @throws Exception + */ + public function orderDomain(Domain $domain, bool $acceptConditions, bool $ownerLegalAge, bool $waiveRetractationPeriod): void + { + $ldhName = $domain->getLdhName(); + if (!$ldhName) throw new Exception("Domain name cannot be null."); + + $authData = $this->getAuthData(); + + $appKey = $authData['appKey']; + $appSecret = $authData['appSecret']; + $apiEndpoint = $authData['apiEndpoint']; + $consumerKey = $authData['consumerKey']; + $ovhSubsidiary = $authData['ovhSubsidiary']; + $pricingMode = $authData['pricingMode']; + + if (!$appKey || + !$appSecret || + !$apiEndpoint || + !$consumerKey || + !$ovhSubsidiary || + !$pricingMode + ) throw new Exception("Auth data cannot be null."); + + $conn = new Api( + $appKey, + $appSecret, + $apiEndpoint, + $consumerKey + ); + + $cart = $conn->post('/order/cart', [ + "ovhSubsidiary" => $ovhSubsidiary, + "description" => "Domain Watchdog" + ]); + $cartId = $cart['cartId']; + + $offers = $conn->get("/order/cart/{$cartId}/domain", [ + "domain" => $ldhName + ]); + $offer = array_filter($offers, fn($offer) => $offer['action'] === 'create' && + $offer['orderable'] === true && + $offers['pricingMode'] === $pricingMode + ); + if (empty($offer)) throw new Exception('Cannot buy this domain name.'); + + $item = $conn->post("/order/cart/{$cartId}/domain", [ + "domain" => $ldhName, + "duration" => "P1Y" + ]); + $itemId = $item['itemId']; + + //$conn->get("/order/cart/{$cartId}/summary"); + $conn->post("/order/cart/{$cartId}/assign"); + $conn->get("/order/cart/{$cartId}/item/{$itemId}/requiredConfiguration"); + + $configuration = [ + "ACCEPT_CONDITIONS" => $acceptConditions, + "OWNER_LEGAL_AGE" => $ownerLegalAge + ]; + + foreach ($configuration as $label => $value) { + $conn->post("/order/cart/{$cartId}/item/{$itemId}/configuration", [ + "cartId" => $cartId, + "itemId" => $itemId, + "label" => $label, + "value" => $value + ]); + } + $conn->get("/order/cart/{$cartId}/checkout"); + /* + $conn->post("/order/cart/{$cartId}/checkout", [ + "autoPayWithPreferredPaymentMethod" => true, + "waiveRetractationPeriod" => $waiveRetractationPeriod + ]); + */ + } } \ No newline at end of file diff --git a/src/Entity/WatchListTrigger.php b/src/Entity/WatchListTrigger.php index d85f247..477669d 100644 --- a/src/Entity/WatchListTrigger.php +++ b/src/Entity/WatchListTrigger.php @@ -2,7 +2,6 @@ namespace App\Entity; -use App\Config\EventAction; use App\Config\TriggerAction; use App\Repository\EventTriggerRepository; use Doctrine\ORM\Mapping as ORM; @@ -26,9 +25,6 @@ class WatchListTrigger #[Groups(['watchlist:list', 'watchlist:item', 'watchlist:create', 'watchlist:update'])] private ?TriggerAction $action = null; - #[ORM\ManyToOne(inversedBy: 'watchListTriggers')] - 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/Repository/OVHConnectorRepository.php b/src/Repository/OVHConnectorRepository.php new file mode 100644 index 0000000..a3aa611 --- /dev/null +++ b/src/Repository/OVHConnectorRepository.php @@ -0,0 +1,21 @@ +