feat: ovh API implement

This commit is contained in:
Maël Gangloff
2024-07-29 15:28:05 +02:00
parent ffe063413f
commit c89e9a78b3
10 changed files with 207 additions and 111 deletions

View File

@@ -2,32 +2,61 @@
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
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;
use Doctrine\ORM\Mapping\InheritanceType;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Uid\Uuid;
#[ApiResource(
shortName: 'Connector',
operations: [
new GetCollection(
routeName: 'connector_get_all_mine',
normalizationContext: ['groups' => 'connector:list'],
name: 'get_all_mine',
),
new Get(
normalizationContext: ['groups' => 'connector:list']
),
new Post(
routeName: 'connector_create', normalizationContext: ['groups' => 'connector:create'],
denormalizationContext: ['groups' => 'connector:create'],
name: 'create'
),
new Patch(
normalizationContext: ['groups' => 'connector:list'],
denormalizationContext: ['groups' => 'connector:create']
),
new Delete()
]
)]
#[ORM\Entity(repositoryClass: ConnectorRepository::class)]
#[InheritanceType('SINGLE_TABLE')]
#[DiscriminatorColumn(name: 'provider', enumType: ConnectorProvider::class)]
#[DiscriminatorMap([ConnectorProvider::OVH->value => OVHConnector::class])]
class Connector
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
private ?string $provider = null;
#[ORM\Column(type: 'uuid')]
#[Groups(['connector:list'])]
private ?string $id;
#[ORM\ManyToOne(inversedBy: 'connectors')]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
#[Groups(['connector:list', 'connector:create'])]
#[ORM\Column(enumType: ConnectorProvider::class)]
private ?ConnectorProvider $provider = null;
#[Groups(['connector:create'])]
#[ORM\Column]
private array $authData = [];
@@ -37,28 +66,18 @@ class Connector
#[ORM\OneToMany(targetEntity: WatchListTrigger::class, mappedBy: 'connector')]
private Collection $watchListTriggers;
public function __construct()
{
$this->id = Uuid::v4();
$this->watchListTriggers = new ArrayCollection();
}
public function getId(): ?int
public function getId(): ?string
{
return $this->id;
}
public function getProvider(): ?string
{
return $this->provider;
}
public function setProvider(?string $provider): static
{
$this->provider = $provider;
return $this;
}
public function getUser(): ?User
{
return $this->user;
@@ -113,4 +132,16 @@ class Connector
return $this;
}
public function getProvider(): ?ConnectorProvider
{
return $this->provider;
}
public function setProvider(ConnectorProvider $provider): static
{
$this->provider = $provider;
return $this;
}
}

View File

@@ -1,101 +0,0 @@
<?php
namespace App\Entity;
use App\Config\ConnectorInterface;
use App\Repository\OVHConnectorRepository;
use Doctrine\ORM\Mapping\Entity;
use Exception;
use Ovh\Api;
#[Entity(repositoryClass: OVHConnectorRepository::class)]
class OVHConnector extends Connector implements ConnectorInterface
{
/**
* Order a domain name with the OVH API
* @throws Exception
*/
public function orderDomain(Domain $domain,
bool $acceptConditions,
bool $ownerLegalAge,
bool $waiveRetractationPeriod,
bool $dryRyn = false
): void
{
if (!$domain->getDeleted()) throw new Exception('The domain name still appears in the WHOIS database.');
$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");
if ($dryRyn) return;
$conn->post("/order/cart/{$cartId}/checkout", [
"autoPayWithPreferredPaymentMethod" => true,
"waiveRetractationPeriod" => $waiveRetractationPeriod
]);
}
}

View File

@@ -61,7 +61,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
/**
* @var Collection<int, Connector>
*/
#[ORM\OneToMany(targetEntity: Connector::class, mappedBy: 'userr', orphanRemoval: true)]
#[ORM\OneToMany(targetEntity: Connector::class, mappedBy: 'user', orphanRemoval: true)]
private Collection $connectors;
public function __construct()
@@ -188,7 +188,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
{
if (!$this->connectors->contains($connector)) {
$this->connectors->add($connector);
$connector->setUserr($this);
$connector->setUser($this);
}
return $this;
@@ -198,8 +198,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
{
if ($this->connectors->removeElement($connector)) {
// set the owning side to null (unless already changed)
if ($connector->getUserr() === $this) {
$connector->setUserr(null);
if ($connector->getUser() === $this) {
$connector->setUser(null);
}
}