2024-07-29 15:28:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
|
|
use App\Entity\Connector;
|
|
|
|
|
use App\Entity\User;
|
2024-09-18 13:37:07 +02:00
|
|
|
use App\Service\Connector\AbstractProvider;
|
2024-07-29 15:28:05 +02:00
|
|
|
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;
|
2024-09-30 13:48:15 +02:00
|
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2024-07-29 15:28:05 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2024-08-30 12:54:42 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2024-07-29 15:28:05 +02:00
|
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
2024-08-06 03:38:00 +02:00
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
2024-07-29 15:28:05 +02:00
|
|
|
|
|
|
|
|
class ConnectorController extends AbstractController
|
|
|
|
|
{
|
|
|
|
|
public function __construct(
|
2024-08-04 14:45:27 +02:00
|
|
|
private readonly SerializerInterface $serializer,
|
|
|
|
|
private readonly EntityManagerInterface $em,
|
2024-09-30 13:48:15 +02:00
|
|
|
private readonly LoggerInterface $logger,
|
|
|
|
|
#[Autowire(service: 'service_container')]
|
2024-11-01 00:46:25 +01:00
|
|
|
private ContainerInterface $locator,
|
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']
|
|
|
|
|
)]
|
2024-08-06 03:38:00 +02:00
|
|
|
public function createConnector(Request $request, HttpClientInterface $client): Connector
|
2024-07-29 15:28:05 +02:00
|
|
|
{
|
|
|
|
|
$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,
|
|
|
|
|
]);
|
|
|
|
|
|
2024-08-07 01:10:56 +02:00
|
|
|
if (null === $provider) {
|
2024-08-30 12:54:42 +02:00
|
|
|
throw new BadRequestHttpException('Provider not found');
|
2024-08-02 23:24:52 +02:00
|
|
|
}
|
2024-07-29 15:28:05 +02:00
|
|
|
|
2024-09-30 13:48:15 +02:00
|
|
|
/** @var AbstractProvider $providerClient */
|
|
|
|
|
$providerClient = $this->locator->get($provider->getConnectorProvider());
|
|
|
|
|
$authData = $providerClient->verifyAuthData($connector->getAuthData());
|
2024-08-06 03:38:00 +02:00
|
|
|
$connector->setAuthData($authData);
|
2024-10-02 21:14:34 +02:00
|
|
|
$providerClient->authenticate($authData);
|
2024-08-06 03:38:00 +02:00
|
|
|
|
|
|
|
|
$this->logger->info('User {username} authentication data with the {provider} provider has been validated.', [
|
|
|
|
|
'username' => $user->getUserIdentifier(),
|
|
|
|
|
'provider' => $provider->value,
|
|
|
|
|
]);
|
|
|
|
|
|
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
|
|
|
}
|