vendor/damienharper/doctrine-audit-bundle/src/DoctrineAuditBundle/Event/AuditSubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace DH\DoctrineAuditBundle\Event;
  3. use DH\DoctrineAuditBundle\Manager\AuditManager;
  4. use Doctrine\DBAL\Exception\TableNotFoundException;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class AuditSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @var AuditManager
  10.      */
  11.     private $manager;
  12.     public function __construct(AuditManager $manager)
  13.     {
  14.         $this->manager $manager;
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             LifecycleEvent::class => 'onAuditEvent',
  20.         ];
  21.     }
  22.     /**
  23.      * @param LifecycleEvent $event
  24.      *
  25.      * @throws \Doctrine\DBAL\DBALException
  26.      *
  27.      * @return LifecycleEvent
  28.      */
  29.     public function onAuditEvent(LifecycleEvent $event): LifecycleEvent
  30.     {
  31.         $payload $event->getPayload();
  32.         $auditTable $payload['table'];
  33.         unset($payload['table'], $payload['entity']);
  34.         $fields = [
  35.             'type' => ':type',
  36.             'object_id' => ':object_id',
  37.             'discriminator' => ':discriminator',
  38.             'transaction_hash' => ':transaction_hash',
  39.             'diffs' => ':diffs',
  40.             'blame_id' => ':blame_id',
  41.             'blame_user' => ':blame_user',
  42.             'blame_user_fqdn' => ':blame_user_fqdn',
  43.             'blame_user_firewall' => ':blame_user_firewall',
  44.             'ip' => ':ip',
  45.             'created_at' => ':created_at',
  46.         ];
  47.         $query sprintf(
  48.             'INSERT INTO %s (%s) VALUES (%s)',
  49.             $auditTable,
  50.             implode(', 'array_keys($fields)),
  51.             implode(', 'array_values($fields))
  52.         );
  53.         $storage $this->manager->selectStorageSpace($this->manager->getConfiguration()->getEntityManager());
  54.         $statement $storage->getConnection()->prepare($query);
  55.         foreach ($payload as $key => $value)
  56.         {
  57.             $statement->bindValue($key$value);
  58.         }
  59.         try
  60.         {
  61.             $statement->execute();
  62.         }
  63.         catch (TableNotFoundException $e)
  64.         {
  65.         }
  66.         return $event;
  67.     }
  68. }