|
|
@@ -6,9 +6,12 @@ namespace App\Service\HelloAsso;
|
|
|
|
|
|
use App\ApiResources\HelloAsso\AuthUrl;
|
|
|
use App\ApiResources\HelloAsso\HelloAssoProfile;
|
|
|
-use App\ApiResources\HelloAsso\OrganizationForm;
|
|
|
+use App\ApiResources\HelloAsso\EventForm;
|
|
|
+use App\Entity\Booking\Event;
|
|
|
use App\Entity\HelloAsso\HelloAsso;
|
|
|
use App\Entity\Organization\Organization;
|
|
|
+use App\Repository\Booking\EventRepository;
|
|
|
+use App\Repository\Organization\OrganizationRepository;
|
|
|
use App\Service\Rest\ApiRequestService;
|
|
|
use App\Service\Security\OAuthPkceGenerator;
|
|
|
use App\Service\Utils\DatesUtils;
|
|
|
@@ -17,7 +20,9 @@ use Doctrine\Common\Collections\Collection;
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
+use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
+use Symfony\Contracts\HttpClient\ResponseInterface;
|
|
|
|
|
|
/**
|
|
|
* Service de connexion à HelloAsso.
|
|
|
@@ -29,6 +34,8 @@ class HelloAssoService extends ApiRequestService
|
|
|
{
|
|
|
public function __construct(
|
|
|
HttpClientInterface $client,
|
|
|
+ private readonly OrganizationRepository $organizationRepository,
|
|
|
+ private readonly EventRepository $eventRepository,
|
|
|
private readonly string $baseUrl,
|
|
|
private readonly string $publicAppBaseUrl,
|
|
|
private readonly string $helloAssoApiBaseUrl,
|
|
|
@@ -66,7 +73,7 @@ class HelloAssoService extends ApiRequestService
|
|
|
*/
|
|
|
public function getAuthUrl(int $organizationId): AuthUrl
|
|
|
{
|
|
|
- $organization = $this->entityManager->getRepository(Organization::class)->find($organizationId);
|
|
|
+ $organization = $this->organizationRepository->find($organizationId);
|
|
|
|
|
|
if (!$organization) {
|
|
|
throw new \RuntimeException('Organization not found');
|
|
|
@@ -114,7 +121,7 @@ class HelloAssoService extends ApiRequestService
|
|
|
*/
|
|
|
public function connect(int $organizationId, string $authorizationCode): HelloAsso
|
|
|
{
|
|
|
- $organization = $this->entityManager->getRepository(Organization::class)->find($organizationId);
|
|
|
+ $organization = $this->organizationRepository->find($organizationId);
|
|
|
if (!$organization) {
|
|
|
throw new \RuntimeException('Organization not found');
|
|
|
}
|
|
|
@@ -154,7 +161,7 @@ class HelloAssoService extends ApiRequestService
|
|
|
*/
|
|
|
public function makeHelloAssoProfile(int $organizationId): HelloAssoProfile
|
|
|
{
|
|
|
- $organization = $this->entityManager->getRepository(Organization::class)->find($organizationId);
|
|
|
+ $organization = $this->organizationRepository->find($organizationId);
|
|
|
if (!$organization) {
|
|
|
throw new \RuntimeException('Organization not found');
|
|
|
}
|
|
|
@@ -175,7 +182,7 @@ class HelloAssoService extends ApiRequestService
|
|
|
|
|
|
public function unlinkHelloAssoAccount(int $organizationId): void
|
|
|
{
|
|
|
- $organization = $this->entityManager->getRepository(Organization::class)->find($organizationId);
|
|
|
+ $organization = $this->organizationRepository->find($organizationId);
|
|
|
if (!$organization) {
|
|
|
throw new \RuntimeException('Organization not found');
|
|
|
}
|
|
|
@@ -195,28 +202,20 @@ class HelloAssoService extends ApiRequestService
|
|
|
$this->entityManager->flush();
|
|
|
}
|
|
|
|
|
|
- public function getHelloAssoForms(int $organizationId): array
|
|
|
- {
|
|
|
- $organization = $this->entityManager->getRepository(Organization::class)->find($organizationId);
|
|
|
- if (!$organization) {
|
|
|
- throw new \RuntimeException('Organization not found');
|
|
|
- }
|
|
|
-
|
|
|
- $helloAssoEntity = $organization->getHelloAsso();
|
|
|
- if (!$helloAssoEntity) {
|
|
|
- throw new \RuntimeException('HelloAsso entity not found');
|
|
|
- }
|
|
|
+ public function getResource(HelloAsso $helloAssoEntity, array $routeParts): array {
|
|
|
if (!$helloAssoEntity->getOrganizationSlug() || !$helloAssoEntity->getToken()) {
|
|
|
throw new \RuntimeException('HelloAsso entity incomplete');
|
|
|
}
|
|
|
|
|
|
$helloAssoEntity = $this->refreshTokenIfNeeded($helloAssoEntity);
|
|
|
|
|
|
+ $url = UrlBuilder::concat(
|
|
|
+ $this->helloAssoApiBaseUrl,
|
|
|
+ array_merge(['/v5'], $routeParts),
|
|
|
+ );
|
|
|
+
|
|
|
$response = $this->get(
|
|
|
- UrlBuilder::concat(
|
|
|
- $this->helloAssoApiBaseUrl,
|
|
|
- ['/v5/organizations', $helloAssoEntity->getOrganizationSlug(), '/forms']
|
|
|
- ),
|
|
|
+ $url,
|
|
|
[],
|
|
|
['headers' =>
|
|
|
[
|
|
|
@@ -227,25 +226,98 @@ class HelloAssoService extends ApiRequestService
|
|
|
);
|
|
|
|
|
|
if ($response->getStatusCode() !== 200) {
|
|
|
- throw new HttpException(500, 'Failed to fetch access token: '.$response->getContent(false));
|
|
|
+ throw new HttpException(
|
|
|
+ 500,
|
|
|
+ 'Failed to fetch resource: ['.$response->getStatusCode().'] '.$response->getContent(false)
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
- $data = json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR)['data'];
|
|
|
+ return json_decode($response->getContent(), true, 512, JSON_THROW_ON_ERROR);
|
|
|
+ }
|
|
|
|
|
|
- $forms = [];
|
|
|
+ public function getHelloAssoEventForms(int $organizationId): array
|
|
|
+ {
|
|
|
+ $helloAssoEntity = $this->getHelloAssoEntityFor($organizationId);
|
|
|
+
|
|
|
+ $data = $this->getResource(
|
|
|
+ $helloAssoEntity,
|
|
|
+ ['organizations', $helloAssoEntity->getOrganizationSlug(), 'forms']
|
|
|
+ );
|
|
|
|
|
|
- foreach ($data as $formData) {
|
|
|
- $form = new OrganizationForm();
|
|
|
- $form->setSlug($formData['formSlug']);
|
|
|
- $form->setTitle($formData['title']);
|
|
|
- $form->setUrl($formData['url']);
|
|
|
+ $forms = [];
|
|
|
|
|
|
- $forms[] = $form;
|
|
|
+ foreach ($data['data'] as $formData) {
|
|
|
+ $forms[] = $this->makeHelloAssoEventForm($formData);
|
|
|
}
|
|
|
|
|
|
return $forms;
|
|
|
}
|
|
|
|
|
|
+ public function getHelloAssoEventForm(int $organizationId, string $formSlug): EventForm
|
|
|
+ {
|
|
|
+ $helloAssoEntity = $this->getHelloAssoEntityFor($organizationId);
|
|
|
+
|
|
|
+ if (!$helloAssoEntity->getOrganizationSlug() || !$helloAssoEntity->getToken()) {
|
|
|
+ throw new \RuntimeException('HelloAsso entity incomplete');
|
|
|
+ }
|
|
|
+
|
|
|
+ $formType = 'Event';
|
|
|
+
|
|
|
+ $data = $this->getResource(
|
|
|
+ $helloAssoEntity,
|
|
|
+ ['organizations', $helloAssoEntity->getOrganizationSlug(), 'forms', $formType, $formSlug, 'public']
|
|
|
+ );
|
|
|
+
|
|
|
+ return $this->makeHelloAssoEventForm($data);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getHelloAssoEventFormByEventId(int $organizationId, int $eventId): EventForm
|
|
|
+ {
|
|
|
+ $helloAssoEntity = $this->getHelloAssoEntityFor($organizationId);
|
|
|
+
|
|
|
+ if (!$helloAssoEntity->getOrganizationSlug() || !$helloAssoEntity->getToken()) {
|
|
|
+ throw new \RuntimeException('HelloAsso entity incomplete');
|
|
|
+ }
|
|
|
+
|
|
|
+ $event = $this->eventRepository->find($eventId);
|
|
|
+ if (!$event) {
|
|
|
+ throw new \RuntimeException('Event not found');
|
|
|
+ }
|
|
|
+
|
|
|
+ $helloAssoFormSlug = $event->getHelloAssoSlug();
|
|
|
+ if (!$helloAssoFormSlug) {
|
|
|
+ throw new \RuntimeException('HelloAsso form slug not found');
|
|
|
+ }
|
|
|
+
|
|
|
+ return $this->getHelloAssoEventForm($organizationId, $helloAssoFormSlug);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function getHelloAssoEntityFor(int $organizationId): HelloAsso
|
|
|
+ {
|
|
|
+ $organization = $this->organizationRepository->find($organizationId);
|
|
|
+ if (!$organization) {
|
|
|
+ throw new \RuntimeException('Organization not found');
|
|
|
+ }
|
|
|
+ $helloAssoEntity = $organization->getHelloAsso();
|
|
|
+ if (!$helloAssoEntity) {
|
|
|
+ throw new \RuntimeException('HelloAsso entity not found');
|
|
|
+ }
|
|
|
+ return $helloAssoEntity;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Construit un objet EventForm à partir des données retournées par l'api HelloAsso.
|
|
|
+ * @param array $formData
|
|
|
+ * @return EventForm
|
|
|
+ */
|
|
|
+ protected function makeHelloAssoEventForm(array $formData): EventForm {
|
|
|
+ $form = new EventForm();
|
|
|
+ $form->setSlug($formData['formSlug']);
|
|
|
+ $form->setTitle($formData['title']);
|
|
|
+ $form->setWidgetUrl($formData['widgetFullUrl']);
|
|
|
+
|
|
|
+ return $form;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* Génère l'URL de rappel pour les callbacks suite à l'authentification HelloAsso
|
|
|
@@ -351,7 +423,7 @@ class HelloAssoService extends ApiRequestService
|
|
|
return $helloAssoEntity;
|
|
|
}
|
|
|
|
|
|
- return $this->refreshToken($helloAssoEntity);
|
|
|
+ return $this->refreshTokens($helloAssoEntity);
|
|
|
}
|
|
|
|
|
|
public function refreshTokens(HelloAsso $helloAssoEntity): HelloAsso {
|