Loading...
Loading...
Loading...
Anders & A-Cube S.r.l. / 1 dicembre 2024 • 3 min read
Preservation API (Gov-It-API v2) è un servizio di archiviazione digitale production-grade che garantisce la conservazione a lungo termine di documenti elettronici in conformità con gli standard normativi italiani. Costruito con Symfony 6.2+ e API Platform 3, fornisce storage sicuro e scalabile con validazione e testing completi.
Layer applicazione Symfony 6.2+ con API Platform 3, Doctrine ORM e Validators. Layer logica business per elaborazione documenti, validazione e archiviazione. Integrazione con PostgreSQL per documenti, metadata e audit logs, e servizi AWS per S3 storage, Lambda processor e CloudWatch logs.
Risorse API Platform:
Servizi Symfony:
Entità Documento:
#[ORM\Entity]
#[ApiResource(
operations: [
new Get(),
new GetCollection(),
new Post(security: "is_granted('ROLE_USER')")
]
)]
class Document
{
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
private string $fileName;
#[ORM\Column(length: 64)]
#[Assert\NotBlank]
private string $sha256Hash;
#[ORM\Column]
private \DateTimeImmutable $uploadedAt;
#[ORM\Column(length: 50)]
#[Assert\Choice(choices: [
'pending', 'validated', 'preserved', 'error'
])]
private string $status = 'pending';
}Servizio Elaborazione Documenti: Workflow completo: validazione documento, upload su S3 con crittografia AES256, generazione metadata conservazione, aggiornamento stato documento.
Metadata Conservazione:
private function generatePreservationMetadata(Document $document): array
{
return [
'preservation_timestamp' => (new \DateTimeImmutable())->format(\DateTimeInterface::ISO8601),
'preservation_agent' => 'A-Cube Preservation API v2',
'checksum_algorithm' => 'SHA-256',
'checksum_value' => $document->getSha256Hash(),
'file_format' => mime_content_type($document->getFileName()),
'file_size' => strlen($document->getS3Key())
];
}Configurazione Behat: Suite di test con contesti per documenti, API e autenticazione. Filtri tag per escludere test work-in-progress. Integrazione Symfony2Extension per kernel e bootstrap.
Scenario Behat Esempio:
Feature: Conservazione Documenti
Per conformarmi ai requisiti normativi
Come client API
Devo poter conservare documenti in modo sicuro
Background:
Given Sono autenticato come "user@example.com"
@critical
Scenario: Conservazione successo documento PDF valido
Given Ho un documento PDF "fattura-2024.pdf"
When Invio una richiesta "POST" a "/api/documents"
Then Il codice stato risposta deve essere 201
And La risposta deve essere in JSON
And Il nodo JSON "status" deve essere uguale a "pending"
And Il nodo JSON "sha256Hash" deve corrispondere "/^[a-f0-9]{64}$/"
When Il documento viene elaborato
Then Lo stato documento deve essere "preserved"
And Il documento deve essere memorizzato in S3
And Il documento deve avere metadata conservazioneImplementazione Context Behat: Context fornisce step definitions per elaborazione documenti, verifica storage S3 e validazione metadata conservazione con asserzioni complete.
Configurazione PHPStan:
parameters:
level: 8
paths:
- src
- tests
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: true
reportUnmatchedIgnoredErrors: trueCodice Pulito PHPStan: Utilizzo template generics per type safety completo, annotazioni PHPDoc dettagliate per parametri e return types, gestione null safety rigorosa.
Setup Client S3:
class S3Service
{
public function __construct(
#[Autowire('%env(AWS_REGION)%')]
private string $region,
#[Autowire('%env(AWS_S3_BUCKET)%')]
private string $bucket
) {
$this->client = new S3Client([
'region' => $this->region,
'version' => 'latest'
]);
}
public function upload(string $key, string $content, array $metadata = []): void
{
$this->client->putObject
Invocazione Lambda per Elaborazione Documenti: Invocazione asincrona Lambda per elaborazione documenti con payload JSON contenente documentId e action. Elaborazione background senza blocco request HTTP.
docker-compose.yml: Servizi PHP (con volume mount), PostgreSQL (con persistent data), Nginx (porta 8080). Configurazione environment variables per APP_ENV, DATABASE_URL e AWS_S3_BUCKET.
# Entra nel container PHP
docker-compose exec php bash
# Installa dipendenze
composer install
# Esegui PHPStan
composer run phpstan
# Esegui test Behat
composer run behat
# Esegui tutti i controlli CI
composer ciScript Deploy:
#!/bin/bash
set -e
ENV=${ENV:-dev}
AWS_PROFILE=${AWS_PROFILE:-acube-dev}
echo "Deployment Preservation API su ambiente ${ENV}"
# 1. Esegui test
docker-compose exec -T php composer ci
# 2. Build immagine production
docker build -t preservation-api:${ENV} -f Dockerfile.prod .
# 3. Push su ECR
aws --profile=${AWS_PROFILE} ecr get-login-password --region eu-west-1 | \
docker login --username AWS --password-stdin ${ECR_REGISTRY}
Preservation API dimostra sviluppo backend enterprise-grade con qualità codice eccezionale (PHPStan livello 8) e testing completo (200+ scenari Behat), garantendo conformità normativa e archiviazione documenti affidabile a lungo termine.
Licenza: Proprietaria (A-Cube S.r.l.)