Réorganisation des controllers et services et ajout GuildController
This commit is contained in:
@@ -2,12 +2,18 @@
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use DateTime;
|
||||
use DateInterval;
|
||||
use App\Entity\User;
|
||||
use App\Exception\DiscordApiException;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
|
||||
class DiscordApiService
|
||||
{
|
||||
@@ -64,6 +70,28 @@ class DiscordApiService
|
||||
}
|
||||
}
|
||||
|
||||
public function getUserToken(SessionInterface $session, EntityManagerInterface $entityManager): ?User
|
||||
{
|
||||
// Vérifier si une session est en cours
|
||||
if (!$session->has('user_id')) throw new DiscordApiException(Response::HTTP_UNAUTHORIZED, 'Non authentifié');
|
||||
|
||||
$user = $entityManager->getRepository(User::class)->find($session->get('user_id'));
|
||||
if (!$user) throw new DiscordApiException(Response::HTTP_NOT_FOUND, 'Utilisateur non trouvé');
|
||||
|
||||
// Vérifier si l'utilisateur a un token d'accès et qu'il n'est pas expiré
|
||||
if (!$user->getAccessToken() || $user->getTokenExpiresAt() < new DateTime()) {
|
||||
if ($user->getRefreshToken()) {
|
||||
// Rafraîchir le token d'accès
|
||||
$tokenData = $this->refreshToken($user->getRefreshToken());
|
||||
|
||||
$user->setAccessToken($tokenData['access_token']);
|
||||
$user->setRefreshToken($tokenData['refresh_token']);
|
||||
$user->setTokenExpiresAt((new DateTime())->add(new DateInterval('PT' . $tokenData['expires_in'] . 'S')));
|
||||
} throw new DiscordApiException(Response::HTTP_UNAUTHORIZED, "Token d'accès expiré et/ou token de refresh invalide");
|
||||
}
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function getBotsData(): array
|
||||
{
|
||||
$output = array();
|
||||
@@ -98,7 +126,7 @@ class DiscordApiService
|
||||
*/
|
||||
}
|
||||
|
||||
public function getUserData(string $accessToken): array
|
||||
public function getCurrentUserData(string $accessToken): array
|
||||
{
|
||||
try {
|
||||
$response = $this->client->request('GET', $this->baseUrl . '/users/@me', [
|
||||
@@ -112,7 +140,7 @@ class DiscordApiService
|
||||
}
|
||||
}
|
||||
|
||||
public function getUserGuilds(string $accessToken): array
|
||||
public function getCurrentUserGuildsData(string $accessToken): array
|
||||
{
|
||||
try {
|
||||
$response = $this->client->request('GET', $this->baseUrl . '/users/@me/guilds', [
|
||||
@@ -125,4 +153,32 @@ class DiscordApiService
|
||||
throw new DiscordApiException($e->getCode(), 'Erreur lors de la requête à l\'API Discord: ' . $e->getMessage(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function getUserData(string $userId): array
|
||||
{
|
||||
try {
|
||||
$response = $this->client->request('GET', $this->baseUrl . '/users/' . $userId, [
|
||||
'headers' => [
|
||||
'Authorization' => 'Bot ' . $this->clientToken,
|
||||
],
|
||||
]);
|
||||
return $response->toArray();
|
||||
} catch (ClientExceptionInterface | ServerExceptionInterface | RedirectionExceptionInterface | TransportExceptionInterface $e) {
|
||||
throw new DiscordApiException($e->getCode(), 'Erreur lors de la requête à l\'API Discord: ' . $e->getMessage(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function getGuildData(string $guildId, string $accessToken): array
|
||||
{
|
||||
try {
|
||||
$response = $this->client->request('GET', $this->baseUrl . '/guilds/' . $guildId, [
|
||||
'headers' => [
|
||||
'Authorization' => 'Bearer ' . $accessToken,
|
||||
],
|
||||
]);
|
||||
return $response->toArray();
|
||||
} catch (ClientExceptionInterface | ServerExceptionInterface | RedirectionExceptionInterface | TransportExceptionInterface $e) {
|
||||
throw new DiscordApiException($e->getCode(), 'Erreur lors de la requête à l\'API Discord: ' . $e->getMessage(), $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user