- move elixForms API logic to specific folder
- move everything else under Api folder
This commit is contained in:
2026-07-13 17:34:43 +02:00
parent 1766057cb9
commit 661e2db8b1
27 changed files with 294 additions and 129 deletions
@@ -0,0 +1,87 @@
<?php
namespace ElixForms\Auth;
use ElixForms\Exceptions\ElixFormsException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
class ElixFormsAuthenticationClient {
private $baseUrl;
private $httpClient;
public function __construct(string $baseUrl, Client $httpClient = null) {
$this->baseUrl = rtrim($baseUrl, '/');
$this->httpClient = $httpClient ?? new Client(['timeout' => 10.0, 'http_errors' => false]);
}
/**
* Esegue il login verso l'API di elixForms.
*
* @param string $username
* @param string $password
* @return string Il token di autenticazione (authToken)
* @throws ElixFormsException Se le credenziali sono errate o c'è un errore server
*/
public function login(string $username, string $password): string {
$url = $this->baseUrl . '/eF/services/api/authentication/login/v1';
try {
$response = $this->httpClient->post($url, [
'form_params' => [
'username' => $username,
'password' => $password
]
]);
$status = $response->getStatusCode();
if ($status !== 200) {
throw new ElixFormsException("Errore durante il login elixForms. HTTP Status: " . $status);
}
$body = $response->getBody()->getContents();
$json = json_decode($body, true);
if (json_last_error() !== JSON_ERROR_NONE || empty($json)) {
throw new ElixFormsException("Risposta non valida dal server elixForms: atteso JSON.");
}
if (isset($json['value']['authToken'])) {
return $json['value']['authToken'];
}
throw new ElixFormsException("authToken non trovato nella risposta del login elixForms.");
} catch (GuzzleException $e) {
throw new ElixFormsException("Errore di connessione al server elixForms: " . $e->getMessage(), 0, $e);
}
}
/**
* Effettua il logout invalidando il token sul server elixForms.
*
* @param string $username
* @param string $token
* @return bool True se il logout ha successo
* @throws ElixFormsException Se c'è un errore durante il logout
*/
public function logout(string $username, string $token): bool {
$url = $this->baseUrl . '/eF/services/api/authentication/' . urlencode($username) . '/logout/v1';
try {
$response = $this->httpClient->post($url, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/x-www-form-urlencoded'
]
]);
$status = $response->getStatusCode();
if ($status !== 200) {
throw new ElixFormsException("Errore durante il logout elixForms. HTTP Status: " . $status);
}
return true;
} catch (GuzzleException $e) {
throw new ElixFormsException("Errore di connessione al server elixForms: " . $e->getMessage(), 0, $e);
}
}
}
+52
View File
@@ -0,0 +1,52 @@
<?php
namespace ElixForms;
use ElixForms<Auth\ElixFormsAuthenticationClient as AuthClient;
use ElixForms\Exceptions\ElixFormsException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
class ElixFormsClient
{
private string $baseUrl;
private Client $httpClient;
public function __construct(string $baseUrl, Client $httpClient = null)
{
$this->baseUrl = rtrim($baseUrl, '/');
$this->httpClient = $httpClient ?? new Client(['timeout' => 10.0, 'http_errors' => false]);
}
public function auth(): AuthClient
{
return new AuthClient($this->baseUrl, $this->httpClient);
}
public function request(string $method, string $path, ?array $body = null, array $headers = []): array
{
$url = $this->baseUrl . '/' . ltrim($path, '/');
$options = ['headers' => $headers];
if ($body !== null) {
$options['json'] = $body;
}
try {
$response = $this->httpClient->request($method, $url, $options);
$status = $response->getStatusCode();
$bodyRaw = $response->getBody()->getContents();
$contentType = $response->getHeaderLine('Content-Type');
$isJson = stripos($contentType, 'application/json') !== false;
return [
'status' => $status,
'headers' => $response->getHeaders(),
'body' => $bodyRaw,
'is_json' => $isJson,
'json' => $isJson ? json_decode($bodyRaw, true) : null,
];
} catch (GuzzleException $e) {
throw new ElixFormsException('Errore di connessione al server elixForms: ' . $e->getMessage(), 0, $e);
}
}
}
@@ -0,0 +1,7 @@
<?php
namespace ElixForms\Exceptions;
use Exception;
class ElixFormsException extends Exception {
}