MobytService.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Mobyt;
  4. use App\Service\ApiRequestService;
  5. use JetBrains\PhpStorm\Pure;
  6. use Symfony\Contracts\HttpClient\HttpClientInterface;
  7. /**
  8. * Service d'appel à l'API Mobyt
  9. */
  10. class MobytService extends ApiRequestService
  11. {
  12. private string $userId;
  13. private string $sessionKey;
  14. #[Pure]
  15. function __construct(
  16. HttpClientInterface $mobyt_client
  17. )
  18. {
  19. parent::__construct($mobyt_client);
  20. }
  21. /**
  22. * Send a connexion request to mobyt and get the user id and session key from the response
  23. *
  24. * @param string $login
  25. * @param string $password
  26. * @return void
  27. */
  28. private function connect(string $login, string $password): void
  29. {
  30. $responseContent = $this->getContent('login', ['username' => $login, 'password' => $password]);
  31. list($this->userId, $this->sessionKey) = explode(';', $responseContent);
  32. }
  33. /**
  34. * Get a dolibarr society by its opentalent organization id
  35. *
  36. * @param int $organizationId
  37. * @param string $login
  38. * @param string $password
  39. * @return array
  40. */
  41. public function getUserStatus(int $organizationId, string $login, string $password): array {
  42. $this->connect($login, $password);
  43. return $this->getJsonContent(
  44. 'status',
  45. ['getMoney' => 'true', 'typeAliases' => 'true'],
  46. [
  47. 'headers' => [ 'user_key' => $this->userId, 'Session_key' => $this->sessionKey ]
  48. ]
  49. );
  50. }
  51. }