|
|
@@ -0,0 +1,67 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Service\Mobyt;
|
|
|
+
|
|
|
+use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
+use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
|
|
|
+use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
|
|
+use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Service d'appel à l'API Mobyt
|
|
|
+ */
|
|
|
+class MobytService
|
|
|
+{
|
|
|
+ private HttpClientInterface $client;
|
|
|
+ private string $userId;
|
|
|
+ private string $sessionKey;
|
|
|
+
|
|
|
+ function __construct(
|
|
|
+ HttpClientInterface $mobyt_client
|
|
|
+ )
|
|
|
+ {
|
|
|
+ $this->client = $mobyt_client;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function connect(string $login, string $password){
|
|
|
+ $response = $this->client->request('GET', sprintf('login?username=%s&password=%s', $login, $password));
|
|
|
+ list($this->userId, $this->sessionKey) = explode(';', $response->getContent());
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get a dolibarr society by its opentalent organization id
|
|
|
+ *
|
|
|
+ * @param int $organizationId
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function getUserStatus(int $organizationId, string $login, string $password): array {
|
|
|
+ $this->connect($login, $password);
|
|
|
+ return $this->request(
|
|
|
+ 'status?getMoney=true&typeAliases=true',
|
|
|
+ 'GET',
|
|
|
+ ['headers' => [ 'user_key' => $this->userId, 'Session_key' => $this->sessionKey ]]
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Send an HTTP request to the Dolibarr API,
|
|
|
+ * and return the decoded content of the response's body
|
|
|
+ *
|
|
|
+ * @param string $path
|
|
|
+ * @param string $method
|
|
|
+ * @param array $options
|
|
|
+ * @return array
|
|
|
+ * @throws NotFoundHttpException
|
|
|
+ */
|
|
|
+ private function request(string $path, string $method = 'GET', array $options = []): array
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $uri = ltrim($path, '/');
|
|
|
+ $response = $this->client->request($method, $uri, $options);
|
|
|
+ return json_decode($response->getContent(), true);
|
|
|
+ } catch (HttpExceptionInterface | TransportExceptionInterface $e) {
|
|
|
+ throw new NotFoundHttpException('data_not_found', $e, 404);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|