Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #136

Merged
merged 6 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions src/Model/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,50 +129,6 @@ public function reset(): void
$this->dissociated = [];
}

/**
* TODO: remove for next major release.
*
* @internal
*
* @deprecated use one of the insert/update/remove/associate/dissociate methods instead
*/
public function trackAuditEvent(string $type, array $data): void
{
@trigger_error('This method is deprecated, use one of the Transaction::insert(), Transaction::update(), Transaction::remove(), Transaction::associate(), Transaction::dissociate() methods instead.', E_USER_DEPRECATED);

switch ($type) {
case self::INSERT:
\assert(2 === \count($data));
$this->insert(...$data);

break;

case self::UPDATE:
\assert(2 === \count($data));
$this->update(...$data);

break;

case self::REMOVE:
\assert(2 === \count($data));
$this->remove(...$data);

break;

case self::ASSOCIATE:
\assert(3 === \count($data));
$this->associate(...$data);

break;

case self::DISSOCIATE:
\assert(4 === \count($data));
$this->dissociate(...$data);

break;
}
}

public function insert(object $source, array $changeset): void
{
$this->inserted[] = new InsertEventDto($source, $changeset);
Expand Down
2 changes: 0 additions & 2 deletions src/Model/TransactionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ public function getRemoved(): array;
public function getAssociated(): array;

public function getDissociated(): array;

public function trackAuditEvent(string $type, array $data): void;
}
12 changes: 0 additions & 12 deletions src/Provider/Doctrine/Auditing/Annotation/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,7 @@
namespace DH\Auditor\Provider\Doctrine\Auditing\Annotation;

use Attribute;
use Doctrine\Common\Annotations\Annotation;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("CLASS")
* @Attributes({
* @Attribute("enabled", required=false, type="bool"),
* })
*/
#[Attribute(Attribute::TARGET_CLASS)]
final class Auditable
{
Expand Down
6 changes: 0 additions & 6 deletions src/Provider/Doctrine/Auditing/Annotation/Ignore.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
namespace DH\Auditor\Provider\Doctrine\Auditing\Annotation;

use Attribute;
use Doctrine\Common\Annotations\Annotation;

/**
* @Annotation
*
* @Target("PROPERTY")
*/
#[Attribute(Attribute::TARGET_PROPERTY)]
final class Ignore
{
Expand Down
17 changes: 3 additions & 14 deletions src/Provider/Doctrine/Auditing/Annotation/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,8 @@
namespace DH\Auditor\Provider\Doctrine\Auditing\Annotation;

use Attribute;
use Doctrine\Common\Annotations\Annotation;
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
use Doctrine\Common\Annotations\Annotation\Required;
use Symfony\Contracts\Service\Attribute\Required;

/**
* @Annotation
*
* @NamedArgumentConstructor
*
* @Target("CLASS")
* @Attributes({
* @Attribute("view", required=true, type="array<string>"),
* })
*/
#[Attribute(Attribute::TARGET_CLASS)]
final class Security
{
Expand All @@ -28,8 +16,9 @@ final class Security
public const VIEW_SCOPE = 'view';

/**
* @Required
* @var array<string>
*/
#[Required]
public array $view;

public function __construct(array $view)
Expand Down
30 changes: 0 additions & 30 deletions src/Provider/Doctrine/Auditing/Event/DoctrineSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@

namespace DH\Auditor\Provider\Doctrine\Auditing\Event;

use DH\Auditor\Provider\Doctrine\Auditing\Logger\Logger;
use DH\Auditor\Provider\Doctrine\Auditing\Logger\LoggerChain;
use DH\Auditor\Provider\Doctrine\Auditing\Logger\Middleware\DHDriver;
use DH\Auditor\Provider\Doctrine\Auditing\Transaction\TransactionManager;
use DH\Auditor\Provider\Doctrine\Model\Transaction;
use DH\Auditor\Provider\Doctrine\Persistence\Helper\DoctrineHelper;
use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\Logging\SQLLogger;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;

class DoctrineSubscriber implements EventSubscriber
{
private TransactionManager $transactionManager;

private ?SQLLogger $loggerBackup = null;

public function __construct(TransactionManager $transactionManager)
{
$this->transactionManager = $transactionManager;
Expand All @@ -45,32 +40,7 @@ public function onFlush(OnFlushEventArgs $args): void
$this->transactionManager->process($transaction);
$transaction->reset();
});

return;
}

trigger_deprecation('damienharper/auditor', '2.2', 'SQLLogger is deprecated. Use DHMiddleware instead');
// extend the SQL logger
$this->loggerBackup = $entityManager->getConnection()->getConfiguration()->getSQLLogger();
$auditLogger = new Logger(function () use ($entityManager, $transaction): void {
// flushes pending data
$entityManager->getConnection()->getConfiguration()->setSQLLogger($this->loggerBackup);
$this->transactionManager->process($transaction);
$transaction->reset();
});

// Initialize a new LoggerChain with the new AuditLogger + the existing SQLLoggers.
$loggerChain = new LoggerChain();
if ($this->loggerBackup instanceof LoggerChain) {
foreach ($this->loggerBackup->getLoggers() as $logger) {
$loggerChain->addLogger($logger);
}
} elseif ($this->loggerBackup instanceof SQLLogger) {
$loggerChain->addLogger($this->loggerBackup);
}

$loggerChain->addLogger($auditLogger);
$entityManager->getConnection()->getConfiguration()->setSQLLogger($loggerChain);
}

/**
Expand Down
43 changes: 0 additions & 43 deletions src/Provider/Doctrine/Auditing/Logger/Logger.php

This file was deleted.

51 changes: 0 additions & 51 deletions src/Provider/Doctrine/Auditing/Logger/LoggerChain.php

This file was deleted.

62 changes: 29 additions & 33 deletions src/Provider/Doctrine/Auditing/Transaction/TransactionHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,27 @@ private function hydrateWithScheduledCollectionUpdates(Transaction $transaction,
if (null !== $owner && $this->provider->isAudited($owner)) {
$mapping = $collection->getMapping();

if (null === $mapping) {
continue;
}

/** @var object $entity */
foreach ($collection->getInsertDiff() as $entity) {
if ($this->provider->isAudited($entity)) {
$transaction->associate(
$owner,
$entity,
$mapping,
);
if (null !== $mapping) {
/** @var object $entity */
foreach ($collection->getInsertDiff() as $entity) {
if ($this->provider->isAudited($entity)) {
$transaction->associate(
$owner,
$entity,
$mapping,
);
}
}
}

/** @var object $entity */
foreach ($collection->getDeleteDiff() as $entity) {
if ($this->provider->isAudited($entity) && $collection->getOwner()) {
$transaction->dissociate(
$owner,
$entity,
$mapping,
);
/** @var object $entity */
foreach ($collection->getDeleteDiff() as $entity) {
if ($this->provider->isAudited($entity) && $collection->getOwner()) {
$transaction->dissociate(
$owner,
$entity,
$mapping,
);
}
}
}
}
Expand All @@ -126,18 +124,16 @@ private function hydrateWithScheduledCollectionDeletions(Transaction $transactio
if (null !== $owner && $this->provider->isAudited($owner)) {
$mapping = $collection->getMapping();

if (null === $mapping) {
continue;
}

/** @var object $entity */
foreach ($collection->toArray() as $entity) {
if ($this->provider->isAudited($entity)) {
$transaction->dissociate(
$owner,
$entity,
$mapping,
);
if (null !== $mapping) {
/** @var object $entity */
foreach ($collection->toArray() as $entity) {
if ($this->provider->isAudited($entity)) {
$transaction->dissociate(
$owner,
$entity,
$mapping,
);
}
}
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/Provider/Doctrine/DoctrineProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,6 @@ public function isAudited(object|string $entity): bool

$entityOptions = $configuration->getEntities()[$class];

if (null === $entityOptions) {
// no option defined => $entity is audited
return true;
}

if (isset($entityOptions['enabled'])) {
return (bool) $entityOptions['enabled'];
}
Expand Down Expand Up @@ -208,11 +203,6 @@ public function isAuditedField(object|string $entity, string $field): bool
$class = DoctrineHelper::getRealClassName($entity);
$entityOptions = $this->configuration->getEntities()[$class];

if (null === $entityOptions) {
// no option defined => $field is audited
return true;
}

// are columns excluded and is field part of them?
// yes => $field is not audited
return !(isset($entityOptions['ignored_columns'])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void

\assert($storageService instanceof StorageService);
$platform = $storageService->getEntityManager()->getConnection()->getDatabasePlatform();
if (!$platform->supportsSchemas()) {
if (null !== $platform && !$platform->supportsSchemas()) {
$classMetadata->setPrimaryTable([
'name' => $schemaManager->resolveTableName($classMetadata->getTableName(), $classMetadata->getSchemaName() ?? '', $platform),
'schema' => '',
Expand Down
Loading