Anonymizer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 54
rs 10
c 1
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B anonymize() 0 44 8
1
<?php
2
3
namespace WebnetFr\DatabaseAnonymizer;
4
5
use Doctrine\DBAL\Connection;
6
use WebnetFr\DatabaseAnonymizer\Exception\InvalidAnonymousValueException;
7
8
/**
9
 * Database anonymizer.
10
 *
11
 * @author Vlad Riabchenko <[email protected]>
12
 */
13
class Anonymizer
14
{
15
    /**
16
     * Anonymize entire database based on target tables.
17
     *
18
     * @param Connection $connection
19
     * @param TargetTable[] $targets
20
     *
21
     * @throws \Doctrine\DBAL\DBALException
22
     */
23
    public function anonymize(Connection $connection, array $targets)
24
    {
25
        foreach ($targets as $targetTable) {
26
            if ($targetTable->isTruncate()) {
27
                $dbPlatform = $connection->getDatabasePlatform();
28
                $connection->query('SET FOREIGN_KEY_CHECKS=0');
29
                $truncateQuery = $dbPlatform->getTruncateTableSql($targetTable->getName());
30
                $connection->executeUpdate($truncateQuery);
31
                $connection->query('SET FOREIGN_KEY_CHECKS=1');
32
            } else {
33
                $allFieldNames = $targetTable->getAllFieldNames();
34
                $pk = $targetTable->getPrimaryKey();
35
36
                // Select all rows form current table:
37
                // SELECT <all target fields> FROM <target table>
38
                $fetchRowsSQL = $connection->createQueryBuilder()
39
                    ->select(implode(',', $allFieldNames))
40
                    ->from($targetTable->getName())
41
                    ->getSQL()
42
                ;
43
                $fetchRowsStmt = $connection->prepare($fetchRowsSQL);
44
                $fetchRowsStmt->execute();
45
46
                // Anonymize all rows in current target table.
47
                while ($row = $fetchRowsStmt->fetch()) {
48
                    $values = [];
49
                    // Anonymize all target fields in current row.
50
                    foreach ($targetTable->getTargetFields() as $targetField) {
51
                        $anonValue = $targetField->generate();
52
53
                        if (null !== $anonValue && !\is_string($anonValue)) {
54
                            throw new InvalidAnonymousValueException('Generated value must be null or string');
55
                        }
56
57
                        // Set anonymized value.
58
                        $values[$targetField->getName()] = $anonValue;
59
                    }
60
61
                    $pkValues = [];
62
                    foreach ($pk as $pkField) {
63
                        $pkValues[$pkField] = $row[$pkField];
64
                    }
65
66
                    $connection->update($targetTable->getName(), $values, $pkValues);
67
                }
68
            }
69
        }
70
    }
71
}
72