feat: start ovh API implement

This commit is contained in:
Maël Gangloff
2024-07-29 03:27:55 +02:00
parent d8eae925f1
commit f73aabea88
5 changed files with 106 additions and 61 deletions

View File

@@ -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;
}

View File

@@ -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<int, WatchListTrigger>
*/
#[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<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;
}
}

View File

@@ -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
]);
*/
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Repository;
use App\Entity\OVHConnector;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method OVHConnector|null find($id, $lockMode = null, $lockVersion = null)
* @method OVHConnector|null findOneBy(array $criteria, array $orderBy = null)
* @method OVHConnector[] findAll()
* @method OVHConnector[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class OVHConnectorRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, OVHConnector::class);
}
}