domain-watchdog/src/Controller/ConnectorController.php

91 lines
2.8 KiB
PHP
Raw Normal View History

2024-07-29 15:28:05 +02:00
<?php
namespace App\Controller;
use App\Config\Connector\OvhConnector;
use App\Config\ConnectorProvider;
use App\Entity\Connector;
use App\Entity\User;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
2024-08-04 14:45:27 +02:00
use Psr\Log\LoggerInterface;
2024-07-29 15:28:05 +02:00
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;
class ConnectorController extends AbstractController
{
public function __construct(
2024-08-04 14:45:27 +02:00
private readonly SerializerInterface $serializer,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger
2024-08-02 23:24:52 +02:00
) {
2024-07-29 15:28:05 +02:00
}
#[Route(
path: '/api/connectors',
name: 'connector_get_all_mine',
defaults: [
'_api_resource_class' => Connector::class,
'_api_operation_name' => 'get_all_mine',
],
methods: ['GET']
)]
public function getConnector(): Collection
{
/** @var User $user */
$user = $this->getUser();
2024-08-02 23:24:52 +02:00
2024-07-29 15:28:05 +02:00
return $user->getConnectors();
}
/**
2024-08-02 23:24:52 +02:00
* @throws \Exception
2024-07-29 15:28:05 +02:00
*/
#[Route(
path: '/api/connectors',
name: 'connector_create',
defaults: [
'_api_resource_class' => Connector::class,
'_api_operation_name' => 'create',
],
methods: ['POST']
)]
public function createConnector(Request $request): Connector
{
$connector = $this->serializer->deserialize($request->getContent(), Connector::class, 'json', ['groups' => 'connector:create']);
2024-08-03 00:06:38 +02:00
/** @var User $user */
$user = $this->getUser();
$connector->setUser($user);
2024-07-29 15:28:05 +02:00
2024-08-04 14:45:27 +02:00
$provider = $connector->getProvider();
$this->logger->info('User {username} wants to register a connector from provider {provider}.', [
'username' => $user->getUserIdentifier(),
'provider' => $provider->value,
]);
if (ConnectorProvider::OVH === $provider) {
2024-07-30 00:55:36 +02:00
$authData = OvhConnector::verifyAuthData($connector->getAuthData());
$connector->setAuthData($authData);
2024-08-04 14:45:27 +02:00
$this->logger->info('User {username} authentication data with the OVH provider has been validated.', [
'username' => $user->getUserIdentifier(),
]);
2024-08-02 23:24:52 +02:00
} else {
throw new \Exception('Unknown provider');
}
2024-07-29 15:28:05 +02:00
2024-08-04 14:45:27 +02:00
$this->logger->info('The new API connector requested by {username} has been successfully registered.', [
'username' => $user->getUserIdentifier(),
]);
2024-08-06 00:35:05 +02:00
$connector->setCreatedAt(new \DateTimeImmutable('now'));
2024-07-29 15:28:05 +02:00
$this->em->persist($connector);
$this->em->flush();
return $connector;
}
2024-08-02 23:24:52 +02:00
}