When using multiple storage services, you must implement a StorageMapper to route audits to the correct database. A StorageMapper is a callable that receives the audited entity's FQCN and an array of available StorageServiceInterface instances, returning the appropriate service.
Example: Routing by Entity Type
In this example, specific high-security entities are routed to a secure_entity_manager, while all others go to the default_entity_manager.
<?php
namespace App\Audit;
use App\Entity\HighSecurityEntity;
use App\Entity\SensitiveData;
use DH\Auditor\Provider\Service\StorageServiceInterface;
class StorageMapper
{
private const SECURE_ENTITIES = [
HighSecurityEntity::class,
SensitiveData::class,
];
public function __invoke(string $entity, array $storageServices): StorageServiceInterface
{
if (in_array($entity, self::SECURE_ENTITIES, true)) {
return $storageServices['dh_auditor.provider.doctrine.storage_services.doctrine.orm.secure_entity_manager'];
}
return $storageServices['dh_auditor.provider.doctrine.storage_services.doctrine.orm.default_entity_manager'];
}
}
Register the Mapper
Register your mapper class in config/packages/dh_auditor.yaml under the storage_mapper key:
dh_auditor:
providers:
doctrine:
storage_services:
- '@doctrine.orm.default_entity_manager'
- '@doctrine.orm.secure_entity_manager'
storage_mapper: 'App\Audit\StorageMapper'