|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Pack; |
| 6 | + |
| 7 | +use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AbstractAnonymizer; |
| 8 | +use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AbstractMultipleColumnAnonymizer; |
| 9 | +use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AbstractSingleColumnAnonymizer; |
| 10 | +use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options; |
| 11 | +use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\WithAnonymizerRegistry; |
| 12 | +use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\WithAnonymizerRegistryTrait; |
| 13 | +use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig; |
| 14 | +use MakinaCorpus\DbToolsBundle\Anonymization\Pack\GeneratedIntRange; |
| 15 | +use MakinaCorpus\DbToolsBundle\Anonymization\Pack\GeneratedPart; |
| 16 | +use MakinaCorpus\DbToolsBundle\Anonymization\Pack\GeneratedRaw; |
| 17 | +use MakinaCorpus\DbToolsBundle\Anonymization\Pack\GeneratedRef; |
| 18 | +use MakinaCorpus\DbToolsBundle\Anonymization\Pack\GeneratedString; |
| 19 | +use MakinaCorpus\DbToolsBundle\Error\ConfigurationException; |
| 20 | +use MakinaCorpus\QueryBuilder\Expression; |
| 21 | +use MakinaCorpus\QueryBuilder\Query\Update; |
| 22 | + |
| 23 | +/** |
| 24 | + * Creates a CONCAT(...) expression which is filled with all parts |
| 25 | + * of a generated string pattern, using other anonymizers. |
| 26 | + * |
| 27 | + * Delta that you will encounter in anonymizers is an information |
| 28 | + * that allows the user to create more than one JOIN statement for |
| 29 | + * the same multiple column anonymizer. Per default, it's always 0 |
| 30 | + * and all columns from a same row will be shared for a single |
| 31 | + * updated line, keeping consistency. |
| 32 | + */ |
| 33 | +class GeneratedStringAnonymizer extends AbstractSingleColumnAnonymizer implements WithAnonymizerRegistry |
| 34 | +{ |
| 35 | + use WithAnonymizerRegistryTrait; |
| 36 | + |
| 37 | + private GeneratedString $generated; |
| 38 | + |
| 39 | + /** |
| 40 | + * Child anonymizers, populated during initialize() call in order to be |
| 41 | + * able to initialize them at the same time. |
| 42 | + * |
| 43 | + * It is then being used during clean() for cleanup, then dropped. |
| 44 | + * |
| 45 | + * @var array<string, AbstractAnonymizer> |
| 46 | + */ |
| 47 | + private $childAnonymizers = []; |
| 48 | + |
| 49 | + #[\Override] |
| 50 | + protected function validateOptions(): void |
| 51 | + { |
| 52 | + } |
| 53 | + |
| 54 | + #[\Override] |
| 55 | + public function initialize(): void |
| 56 | + { |
| 57 | + parent::initialize(); |
| 58 | + |
| 59 | + // Initialize all child anonymizers. |
| 60 | + foreach ($this->generated->parts as $part) { |
| 61 | + if ($part instanceof GeneratedRef) { |
| 62 | + $this->getMultipleAnonymizer($part->id, null, $part->delta)->initialize(); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + #[\Override] |
| 68 | + public function createAnonymizeExpression(Update $update): Expression |
| 69 | + { |
| 70 | + $expr = $update->expression(); |
| 71 | + |
| 72 | + $expressions = []; |
| 73 | + foreach ($this->generated->parts as $part) { |
| 74 | + \assert($part instanceof GeneratedPart); |
| 75 | + |
| 76 | + if ($part instanceof GeneratedIntRange) { |
| 77 | + // Integer anonymizer doesn't require any initialization so we |
| 78 | + // can safely do this, without any prior initialization. For |
| 79 | + // each range we have in the pattern, options will be different |
| 80 | + // so we need to create as many instances as we have ranges. |
| 81 | + $expr[] = $this |
| 82 | + ->getSingleAnonymizer( |
| 83 | + 'int', |
| 84 | + new Options([ |
| 85 | + 'min' => $part->start, |
| 86 | + 'max' => $part->stop, |
| 87 | + ], |
| 88 | + )) |
| 89 | + ->createAnonymizeExpression($update) |
| 90 | + ; |
| 91 | + } else if ($part instanceof GeneratedRaw) { |
| 92 | + $expressions[] = $expr->value($part->string, 'text'); |
| 93 | + } else if ($part instanceof GeneratedRef) { |
| 94 | + // Anonymizer was prepolulated with options during initialize() |
| 95 | + // which makes passing any options useless (the cached instance |
| 96 | + // will be retrieved, already carries options). |
| 97 | + $childAnonymizer = $this->getMultipleAnonymizer($part->id, null, $part->delta); |
| 98 | + // If the anonymizer is used more than once, addJoinToQuery() |
| 99 | + // method will not add as many join as there are tables, it |
| 100 | + // prevents duplicates. |
| 101 | + $joinAlias = $childAnonymizer->addJoinToQuery($update); |
| 102 | + $expressions[] = $expr->column($part->column, $joinAlias); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return $expr->concat(...$expressions); |
| 107 | + } |
| 108 | + |
| 109 | + #[\Override] |
| 110 | + public function clean(): void |
| 111 | + { |
| 112 | + try { |
| 113 | + // Clean all child anonymizers. |
| 114 | + foreach ($this->childAnonymizers as $anonymizer) { |
| 115 | + \assert($anonymizer instanceof AbstractAnonymizer); |
| 116 | + $anonymizer->clean(); |
| 117 | + } |
| 118 | + $this->childAnonymizers = []; |
| 119 | + } finally { |
| 120 | + parent::clean(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Create child anonymizer and ensures it's a single column anonymizer. |
| 126 | + */ |
| 127 | + private function getSingleAnonymizer(string $anonymizer, ?Options $options = null, int $delta = 0): AbstractSingleColumnAnonymizer |
| 128 | + { |
| 129 | + $key = \sprintf("%s[%d]", $anonymizer, $delta); |
| 130 | + |
| 131 | + $ret = $this->childAnonymizers[$key] ??= $this->getAnonymizer($anonymizer, $options); |
| 132 | + |
| 133 | + if (!$ret instanceof AbstractSingleColumnAnonymizer) { |
| 134 | + // @todo better message |
| 135 | + throw new ConfigurationException("Not a single column anonymizer"); |
| 136 | + } |
| 137 | + |
| 138 | + return $ret; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Create child anonymizer and ensures it's a multiple column anonymizer. |
| 143 | + */ |
| 144 | + private function getMultipleAnonymizer(string $anonymizer, ?Options $options = null, int $delta = 0): AbstractMultipleColumnAnonymizer |
| 145 | + { |
| 146 | + $key = \sprintf("%s[%d]", $anonymizer, $delta); |
| 147 | + |
| 148 | + $ret = $this->childAnonymizers[$key] ??= $this->getAnonymizer($anonymizer, $options); |
| 149 | + |
| 150 | + if (!$ret instanceof AbstractMultipleColumnAnonymizer) { |
| 151 | + // @todo better message |
| 152 | + throw new ConfigurationException("Not a multiple column anonymizer"); |
| 153 | + } |
| 154 | + |
| 155 | + return $ret; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Create child anonymizer. |
| 160 | + */ |
| 161 | + private function getAnonymizer(string $anonymizer, ?Options $options = null): AbstractAnonymizer |
| 162 | + { |
| 163 | + $config = new AnonymizerConfig($this->tableName, $this->columnName, $anonymizer, new Options()); |
| 164 | + |
| 165 | + return $this |
| 166 | + ->getAnonymizerRegistry() |
| 167 | + ->createAnonymizer( |
| 168 | + $anonymizer, |
| 169 | + $config, |
| 170 | + $options ?? new Options(), |
| 171 | + $this->databaseSession |
| 172 | + ) |
| 173 | + ; |
| 174 | + } |
| 175 | +} |
0 commit comments