Skip to content

Commit 4b229a3

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

File tree

6 files changed

+43
-24
lines changed

6 files changed

+43
-24
lines changed

src/Anonymization/Anonymizator.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,22 @@ 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+
?string $salt = null,
53+
?Context $defaultContext = null,
5354
) {
5455
$this->logger = new NullLogger();
5556
$this->output = new NullOutput();
57+
58+
if ($salt) {
59+
\trigger_deprecation('makinacorpus/db-tools-bundle', '2.1.0', \sprintf("%s::__construct() '\$salt' will be removed in 3.0, use %s class to pass a salt.", static::class, Context::class));
60+
}
61+
$this->defaultContext = $defaultContext ?? new Context($salt);
5662
}
5763

5864
/**
@@ -73,14 +79,10 @@ public function setOutput(OutputInterface $output): self
7379
return $this;
7480
}
7581

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

8688
/**
@@ -92,7 +94,7 @@ protected function createAnonymizer(AnonymizerConfig $config, Context $context):
9294
$config->anonymizer,
9395
$config,
9496
// @todo "salt" should belong to context instead.
95-
$context->withOptions($config->options->with(['salt' => $this->getSalt()])),
97+
$context->withOptions($config->options),
9698
$this->databaseSession
9799
);
98100
}

src/Anonymization/Anonymizer/AbstractAnonymizer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ final public function __construct(
3535
) {
3636
if ($options instanceof Context) {
3737
$this->context = $options;
38-
$this->options = $options->options;
38+
$this->options = $options->options->with(['salt' => $options->salt]);
3939
} else {
4040
\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));
4141
$this->options = $options;
42-
$this->context = new Context($options);
42+
$this->context = new Context(salt: $options->getString('salt'));
4343
}
4444

4545
$this->validateOptions();
@@ -95,9 +95,10 @@ protected function getJoinColumn(): Expression
9595
/**
9696
* Get a random, global salt for anonymizing hashed values.
9797
*/
98+
#[\Deprecated(message: "Will be removed in 3.0, use \$this->context->salt instead.", since: "2.1.0")]
9899
protected function getSalt(): string
99100
{
100-
return $this->options->get('salt') ?? Anonymizator::generateRandomSalt();
101+
return $this->context->salt;
101102
}
102103

103104
/**

src/Anonymization/Anonymizer/Context.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,26 @@
1010
*/
1111
class Context extends Options
1212
{
13+
public readonly string $salt;
14+
1315
public function __construct(
16+
?string $salt = null,
17+
/**
18+
* @todo Remove this in 3.0.
19+
*/
1420
public Options $options = new Options(),
15-
) {}
21+
) {
22+
$this->salt = $salt ?? self::generateRandomSalt();
23+
}
24+
25+
public static function generateRandomSalt(): string
26+
{
27+
return \base64_encode(\random_bytes(12));
28+
}
1629

1730
public function withOptions(Options $options): Context
1831
{
19-
return new self($options);
32+
return new self($this->salt, $options);
2033
}
2134

2235
#[\Deprecated(message: "Only exists for class signature backward compatibility.", since: "2.1.0")]

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.

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

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

7-
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options;
7+
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Context;
88
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Core\EmailAnonymizer;
9+
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options;
910
use MakinaCorpus\DbToolsBundle\Test\UnitTestCase;
1011

1112
class EmailAnonymizerTest extends UnitTestCase
@@ -86,9 +87,9 @@ public function testAnonymizeWithDefaultDomain(): void
8687
'some_table',
8788
'email',
8889
$this->getDatabaseSession(),
89-
new Options([
90-
'salt' => 'my_salt',
91-
])
90+
new Context(
91+
salt: 'my_salt',
92+
),
9293
);
9394

9495
$instance->anonymize($update);
@@ -121,10 +122,12 @@ public function testAnonymize(): void
121122
'some_table',
122123
'email',
123124
$this->getDatabaseSession(),
124-
new Options([
125-
'domain' => 'makina-corpus.com',
126-
'salt' => 'my_salt',
127-
]),
125+
new Context(
126+
salt: 'my_salt',
127+
options: new Options([
128+
'domain' => 'makina-corpus.com',
129+
]),
130+
),
128131
);
129132

130133
$instance->anonymize($update);

0 commit comments

Comments
 (0)