mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-02-19 22:59:58 +00:00
Add AuditTrailPersisterInterface and improve AuditTrailPersister
- Introduced `AuditTrailPersisterInterface` to define the persisting contract. - Updated `AuditTrailPersister` to implement the interface and delegate persistence to `AuditTrailRepository`. - Added `AuditTrailRepository` with `persistImmediately` method to save audit trails directly to the database.
This commit is contained in:
@@ -12,8 +12,14 @@ declare(strict_types=1);
|
||||
namespace Chill\MainBundle\Audit;
|
||||
|
||||
use Chill\MainBundle\Entity\AuditTrail;
|
||||
use Chill\MainBundle\Repository\AuditTrailRepository;
|
||||
|
||||
class AuditTrailPersister
|
||||
final readonly class AuditTrailPersister implements AuditTrailPersisterInterface
|
||||
{
|
||||
public function persistAuditTrail(AuditTrail $auditTrail): void {}
|
||||
public function __construct(private AuditTrailRepository $auditTrailRepository) {}
|
||||
|
||||
public function persistAuditTrail(AuditTrail $auditTrail): void
|
||||
{
|
||||
$this->auditTrailRepository->persistImmediately($auditTrail);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Audit;
|
||||
|
||||
use Chill\MainBundle\Entity\AuditTrail;
|
||||
|
||||
interface AuditTrailPersisterInterface
|
||||
{
|
||||
public function persistAuditTrail(AuditTrail $auditTrail): void;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Repository;
|
||||
|
||||
use Chill\MainBundle\Entity\AuditTrail;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @implements ServiceEntityRepository<AuditTrail>
|
||||
*/
|
||||
class AuditTrailRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, AuditTrail::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist an AuditTrail without storing it in the entity manager.
|
||||
*/
|
||||
public function persistImmediately(AuditTrail $auditTrail): void
|
||||
{
|
||||
$metadata = $this->getClassMetadata();
|
||||
|
||||
$sql = $this->getEntityManager()->getConnection()->createQueryBuilder();
|
||||
$sql
|
||||
->insert($metadata->getTableName())
|
||||
->values([
|
||||
'id' => ':id',
|
||||
'action' => ':action',
|
||||
'occurredat' => ':occured_at',
|
||||
'user_id' => ':user_id',
|
||||
'description' => ':description',
|
||||
'targets' => ':targets',
|
||||
'metadata' => ':metadata',
|
||||
])
|
||||
->setParameter('id', $auditTrail->getId())
|
||||
->setParameter('action', $auditTrail->getAction())
|
||||
->setParameter('occured_at', $auditTrail->getOccurredAt(), Types::DATETIMETZ_IMMUTABLE)
|
||||
->setParameter('user_id', $auditTrail->getUser()?->getId())
|
||||
->setParameter('description', $auditTrail->getDescription())
|
||||
->setParameter('targets', $auditTrail->getTargets(), Types::JSON)
|
||||
->setParameter('metadata', $auditTrail->getMetadata(), Types::JSON);
|
||||
|
||||
$sql->executeQuery();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user