Skip to content

Commit

Permalink
Refactoring and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienHarper committed Jan 29, 2018
1 parent 8ada279 commit 4abe9c9
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 11 deletions.
34 changes: 27 additions & 7 deletions src/DoctrineAuditBundle/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ public function __construct(AuditConfiguration $configuration, EntityManagerInte
$this->entityManager = $entityManager;
}

/**
* @return AuditConfiguration
*/
public function getConfiguration(): AuditConfiguration
{
return $this->configuration;
}

/**
* Returns an array of audit table names indexed by entity FQN
*
* @return array
*/
public function getEntities(): array
{
$entities = $this->entityManager->getConfiguration()->getMetadataDriverImpl()->getAllClassNames();
Expand All @@ -41,12 +49,21 @@ public function getEntities(): array
return $audited;
}

public function getAudits(string $entity, int $id = null, int $page = 1, int $pageSize = 50): array
/**
* Returns an array of audited entries/operations
*
* @param string|object $entity
* @param int|null $id
* @param int $page
* @param int $pageSize
* @return array
*/
public function getAudits($entity, int $id = null, int $page = 1, int $pageSize = 50): array
{
$connection = $this->entityManager->getConnection();
$auditTable = implode('', [
$this->configuration->getTablePrefix(),
$this->getEntityTableName($entity),
$this->getEntityTableName(is_string($entity) ? $entity : get_class($entity)),
$this->configuration->getTableSuffix(),
]);

Expand All @@ -73,12 +90,17 @@ public function getAudits(string $entity, int $id = null, int $page = 1, int $pa
;
}

public function getAudit(string $entity, int $id)
/**
* @param string|object $entity
* @param int $id
* @return mixed
*/
public function getAudit($entity, int $id)
{
$connection = $this->entityManager->getConnection();
$auditTable = implode('', [
$this->configuration->getTablePrefix(),
$this->getEntityTableName($entity),
$this->getEntityTableName(is_string($entity) ? $entity : get_class($entity)),
$this->configuration->getTableSuffix(),
]);

Expand All @@ -101,9 +123,7 @@ public function getAudit(string $entity, int $id)
/**
* Returns the table name of $entity.
*
* @param EntityManagerInterface $em
* @param $entity
*
* @param string|object $entity
* @return string
*/
public function getEntityTableName($entity): string
Expand Down
8 changes: 4 additions & 4 deletions src/DoctrineAuditBundle/Controller/AuditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public function listAuditsAction()
public function showEntityHistoryAction(string $entity, int $id = null, int $page = 1, int $pageSize = 50)
{
$reader = $this->container->get('dh_doctrine_audit.reader');
$results = $reader->getAudits($entity, $id, $page, $pageSize);
$entries = $reader->getAudits($entity, $id, $page, $pageSize);

return $this->render('DHDoctrineAuditBundle:Audit:entity_history.html.twig', [
'entity' => $entity,
'entries' => $results,
'entries' => $entries,
]);
}

Expand All @@ -48,11 +48,11 @@ public function showEntityHistoryAction(string $entity, int $id = null, int $pag
public function showAuditEntryAction(string $entity, int $id)
{
$reader = $this->container->get('dh_doctrine_audit.reader');
$results = $reader->getAudit($entity, $id);
$data = $reader->getAudit($entity, $id);

return $this->render('DHDoctrineAuditBundle:Audit:entity_audit_details.html.twig', [
'entity' => $entity,
'entry' => $results[0],
'entry' => $data[0],
]);
}
}
6 changes: 6 additions & 0 deletions src/DoctrineAuditBundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ services:
arguments: ["@dh_doctrine_audit.configuration"]
tags:
- { name: doctrine.event_subscriber, connection: default }

dh_doctrine_audit.twig_extension:
class: DH\DoctrineAuditBundle\Twig\Extension\TwigExtension
arguments: ['@doctrine']
tags:
- { name: twig.extension }
57 changes: 57 additions & 0 deletions src/DoctrineAuditBundle/Twig/Extension/TwigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace DH\DoctrineAuditBundle\Twig\Extension;

use Symfony\Bridge\Doctrine\RegistryInterface;

class TwigExtension extends \Twig_Extension
{
protected $doctrine;

public function getFunctions()
{
// Register the function in twig :
// In your template you can use it as : {{findUser(123)}}
return [
new \Twig_SimpleFunction('findUser', [$this, 'findUser']),
new \Twig_SimpleFunction('class', [$this, 'getClass']),
new \Twig_SimpleFunction('tablename', [$this, 'getTablename']),
];
}

public function __construct(RegistryInterface $doctrine)
{
$this->doctrine = $doctrine;
}

public function findUser($id, $repository)
{
$em = $this->doctrine->getManager();
$repo = $em->getRepository($repository);

return $repo->find($id);
}

public function getClass($entity)
{
return get_class($entity);
}

public function getTablename($entity)
{
return $this
->doctrine
->getManager()
->getClassMetadata(get_class($entity))
->table['name']
;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'twig_extensions';
}
}

0 comments on commit 4abe9c9

Please sign in to comment.