Skip to content

Commit e8671d4

Browse files
committed
no issue - introduce anonymizator Context, for AbstractAnonymizer signature change in 3.0
1 parent ea798c7 commit e8671d4

File tree

3 files changed

+138
-5
lines changed

3 files changed

+138
-5
lines changed

src/Anonymization/Anonymizator.php

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

77
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AbstractAnonymizer;
88
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AnonymizerRegistry;
9+
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Context;
910
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizationConfig;
1011
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig;
1112
use MakinaCorpus\DbToolsBundle\Helper\Format;
@@ -48,6 +49,7 @@ public function __construct(
4849
private AnonymizerRegistry $anonymizerRegistry,
4950
private AnonymizationConfig $anonymizationConfig,
5051
private ?string $salt = null,
52+
private readonly Context $defaultContext = new Context(),
5153
) {
5254
$this->logger = new NullLogger();
5355
$this->output = new NullOutput();
@@ -84,12 +86,13 @@ protected function getSalt(): string
8486
/**
8587
* Create anonymizer instance.
8688
*/
87-
protected function createAnonymizer(AnonymizerConfig $config): AbstractAnonymizer
89+
protected function createAnonymizer(AnonymizerConfig $config, Context $context): AbstractAnonymizer
8890
{
8991
return $this->anonymizerRegistry->createAnonymizer(
9092
$config->anonymizer,
9193
$config,
92-
$config->options->with(['salt' => $this->getSalt()]),
94+
// @todo "salt" should belong to context instead.
95+
$context->withOptions($config->options->with(['salt' => $this->getSalt()])),
9396
$this->databaseSession
9497
);
9598
}
@@ -127,6 +130,7 @@ public function anonymize(
127130
}
128131

129132
$plan = [];
133+
$context = clone $this->defaultContext;
130134

131135
if ($onlyTargets) {
132136
foreach ($onlyTargets as $targetString) {
@@ -160,7 +164,7 @@ public function anonymize(
160164
foreach ($plan as $table => $targets) {
161165
$anonymizers[$table] = [];
162166
foreach ($this->anonymizationConfig->getTableConfig($table, $targets) as $target => $config) {
163-
$anonymizers[$table][] = $this->createAnonymizer($config);
167+
$anonymizers[$table][] = $this->createAnonymizer($config, $context);
164168
}
165169
}
166170

@@ -910,7 +914,7 @@ public function checkAnonymizationConfig(): array
910914
foreach ($this->anonymizationConfig->all() as $table => $tableConfig) {
911915
foreach ($tableConfig as $config) {
912916
try {
913-
$this->createAnonymizer($config);
917+
$this->createAnonymizer($config, $this->defaultContext);
914918
} catch (\Exception $e) {
915919
if (!\key_exists($table, $errors)) {
916920
$errors[$table] = [];

src/Anonymization/Anonymizer/AbstractAnonymizer.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,30 @@ abstract class AbstractAnonymizer
1818
public const JOIN_TABLE = '_target_table';
1919
public const TEMP_TABLE_PREFIX = '_db_tools_sample_';
2020

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+
2127
final public function __construct(
2228
protected string $tableName,
2329
protected string $columnName,
2430
protected DatabaseSession $databaseSession,
25-
protected Options $options,
31+
/**
32+
* @todo In 3.0, Options will be replaced with Context instead.
33+
*/
34+
Options $options,
2635
) {
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+
2745
$this->validateOptions();
2846
}
2947

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer;
6+
7+
/**
8+
* @todo
9+
* Remove "extends Options" in 3.0. Change AbstractAnonymizer::__construct() signature accordingly.
10+
*/
11+
class Context extends Options
12+
{
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+
}
34+
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__);
103+
}
104+
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
108+
{
109+
$this->throwOptionsDeprecation(__METHOD__);
110+
}
111+
}

0 commit comments

Comments
 (0)