Typo3Service.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Service\Typo3;
  3. use App\Service\Utils\UrlBuilder;
  4. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  5. use Symfony\Contracts\HttpClient\HttpClientInterface;
  6. use Symfony\Contracts\HttpClient\ResponseInterface;
  7. /**
  8. * Service d'appel à l'API de l'instance Typo3
  9. */
  10. class Typo3Service
  11. {
  12. public function __construct(
  13. private HttpClientInterface $typo3_client,
  14. )
  15. {}
  16. /**
  17. * Send a command to the given route of the Typo3 API
  18. *
  19. * @param string $route
  20. * @param array $parameters
  21. *
  22. * @return ResponseInterface
  23. * @throws TransportExceptionInterface
  24. */
  25. protected function sendCommand(string $route, array $parameters): ResponseInterface
  26. {
  27. $url = UrlBuilder::concatParameters('/typo3/index.php?route=' . $route, $parameters);
  28. return $this->typo3_client->request('GET', $url);
  29. }
  30. /**
  31. * Clear the cache of the given organization's website
  32. * @throws TransportExceptionInterface
  33. */
  34. public function clearSiteCache(int $organizationId): ResponseInterface
  35. {
  36. return $this->sendCommand('/otadmin/site/clear-cache', ['organization-id' => $organizationId]);
  37. }
  38. /**
  39. * Create a new Typo3 website for the given organization
  40. * @throws TransportExceptionInterface
  41. */
  42. public function createSite(int $organizationId): ResponseInterface
  43. {
  44. return $this->sendCommand('/otadmin/site/create', ['organization-id' => $organizationId]);
  45. }
  46. /**
  47. * Update the given organization's website with the Opentalent DB data
  48. * @throws TransportExceptionInterface
  49. */
  50. public function updateSite(int $organizationId): ResponseInterface
  51. {
  52. return $this->sendCommand('/otadmin/site/update', ['organization-id' => $organizationId]);
  53. }
  54. /**
  55. * Mark the given organization's website as deleted. This can be reverted with 'undeleteSite'
  56. * @throws TransportExceptionInterface
  57. */
  58. public function deleteSite(int $organizationId): ResponseInterface
  59. {
  60. return $this->sendCommand('/otadmin/site/delete', ['organization-id' => $organizationId]);
  61. }
  62. /**
  63. * Restore a website that has been deleted with 'deleteSite'
  64. * @throws TransportExceptionInterface
  65. */
  66. public function undeleteSite(int $organizationId): ResponseInterface
  67. {
  68. return $this->sendCommand('/otadmin/site/undelete', ['organization-id' => $organizationId]);
  69. }
  70. /**
  71. * Set a custom domain for the given website
  72. * @throws TransportExceptionInterface
  73. */
  74. public function setSiteDomain(int $organizationId, string $newDomain, bool $addRedirection=false): ResponseInterface
  75. {
  76. $params = ['organization-id' => $organizationId, 'domain' => $newDomain];
  77. if ($addRedirection) {
  78. $params['redirect'] = 1;
  79. }
  80. return $this->sendCommand('/otadmin/site/set-domain', $params);
  81. }
  82. /**
  83. * Reset the permissions of the BE users of the given organization's website
  84. * @throws TransportExceptionInterface
  85. */
  86. public function resetSitePerms(int $organizationId): ResponseInterface
  87. {
  88. return $this->sendCommand('/otadmin/site/reset-perms', ['organization-id' => $organizationId]);
  89. }
  90. /**
  91. * Returns the given organization's website status
  92. * @throws TransportExceptionInterface
  93. */
  94. public function getSiteStatus(int $organizationId): ResponseInterface
  95. {
  96. return $this->sendCommand('/otadmin/site/status', ['organization-id' => $organizationId]);
  97. }
  98. /**
  99. * Returns the given organization's website status
  100. * @throws TransportExceptionInterface
  101. */
  102. public function addRedirection(string $fromDomain, string $toDomain): ResponseInterface
  103. {
  104. return $this->sendCommand(
  105. '/otadmin/redirect/add',
  106. ['from-domain' => $fromDomain, 'to-domain' => $toDomain]
  107. );
  108. }
  109. }