Easy_solution/src/Service/SSO/ProjectService.php

87 lines
3.3 KiB
PHP

<?php
namespace App\Service\SSO;
use App\Entity\Project;
use Doctrine\ORM\EntityManagerInterface;
use League\Bundle\OAuth2ServerBundle\Model\Client;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class ProjectService
{
public function __construct(private readonly HttpClientInterface $httpClient,
private string $appUrl,
private string $clientIdentifier, private readonly EntityManagerInterface $entityManager)
{
}
// Inside your SSO Server Service
public function createRemoteProject(string $clientAppUrl, Project $project): void
{
// 1. Get a token for "ourselves" -> on en a besoin parce que c'est du M2M.
$tokenResponse = $this->getTokenResponse();
$accessToken = $tokenResponse->toArray()['access_token'];
// data must match easy check database
$projectJson = $this->getProjectToJson($project);
// 2. Call the Client Application's Webhook/API
$this->httpClient->request('POST', $clientAppUrl . '/api/v1/project/create', [
'headers' => ['Authorization' => 'Bearer ' . $accessToken],
'json' => $projectJson
]);
}
public function editRemoteProject(string $clientAppUrl, Project $project): void
{
$tokenResponse = $this->getTokenResponse();
$accessToken = $tokenResponse->toArray()['access_token'];
// data must match easy check database
$projectJson = $this->getProjectToJson($project);
// 2. Call the Client Application's Webhook/API
$this->httpClient->request('PUT', $clientAppUrl . '/api/v1/project/edit/'. $project->getId(), [
'headers' => ['Authorization' => 'Bearer ' . $accessToken],
'json' => $projectJson
]);
}
public function deleteRemoteProject(string $clientAppUrl, int $projectId): void
{
$tokenResponse = $this->getTokenResponse();
$accessToken = $tokenResponse->toArray()['access_token'];
// 2. Call the Client Application's Webhook/API
$this->httpClient->request('DELETE', $clientAppUrl . '/api/v1/project/delete/'. $projectId, [
'headers' => ['Authorization' => 'Bearer ' . $accessToken],
]);
}
public function getTokenResponse(): ResponseInterface{
$portalClient = $this->entityManager->getRepository(Client::class)->findOneBy(['identifier' => $this->clientIdentifier]);
return $this->httpClient->request('POST', $this->appUrl . 'token', [
'auth_basic' => [$portalClient->getIdentifier(),$portalClient->getSecret()], // ID and Secret go here
'body' => [
'grant_type' => 'client_credentials',
],
]);
}
public function getProjectToJson(Project $project): array {
return [
'id' => $project->getId(),
'projet' => $project->getName(),
'entity_id' => $project->getOrganization()->getId(),
'bdd' => $project->getBddName(),
'isactive' => $project->isActive(),
'logo' => $project->getLogo(),
'timestamp'=> $project->getTimestampPrecision(),
'deletion' => $project->isDeletionAllowed()
];
}
}