Compare commits

...
8 Commits
Author SHA1 Message Date
pierpaolo.mammi ad6fa5ef2a more optimizations 2026-07-22 17:01:18 +02:00
pierpaolo.mammi 6e3665696c cleanup login function 2026-07-22 16:15:00 +02:00
pierpaolo.mammi 99a4f2787b rename function for better readability 2026-07-22 16:14:45 +02:00
pierpaolo.mammi 5ae01c3038 change field lookup logic 2026-07-22 16:14:00 +02:00
pierpaolo.mammi 90851908b7 optimize global calls 2026-07-22 16:13:07 +02:00
pierpaolo.mammi 8c61b69320 fix api urls 2026-07-22 16:11:45 +02:00
pierpaolo.mammi 48c120ab01 optimize sprintf calls 2026-07-22 15:57:32 +02:00
pierpaolo.mammi ea8fbaebfe update composer package name 2026-07-22 12:55:35 +02:00
7 changed files with 55 additions and 47 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
{ {
"name": "yourorg/api", "name": "elixforms-web-services/api",
"require": { "require": {
"php": ">=7.4", "php": ">=7.4",
"guzzlehttp/guzzle": "^7.0", "guzzlehttp/guzzle": "^7.0",
+14 -16
View File
@@ -41,7 +41,7 @@ class ElixFormsController
$username = $this->config->secret('elixforms_api_username'); $username = $this->config->secret('elixforms_api_username');
$password = $this->config->secret('elixforms_api_password'); $password = $this->config->secret('elixforms_api_password');
if (!is_string($username) || trim($username) === '' || !is_string($password) || $password === '') { if (!\is_string($username) || trim($username) === '' || !\is_string($password) || $password === '') {
throw new ElixFormsException('Credenziali elixForms non configurate.'); throw new ElixFormsException('Credenziali elixForms non configurate.');
} }
@@ -50,7 +50,7 @@ class ElixFormsController
$matchingInstances = []; $matchingInstances = [];
foreach ($instances as $instance) { foreach ($instances as $instance) {
if (!is_array($instance)) { if (!\is_array($instance)) {
continue; continue;
} }
@@ -67,18 +67,16 @@ class ElixFormsController
$exportGroup $exportGroup
); );
foreach ($exportTags as $exportTag) { $exportTagsByName = array_column($exportTags, null, 'name');
if (!is_array($exportTag) || !isset($exportTag['name'])) { if (!\array_key_exists($fieldName, $exportTagsByName)) {
continue; continue;
} }
$name = (string) $exportTag['name']; $exportTag = $exportTagsByName[$fieldName];
$value = isset($exportTag['value']) ? (string) $exportTag['value'] : ''; $value = isset($exportTag['value']) ? (string) $exportTag['value'] : '';
if (strcasecmp($name, $fieldName) === 0 && stripos($value, $fieldValue) !== false) { if (stripos($value, $fieldValue) !== false) {
$matchingInstances[] = $instance; $matchingInstances[] = $instance;
break;
}
} }
} }
@@ -87,7 +85,7 @@ class ElixFormsController
private function requiredString(array $values, string $key): ?string private function requiredString(array $values, string $key): ?string
{ {
if (!isset($values[$key]) || !is_string($values[$key])) { if (!isset($values[$key]) || !\is_string($values[$key])) {
return null; return null;
} }
@@ -98,11 +96,11 @@ class ElixFormsController
private function optionalNonEmptyString(array $values, string $key, string $default): ?string private function optionalNonEmptyString(array $values, string $key, string $default): ?string
{ {
if (!array_key_exists($key, $values)) { if (!\array_key_exists($key, $values)) {
return $default; return $default;
} }
if (!is_string($values[$key])) { if (!\is_string($values[$key])) {
return null; return null;
} }
@@ -114,7 +112,7 @@ class ElixFormsController
private function requestId(array $instance) private function requestId(array $instance)
{ {
foreach (['idDomanda', 'requestId', 'idRequest'] as $key) { foreach (['idDomanda', 'requestId', 'idRequest'] as $key) {
if (isset($instance[$key]) && (is_int($instance[$key]) || is_string($instance[$key]))) { if (isset($instance[$key]) && (\is_int($instance[$key]) || \is_string($instance[$key]))) {
return $instance[$key]; return $instance[$key];
} }
} }
+3 -3
View File
@@ -53,7 +53,7 @@ class Container {
*/ */
public function make(string $abstract) { public function make(string $abstract) {
// return existing singleton instance if already created // return existing singleton instance if already created
if (array_key_exists($abstract, $this->instances) && $this->instances[$abstract] !== null) { if (\array_key_exists($abstract, $this->instances) && $this->instances[$abstract] !== null) {
return $this->instances[$abstract]; return $this->instances[$abstract];
} }
@@ -70,7 +70,7 @@ class Container {
if (is_callable($concrete)) { if (is_callable($concrete)) {
// factory receives the container // factory receives the container
$object = $concrete($this); $object = $concrete($this);
} elseif (is_string($concrete) && class_exists($concrete)) { } elseif (\is_string($concrete) && class_exists($concrete)) {
$object = $this->build($concrete); $object = $this->build($concrete);
} else { } else {
throw new \Exception("Invalid binding for [{$abstract}]"); throw new \Exception("Invalid binding for [{$abstract}]");
@@ -78,7 +78,7 @@ class Container {
} }
// if abstract was registered as singleton, cache the instance // if abstract was registered as singleton, cache the instance
if (array_key_exists($abstract, $this->instances)) { if (\array_key_exists($abstract, $this->instances)) {
$this->instances[$abstract] = $object; $this->instances[$abstract] = $object;
} }
+1 -1
View File
@@ -55,7 +55,7 @@ class Json
public static function sendError($message = 'Bad Request', int $status = 400, ?LoggerInterface $logger = null): void public static function sendError($message = 'Bad Request', int $status = 400, ?LoggerInterface $logger = null): void
{ {
$payload = [ $payload = [
'error' => is_array($message) ? $message : ['message' => $message] 'error' => \is_array($message) ? $message : ['message' => $message]
]; ];
if ($logger) { if ($logger) {
+14 -14
View File
@@ -36,11 +36,11 @@ class ElixFormsApiClient
} }
$queryParts[] = 'moduleTag=' . rawurlencode($moduleTag); $queryParts[] = 'moduleTag=' . rawurlencode($moduleTag);
$url = $this->baseUrl . '/eF/api/request/lookup/by-status?' . implode('&', $queryParts); $url = "{$this->baseUrl}/api/request/lookup/by-status?" . implode('&', $queryParts);
$payload = $this->getJson($url, $authToken, $username, 'LookupByStatus'); $payload = $this->getJsonAsArray($url, $authToken, $username, 'LookupByStatus');
$requests = $payload['value']['requests'] ?? []; $requests = $payload['value']['requests'] ?? [];
if (!is_array($requests)) { if (!\is_array($requests)) {
throw new ElixFormsException('Risposta LookupByStatus non valida: requests deve essere un array.'); throw new ElixFormsException('Risposta LookupByStatus non valida: requests deve essere un array.');
} }
@@ -64,31 +64,31 @@ class ElixFormsApiClient
throw new \InvalidArgumentException('exportGroup deve essere una stringa non vuota.'); throw new \InvalidArgumentException('exportGroup deve essere una stringa non vuota.');
} }
$url = sprintf( $url = \sprintf(
'%s/eF/services/api/request/%s/view/_DEFAULT/exportTags/get/v1?moduleTag=%s&exportGroup=%s', '%s/services/api/request/%s/view/_DEFAULT/exportTags/get/v1?moduleTag=%s&exportGroup=%s',
$this->baseUrl, $this->baseUrl,
rawurlencode((string) $requestId), rawurlencode((string) $requestId),
rawurlencode($moduleTag), rawurlencode($moduleTag),
rawurlencode($exportGroup) rawurlencode($exportGroup)
); );
$payload = $this->getJson($url, $authToken, $username, 'GetExportTags'); $payload = $this->getJsonAsArray($url, $authToken, $username, 'GetExportTags');
$exportTags = $payload['value']['exportTags'] ?? []; $exportTags = $payload['value']['exportTags'] ?? [];
if (!is_array($exportTags)) { if (!\is_array($exportTags)) {
throw new ElixFormsException('Risposta GetExportTags non valida: exportTags deve essere un array.'); throw new ElixFormsException('Risposta GetExportTags non valida: exportTags deve essere un array.');
} }
return array_values($exportTags); return array_values($exportTags);
} }
private function getJson( private function getJsonAsArray(
string $url, string $url,
string $authToken, string $authToken,
string $username, string $username,
string $operation string $operation
): array { ): array {
$response = $this->httpClient->get($url, [ $response = $this->httpClient->get($url, [
'Authorization' => 'Bearer ' . $authToken, 'Authorization' => "Bearer {$authToken}",
'Accept' => 'application/json', 'Accept' => 'application/json',
'x-requested-with' => 'XMLHttpRequest', 'x-requested-with' => 'XMLHttpRequest',
'x-api-username' => $username, 'x-api-username' => $username,
@@ -96,7 +96,7 @@ class ElixFormsApiClient
$status = $response['status'] ?? 500; $status = $response['status'] ?? 500;
if ($status !== 200) { if ($status !== 200) {
throw new ElixFormsException(sprintf( throw new ElixFormsException(\sprintf(
'Errore durante %s. HTTP Status: %d', 'Errore durante %s. HTTP Status: %d',
$operation, $operation,
$status $status
@@ -105,13 +105,13 @@ class ElixFormsApiClient
$payload = $response['json'] ?? null; $payload = $response['json'] ?? null;
$jsonError = JSON_ERROR_NONE; $jsonError = JSON_ERROR_NONE;
if (!is_array($payload) && isset($response['body']) && is_string($response['body'])) { if (!\is_array($payload) && isset($response['body']) && \is_string($response['body'])) {
$payload = json_decode($response['body'], true); $payload = json_decode($response['body'], true);
$jsonError = json_last_error(); $jsonError = json_last_error();
} }
if (!is_array($payload) || $jsonError !== JSON_ERROR_NONE) { if (!\is_array($payload) || $jsonError !== JSON_ERROR_NONE) {
throw new ElixFormsException(sprintf( throw new ElixFormsException(\sprintf(
'Risposta non valida da %s: atteso JSON.', 'Risposta non valida da %s: atteso JSON.',
$operation $operation
)); ));
@@ -120,7 +120,7 @@ class ElixFormsApiClient
$globalStatus = $payload['value']['globalStatus'] ?? null; $globalStatus = $payload['value']['globalStatus'] ?? null;
if ($globalStatus === 'ERROR') { if ($globalStatus === 'ERROR') {
$description = $payload['value']['description'] ?? 'errore non specificato'; $description = $payload['value']['description'] ?? 'errore non specificato';
throw new ElixFormsException(sprintf( throw new ElixFormsException(\sprintf(
'%s ha restituito un errore: %s', '%s ha restituito un errore: %s',
$operation, $operation,
$description $description
@@ -23,11 +23,15 @@ class ElixFormsAuthenticationClient {
* @throws ElixFormsException Se le credenziali sono errate o c'è un errore server * @throws ElixFormsException Se le credenziali sono errate o c'è un errore server
*/ */
public function login(string $username, string $password): string { public function login(string $username, string $password): string {
$url = $this->baseUrl . '/eF/services/api/authentication/login/v1'; $url = "{$this->baseUrl}/services/api/authentication/login/v1";
try { try {
$response = $this->httpClient->post($url, [ $response = $this->httpClient->post($url, [
'form_params' => [ 'headers' => [
'x-requested-with' => 'XMLHttpRequest',
'Content-Type' => 'application/json'
],
'json' => [
'username' => $username, 'username' => $username,
'password' => $password 'password' => $password
] ]
@@ -64,12 +68,12 @@ class ElixFormsAuthenticationClient {
* @throws ElixFormsException Se c'è un errore durante il logout * @throws ElixFormsException Se c'è un errore durante il logout
*/ */
public function logout(string $username, string $token): bool { public function logout(string $username, string $token): bool {
$url = $this->baseUrl . '/eF/services/api/authentication/' . urlencode($username) . '/logout/v1'; $url = "{$this->baseUrl}/services/api/authentication/" . urlencode($username) . '/logout/v1';
try { try {
$response = $this->httpClient->post($url, [ $response = $this->httpClient->post($url, [
'headers' => [ 'headers' => [
'Authorization' => 'Bearer ' . $token, 'Authorization' => "Bearer {$token}",
'Content-Type' => 'application/x-www-form-urlencoded' 'Content-Type' => 'application/x-www-form-urlencoded'
] ]
]); ]);
+14 -8
View File
@@ -54,16 +54,22 @@ final class ElixFormsControllerTest extends TestCase
throw new \RuntimeException('exportGroup non inoltrato al client.'); throw new \RuntimeException('exportGroup non inoltrato al client.');
} }
$values = [ $tags = [
10 => 'Nessuna corrispondenza', 10 => [
20 => 'Il contratto ABC-123 è presente', 'name' => 'contratto.altro',
30 => 'Altro valore', 'value' => 'Il contratto ABC-123 è presente',
],
20 => [
'name' => 'contratto.id',
'value' => 'Il contratto ABC-123 è presente',
],
30 => [
'name' => 'contratto.id',
'value' => 'Altro valore',
],
]; ];
return [[ return [$tags[$requestId]];
'name' => 'contratto.id',
'value' => $values[$requestId],
]];
} }
}; };
$config = new class extends Config { $config = new class extends Config {