Skip to content

Commit

Permalink
Table creation events come from auditing entity managers (#24)
Browse files Browse the repository at this point in the history
* Table creation events come from auditing entity managers

* Removed useless code

* Fixed auditing and storage services inversions
  • Loading branch information
DamienHarper authored Feb 10, 2021
1 parent feae12d commit 04a6a1e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
30 changes: 18 additions & 12 deletions src/Provider/Doctrine/DoctrineProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DH\Auditor\Provider\Doctrine;

use DH\Auditor\Event\LifecycleEvent;
use DH\Auditor\Exception\InvalidArgumentException;
use DH\Auditor\Exception\ProviderException;
use DH\Auditor\Provider\AbstractProvider;
use DH\Auditor\Provider\ConfigurationInterface;
Expand All @@ -17,6 +18,7 @@
use DH\Auditor\Provider\Service\AuditingServiceInterface;
use DH\Auditor\Provider\Service\StorageServiceInterface;
use Doctrine\ORM\EntityManagerInterface;
use Exception;

class DoctrineProvider extends AbstractProvider
{
Expand All @@ -40,30 +42,34 @@ public function registerAuditingService(AuditingServiceInterface $service): Prov
$evm = $entityManager->getEventManager();

// Register subscribers
$evm->addEventSubscriber(new CreateSchemaListener($this));
$evm->addEventSubscriber(new DoctrineSubscriber($this->transactionManager));

$this->loadAnnotations($entityManager);

return $this;
}

public function registerStorageService(StorageServiceInterface $service): ProviderInterface
public function isStorageMapperRequired(): bool
{
parent::registerStorageService($service);
return \count($this->getStorageServices()) > 1;
}

\assert($service instanceof StorageService); // helps PHPStan
$entityManager = $service->getEntityManager();
$evm = $entityManager->getEventManager();
public function getAuditingServiceForEntity(string $entity): AuditingServiceInterface
{
foreach ($this->auditingServices as $name => $service) {
\assert($service instanceof AuditingService); // helps PHPStan

// Register subscribers
$evm->addEventSubscriber(new CreateSchemaListener($this));
try {
// entity is managed by the entity manager of this service
$service->getEntityManager()->getClassMetadata($entity)->getTableName();

return $this;
}
return $service;
} catch (Exception $e) {
}
}

public function isStorageMapperRequired(): bool
{
return \count($this->getStorageServices()) > 1;
throw new InvalidArgumentException(sprintf('Auditing service not found for "%s".', $entity));
}

public function getStorageServiceForEntity(string $entity): StorageServiceInterface
Expand Down
12 changes: 11 additions & 1 deletion src/Provider/Doctrine/Persistence/Event/CreateSchemaListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DH\Auditor\Provider\Doctrine\DoctrineProvider;
use DH\Auditor\Provider\Doctrine\Persistence\Schema\SchemaManager;
use DH\Auditor\Provider\Doctrine\Service\StorageService;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;
Expand Down Expand Up @@ -58,8 +59,17 @@ public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs)
}
}

$storageService = $this->provider->getStorageServiceForEntity($metadata->name);
\assert($storageService instanceof StorageService); // helps PHPStan
$connection = $storageService->getEntityManager()->getConnection();
$storageSchemaManager = $connection->getSchemaManager();
$fromSchema = $storageSchemaManager->createSchema();
$sqls = [];

$updater = new SchemaManager($this->provider);
$updater->createAuditTable($metadata->name, $eventArgs->getClassTable(), $eventArgs->getSchema());
$toSchema = $updater->createAuditTable($metadata->name, $eventArgs->getClassTable(), clone $fromSchema);
$sqls[$storageService->getName()] = $fromSchema->getMigrateToSql($toSchema, $storageSchemaManager->getDatabasePlatform());
$updater->updateAuditSchema($sqls);
}

/**
Expand Down
20 changes: 11 additions & 9 deletions src/Provider/Doctrine/Persistence/Reader/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Security;
use DH\Auditor\Provider\Doctrine\Configuration;
use DH\Auditor\Provider\Doctrine\DoctrineProvider;
use DH\Auditor\Provider\Doctrine\Service\AuditingService;
use DH\Auditor\Provider\Doctrine\Service\StorageService;
use Doctrine\ORM\Mapping\ClassMetadata as ORMMetadata;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -45,9 +46,8 @@ public function createQuery(string $entity, array $options = []): Query

/** @var StorageService $storageService */
$storageService = $this->provider->getStorageServiceForEntity($entity);
$entityManager = $storageService->getEntityManager();

$query = new Query($this->getEntityAuditTableName($entity), $entityManager->getConnection());
$query = new Query($this->getEntityAuditTableName($entity), $storageService->getEntityManager()->getConnection());
$query
->addOrderBy(Query::CREATED_AT, 'DESC')
->addOrderBy(Query::ID, 'DESC')
Expand All @@ -69,7 +69,9 @@ public function createQuery(string $entity, array $options = []): Query
$query->limit($config['page_size'], ($config['page'] - 1) * $config['page_size']);
}

$metadata = $entityManager->getClassMetadata($entity);
/** @var AuditingService $auditingService */
$auditingService = $this->provider->getAuditingServiceForEntity($entity);
$metadata = $auditingService->getEntityManager()->getClassMetadata($entity);
if (
$config['strict']
&& $metadata instanceof ORMMetadata
Expand Down Expand Up @@ -157,10 +159,10 @@ public function paginate(Query $query, int $page = 1, int $pageSize = self::PAGE
*/
public function getEntityTableName(string $entity): string
{
/** @var StorageService $storageService */
$storageService = $this->provider->getStorageServiceForEntity($entity);
/** @var AuditingService $auditingService */
$auditingService = $this->provider->getAuditingServiceForEntity($entity);

return $storageService->getEntityManager()->getClassMetadata($entity)->getTableName();
return $auditingService->getEntityManager()->getClassMetadata($entity)->getTableName();
}

/**
Expand All @@ -171,9 +173,9 @@ public function getEntityAuditTableName(string $entity): string
/** @var Configuration $configuration */
$configuration = $this->provider->getConfiguration();

/** @var StorageService $storageService */
$storageService = $this->provider->getStorageServiceForEntity($entity);
$entityManager = $storageService->getEntityManager();
/** @var AuditingService $auditingService */
$auditingService = $this->provider->getAuditingServiceForEntity($entity);
$entityManager = $auditingService->getEntityManager();
$schema = '';
if ($entityManager->getClassMetadata($entity)->getSchemaName()) {
$schema = $entityManager->getClassMetadata($entity)->getSchemaName().'.';
Expand Down
2 changes: 1 addition & 1 deletion tests/Provider/Doctrine/Traits/DoctrineProviderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private function createDoctrineProviderWith1AEM2SEM(?Configuration $configuratio
// Entity manager "aem1" is used for auditing only
$provider->registerAuditingService(new AuditingService('aem1', $this->createEntityManager([
__DIR__.'/../../../../src/Provider/Doctrine/Auditing/Annotation',
__DIR__.'/../Fixtures/Entity/Standard',
__DIR__.'/../Fixtures/Entity',
])));

$db = self::getConnectionParameters();
Expand Down

0 comments on commit 04a6a1e

Please sign in to comment.