Skip to content

Commit

Permalink
issue #142 - remove doctrine dependency in anonymizator
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Apr 19, 2024
1 parent 22a0721 commit b41e779
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [feature] ⭐️ Add Doctrine DBAL 4.0 compatibility (#140).
* [feature] ⭐️ As a side effect, Doctrine ORM 3.0 should now work (#140).
* [internal] Remove `doctrine/dbal` dependency from `Anonymizator`.
* [internal] Use `makinacorpus/query-builder` schema manager for DDL alteration (#140).
* [internal] Raise `makinacorpus/query-builder` dependency to version 1.5.3 (#140).

Expand Down
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ services:
class: MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AnonymizerRegistry
arguments: ['%kernel.project_dir%', '%db_tools.anonymization.anonymizer.paths%']
db_tools.anonymization.anonymizator.factory:
class: MakinaCorpus\DbToolsBundle\Anonymization\AnonymizatorFactory
class: MakinaCorpus\DbToolsBundle\Anonymization\Factory\DoctrineAnonymizatorFactory
arguments:
- '@doctrine'
- '@db_tools.anonymization.anonymizer.registry'
Expand Down
6 changes: 1 addition & 5 deletions src/Anonymization/Anonymizator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace MakinaCorpus\DbToolsBundle\Anonymization;

use Doctrine\DBAL\Connection;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AbstractAnonymizer;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AnonymizerRegistry;
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizationConfig;
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig;
use MakinaCorpus\DbToolsBundle\Helper\Format;
use MakinaCorpus\DbToolsBundle\Helper\Output\NullOutput;
use MakinaCorpus\DbToolsBundle\Helper\Output\OutputInterface;
use MakinaCorpus\QueryBuilder\Bridge\Doctrine\DoctrineQueryBuilder;
use MakinaCorpus\QueryBuilder\DatabaseSession;
use MakinaCorpus\QueryBuilder\Error\Server\DatabaseObjectDoesNotExistError;
use MakinaCorpus\QueryBuilder\Query\Update;
Expand All @@ -30,17 +28,15 @@ class Anonymizator implements LoggerAwareInterface
use LoggerAwareTrait;

private OutputInterface $output;
private DatabaseSession $databaseSession;

public function __construct(
Connection $connection,
private DatabaseSession $databaseSession,
private AnonymizerRegistry $anonymizerRegistry,
private AnonymizationConfig $anonymizationConfig,
private ?string $salt = null,
) {
$this->logger = new NullLogger();
$this->output = new NullOutput();
$this->databaseSession = new DoctrineQueryBuilder($connection);
}

/**
Expand Down
33 changes: 24 additions & 9 deletions src/Anonymization/AnonymizatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,61 @@

namespace MakinaCorpus\DbToolsBundle\Anonymization;

use Doctrine\DBAL\Connection;
use Doctrine\Persistence\ManagerRegistry;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AnonymizerRegistry;
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizationConfig;
use MakinaCorpus\DbToolsBundle\Anonymization\Config\Loader\LoaderInterface;
use MakinaCorpus\QueryBuilder\DatabaseSession;
use Psr\Log\LoggerInterface;

class AnonymizatorFactory
abstract class AnonymizatorFactory
{
/** @var Array<string, Anonymizator> */
private array $anonymizators = [];
/** @var LoaderInterface[] */
private array $configurationLoaders = [];

public function __construct(
private ManagerRegistry $doctrineRegistry,
private AnonymizerRegistry $anonymizerRegistry,
private ?LoggerInterface $logger = null,
) {}

/**
* Get all known connection names.
*
* @return string[]
*/
abstract protected function getKnownConnectionNames(): array;

/**
* Get database session for given connection name.
*/
abstract protected function getDatabaseSession(string $connectionName): DatabaseSession;

/**
* Add configuration loader.
*/
public function addConfigurationLoader(LoaderInterface $loader): void
{
$this->configurationLoaders[] = $loader;
}

/**
* Get anonymizator instance for given connection name.
*/
public function getOrCreate(string $connectionName): Anonymizator
{
if (isset($this->anonymizators[$connectionName])) {
return $this->anonymizators[$connectionName];
}

$connection = $this->doctrineRegistry->getConnection($connectionName);
\assert($connection instanceof Connection);

$config = new AnonymizationConfig($connectionName);

foreach ($this->configurationLoaders as $loader) {
$loader->load($config);
}

$anonymizator = new Anonymizator(
$connection,
$this->getDatabaseSession($connectionName),
$this->anonymizerRegistry,
$config
);
Expand All @@ -58,12 +71,14 @@ public function getOrCreate(string $connectionName): Anonymizator
}

/**
* Get all anonymizators for each known connection name.
*
* @return array<string, Anonymizator>
*/
public function all(): array
{
$ret = [];
foreach (\array_keys($this->doctrineRegistry->getConnections()) as $connectionName) {
foreach ($this->getKnownConnectionNames() as $connectionName) {
$ret[$connectionName] = $this->getOrCreate($connectionName);
}

Expand Down
35 changes: 35 additions & 0 deletions src/Anonymization/Factory/DoctrineAnonymizatorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace MakinaCorpus\DbToolsBundle\Anonymization\Factory;

use Doctrine\Persistence\ManagerRegistry;
use MakinaCorpus\DbToolsBundle\Anonymization\AnonymizatorFactory;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AnonymizerRegistry;
use MakinaCorpus\QueryBuilder\Bridge\Doctrine\DoctrineQueryBuilder;
use MakinaCorpus\QueryBuilder\DatabaseSession;
use Psr\Log\LoggerInterface;

class DoctrineAnonymizatorFactory extends AnonymizatorFactory
{
public function __construct(
private ManagerRegistry $doctrineRegistry,
AnonymizerRegistry $anonymizerRegistry,
?LoggerInterface $logger = null,
) {
parent::__construct($anonymizerRegistry, $logger);
}

#[\Override]
protected function getKnownConnectionNames(): array
{
return \array_keys($this->doctrineRegistry->getConnections());
}

#[\Override]
protected function getDatabaseSession(string $connectionName): DatabaseSession
{
return new DoctrineQueryBuilder($this->doctrineRegistry->getConnection($connectionName));
}
}
2 changes: 1 addition & 1 deletion src/Test/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ protected function createAnonymizatorWithConfig(AnonymizerConfig ...$anonymizerC
}

return new Anonymizator(
$this->getDoctrineConnection(),
$this->getDatabaseSession(),
new AnonymizerRegistry(),
$config
);
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/Anonymizer/AnonymizatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testMultipleAnonymizersAtOnce(): void
]),
));

$anonymizator = new Anonymizator($this->getDoctrineConnection(), new AnonymizerRegistry(), $config);
$anonymizator = new Anonymizator($this->getDatabaseSession(), new AnonymizerRegistry(), $config);
$anonymizator->addAnonymizerIdColumn('table_test');
$anonymizator->anonymize();

Expand Down Expand Up @@ -95,7 +95,7 @@ public function testSerial(): void
$actual,
);

$anonymizator = new Anonymizator($this->getDoctrineConnection(), new AnonymizerRegistry(), new AnonymizationConfig());
$anonymizator = new Anonymizator($this->getDatabaseSession(), new AnonymizerRegistry(), new AnonymizationConfig());
$anonymizator->addAnonymizerIdColumn('table_test');

if (Vendor::SQLITE === $this->getDatabaseSession()->getVendorName()) {
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Anonymization/AnonymizatorFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use Doctrine\DBAL\Connection;
use Doctrine\Persistence\ManagerRegistry;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizator;
use MakinaCorpus\DbToolsBundle\Anonymization\AnonymizatorFactory;
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AnonymizerRegistry;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options;
use MakinaCorpus\DbToolsBundle\Tests\Mock\TestingAnonymizationLoader;
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig;
use MakinaCorpus\DbToolsBundle\Anonymization\Factory\DoctrineAnonymizatorFactory;
use MakinaCorpus\DbToolsBundle\Test\UnitTestCase;
use MakinaCorpus\DbToolsBundle\Tests\Mock\TestingAnonymizationLoader;

class AnonymizatorFactoryTest extends UnitTestCase
{
Expand All @@ -27,7 +27,7 @@ public function testGetOrCreateWithEmptyConfig(): void
->willReturn($entityManager)
;

$factory = new AnonymizatorFactory(
$factory = new DoctrineAnonymizatorFactory(
$doctrineRegistry,
$this->createMock(AnonymizerRegistry::class)
);
Expand Down Expand Up @@ -58,7 +58,7 @@ public function testGetOrCreateWithConfig(): void
->willReturn($entityManager)
;

$factory = new AnonymizatorFactory(
$factory = new DoctrineAnonymizatorFactory(
$doctrineRegistry,
$this->createMock(AnonymizerRegistry::class),
);
Expand Down

0 comments on commit b41e779

Please sign in to comment.