Skip to content

Commit be29e73

Browse files
committed
no issue - introduce anonymizator Context, move salt into context
1 parent e8671d4 commit be29e73

18 files changed

+106
-152
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
## Next
44

55
* [feature] 🌟 String pattern anonymizer, build complex strings by fetching values from other anonymizers.
6+
* [internal] introduce anonymizer context for carrying environment configuration to anonymizers (#235).
7+
* [bc] Salt in `AbstractAnonymizer::$option->get('salt')` in now in `AbstractAnonymizer::$context->salt` (#235).
8+
* [bc] `AbstractAnonymizer::__construct()` now expects an additional `$context` parameter (#235).
9+
* [bc] `Anonymizator::__construct()` `$salt` parameter was removed (#235).
10+
* [fix] Some minor PHP 8.4 deprecations.
611

712
## 2.0.3
813

src/Anonymization/Anonymizator.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@ class Anonymizator implements LoggerAwareInterface
4343
];
4444

4545
private OutputInterface $output;
46+
private readonly Context $defaultContext;
4647

4748
public function __construct(
4849
private DatabaseSession $databaseSession,
4950
private AnonymizerRegistry $anonymizerRegistry,
5051
private AnonymizationConfig $anonymizationConfig,
51-
private ?string $salt = null,
52-
private readonly Context $defaultContext = new Context(),
52+
?Context $defaultContext = null,
5353
) {
5454
$this->logger = new NullLogger();
5555
$this->output = new NullOutput();
56+
$this->defaultContext = $defaultContext ?? new Context();
5657
}
5758

5859
/**
@@ -73,14 +74,10 @@ public function setOutput(OutputInterface $output): self
7374
return $this;
7475
}
7576

77+
#[\Deprecated(message: "Will be removed in 3.0, use Context::generateRandomSalt() instead.", since: "2.1.0")]
7678
public static function generateRandomSalt(): string
7779
{
78-
return \base64_encode(\random_bytes(12));
79-
}
80-
81-
protected function getSalt(): string
82-
{
83-
return $this->salt ??= self::generateRandomSalt();
80+
return Context::generateRandomSalt();
8481
}
8582

8683
/**
@@ -91,8 +88,7 @@ protected function createAnonymizer(AnonymizerConfig $config, Context $context):
9188
return $this->anonymizerRegistry->createAnonymizer(
9289
$config->anonymizer,
9390
$config,
94-
// @todo "salt" should belong to context instead.
95-
$context->withOptions($config->options->with(['salt' => $this->getSalt()])),
91+
$context,
9692
$this->databaseSession
9793
);
9894
}

src/Anonymization/Anonymizer/AbstractAnonymizer.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer;
66

7-
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizator;
87
use MakinaCorpus\QueryBuilder\DatabaseSession;
98
use MakinaCorpus\QueryBuilder\Expression;
109
use MakinaCorpus\QueryBuilder\ExpressionFactory;
@@ -18,30 +17,13 @@ abstract class AbstractAnonymizer
1817
public const JOIN_TABLE = '_target_table';
1918
public const TEMP_TABLE_PREFIX = '_db_tools_sample_';
2019

21-
/**
22-
* @todo in 3.0 move this as a constructor-promoted property.
23-
*/
24-
protected readonly Context $context;
25-
protected readonly Options $options;
26-
2720
final public function __construct(
2821
protected string $tableName,
2922
protected string $columnName,
3023
protected DatabaseSession $databaseSession,
31-
/**
32-
* @todo In 3.0, Options will be replaced with Context instead.
33-
*/
34-
Options $options,
24+
protected readonly Context $context,
25+
protected readonly Options $options,
3526
) {
36-
if ($options instanceof Context) {
37-
$this->context = $options;
38-
$this->options = $options->options;
39-
} else {
40-
\trigger_deprecation('makinacorpus/db-tools-bundle', '2.1.0', \sprintf("%s::__construct() 'Options \$options' will be changed to 'Context \$context' in 3.0", static::class));
41-
$this->options = $options;
42-
$this->context = new Context($options);
43-
}
44-
4527
$this->validateOptions();
4628
}
4729

@@ -95,9 +77,10 @@ protected function getJoinColumn(): Expression
9577
/**
9678
* Get a random, global salt for anonymizing hashed values.
9779
*/
80+
#[\Deprecated(message: "Will be removed in 3.0, use \$this->context->salt instead.", since: "2.1.0")]
9881
protected function getSalt(): string
9982
{
100-
return $this->options->get('salt') ?? Anonymizator::generateRandomSalt();
83+
return $this->context->salt;
10184
}
10285

10386
/**

src/Anonymization/Anonymizer/AnonymizerRegistry.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ public function getAllAnonymizerMetadata(): array
6767
public function createAnonymizer(
6868
string $name,
6969
AnonymizerConfig $config,
70-
Options $options,
70+
Context $context,
7171
DatabaseSession $databaseSession,
7272
): AbstractAnonymizer {
7373
$className = $this->getAnonymizerClass($name);
7474

75-
$ret = new $className($config->table, $config->targetName, $databaseSession, $options);
75+
$ret = new $className($config->table, $config->targetName, $databaseSession, $context, $config->options);
7676
\assert($ret instanceof AbstractAnonymizer);
7777

7878
if ($ret instanceof WithAnonymizerRegistry) {

src/Anonymization/Anonymizer/Context.php

Lines changed: 8 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -4,108 +4,18 @@
44

55
namespace MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer;
66

7-
/**
8-
* @todo
9-
* Remove "extends Options" in 3.0. Change AbstractAnonymizer::__construct() signature accordingly.
10-
*/
11-
class Context extends Options
7+
class Context
128
{
13-
public function __construct(
14-
public Options $options = new Options(),
15-
) {}
16-
17-
public function withOptions(Options $options): Context
18-
{
19-
return new self($options);
20-
}
21-
22-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
23-
private function throwOptionsDeprecation(string $method): never
24-
{
25-
throw new \LogicException(\sprintf("Calling %s::%s() is forbidden, this method only exists for backward compatibility purpose..", static::class, $method));
26-
}
27-
28-
#[\Override]
29-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
30-
public function has(string $name): bool
31-
{
32-
$this->throwOptionsDeprecation(__METHOD__);
33-
}
9+
public readonly string $salt;
3410

35-
#[\Override]
36-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
37-
public function get(string $name, mixed $default = null, bool $required = false): mixed
38-
{
39-
$this->throwOptionsDeprecation(__METHOD__);
40-
}
41-
42-
#[\Override]
43-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
44-
public function all(): array
45-
{
46-
$this->throwOptionsDeprecation(__METHOD__);
47-
}
48-
49-
#[\Override]
50-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
51-
public function count(): int
52-
{
53-
$this->throwOptionsDeprecation(__METHOD__);
54-
}
55-
56-
#[\Override]
57-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
58-
public function with(array $options): Options
59-
{
60-
$this->throwOptionsDeprecation(__METHOD__);
61-
}
62-
63-
#[\Override]
64-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
65-
public function toDisplayString(): string
66-
{
67-
$this->throwOptionsDeprecation(__METHOD__);
68-
}
69-
70-
#[\Override]
71-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
72-
public function getString(string $name, ?string $default = null, bool $required = false): ?string
73-
{
74-
$this->throwOptionsDeprecation(__METHOD__);
75-
}
76-
77-
#[\Override]
78-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
79-
public function getBool(string $name, ?bool $default = null, bool $required = false): ?bool
80-
{
81-
$this->throwOptionsDeprecation(__METHOD__);
82-
}
83-
84-
#[\Override]
85-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
86-
public function getInt(string $name, ?int $default = null, bool $required = false): ?int
87-
{
88-
$this->throwOptionsDeprecation(__METHOD__);
89-
}
90-
91-
#[\Override]
92-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
93-
public function getFloat(string $name, ?float $default = null, bool $required = false): ?float
94-
{
95-
$this->throwOptionsDeprecation(__METHOD__);
96-
}
97-
98-
#[\Override]
99-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
100-
public function getDate(string $name, ?\DateTimeImmutable $default = null, bool $required = false): ?\DateTimeImmutable
101-
{
102-
$this->throwOptionsDeprecation(__METHOD__);
11+
public function __construct(
12+
?string $salt = null,
13+
) {
14+
$this->salt = $salt ?? self::generateRandomSalt();
10315
}
10416

105-
#[\Override]
106-
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]
107-
public function getInterval(string $name, ?\DateInterval $default = null, bool $required = false): ?\DateInterval
17+
public static function generateRandomSalt(): string
10818
{
109-
$this->throwOptionsDeprecation(__METHOD__);
19+
return \base64_encode(\random_bytes(12));
11020
}
11121
}

src/Anonymization/Anonymizer/Core/EmailAnonymizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function createAnonymizeExpression(Update $update): Expression
3838
$userExpr = $expr->column($this->columnName, $this->tableName);
3939

4040
if ($this->options->getBool('use_salt', true)) {
41-
$userExpr = $expr->concat($userExpr, $expr->value($this->getSalt()));
41+
$userExpr = $expr->concat($userExpr, $expr->value($this->context->salt));
4242
}
4343

4444
$emailHashExpr = $expr->md5($userExpr);

src/Anonymization/Anonymizer/Core/Md5Anonymizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function createAnonymizeExpression(Update $update): Expression
2727
$columnExpr = $expr->column($this->columnName, $this->tableName);
2828

2929
if ($this->options->get('use_salt', true)) {
30-
$columnExpr = $expr->concat($columnExpr, $expr->value($this->getSalt()));
30+
$columnExpr = $expr->concat($columnExpr, $expr->value($this->context->salt));
3131

3232
// Work around some RDBMS not seeing the NULL value anymore
3333
// once we added the string concat.

src/Anonymization/Anonymizer/Core/StringPatternAnonymizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,14 @@ private function getAnonymizer(string $anonymizer, ?Options $options = null, int
192192
return $ret;
193193
}
194194

195-
$config = new AnonymizerConfig($this->tableName, $this->columnName, $anonymizer, new Options());
195+
$config = new AnonymizerConfig($this->tableName, $this->columnName, $anonymizer, $options ?? new Options());
196196

197197
return $this->childAnonymizers[$key] = $this
198198
->getAnonymizerRegistry()
199199
->createAnonymizer(
200200
$anonymizer,
201201
$config,
202-
$options ?? new Options(),
202+
$this->context,
203203
$this->databaseSession
204204
)
205205
;

tests/Unit/Anonymization/Anonymizer/AbstractMultipleColumnAnonymizerTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace MakinaCorpus\DbToolsBundle\Tests\Unit\Anonymization\Anonymizer;
66

77
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AbstractMultipleColumnAnonymizer;
8+
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Context;
89
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options;
910
use MakinaCorpus\DbToolsBundle\Attribute\AsAnonymizer;
1011
use MakinaCorpus\DbToolsBundle\Test\UnitTestCase;
@@ -17,6 +18,7 @@ public function testValidateOptionsOkWithAllColumnOption(): void
1718
'some_table',
1819
'some_column',
1920
$this->getDatabaseSession(),
21+
new Context(),
2022
new Options([
2123
'column_1' => 'actual_column_1',
2224
'column_2' => 'actual_column_2',
@@ -32,6 +34,7 @@ public function testValidateOptionsOkWithSomeColumnOption(): void
3234
'some_table',
3335
'some_column',
3436
$this->getDatabaseSession(),
37+
new Context(),
3538
new Options([
3639
'column_2' => 'actual_column_2',
3740
]),
@@ -48,7 +51,8 @@ public function testValidateOptionsKoWithNoOption(): void
4851
'some_table',
4952
'some_column',
5053
$this->getDatabaseSession(),
51-
new Options([]),
54+
new Context(),
55+
new Options(),
5256
);
5357
}
5458

@@ -60,6 +64,7 @@ public function testValidateOptionsKoWithColumnMapedTwice(): void
6064
'some_table',
6165
'some_column',
6266
$this->getDatabaseSession(),
67+
new Context(),
6368
new Options([
6469
'column_1' => 'actual_column_1',
6570
'column_2' => 'actual_column_1',

tests/Unit/Anonymization/Anonymizer/Core/ConstantAnonymizerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MakinaCorpus\DbToolsBundle\Tests\Unit\Anonymization\Anonymizer\Core;
66

7+
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Context;
78
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Core\ConstantAnonymizer;
89
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options;
910
use MakinaCorpus\DbToolsBundle\Test\UnitTestCase;
@@ -16,6 +17,7 @@ public function testValidateOptionsOkWithValueOption(): void
1617
'some_table',
1718
'some_column',
1819
$this->getDatabaseSession(),
20+
new Context(),
1921
new Options([
2022
'value' => 'test',
2123
]),
@@ -32,6 +34,7 @@ public function testValidateOptionsKoWithValueOption(): void
3234
'some_table',
3335
'some_column',
3436
$this->getDatabaseSession(),
37+
new Context(),
3538
new Options([]),
3639
);
3740
}

0 commit comments

Comments
 (0)