Anonymiser   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 10
eloc 55
c 3
b 0
f 1
dl 0
loc 111
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A AnonymiseTable() 0 10 2
A AnonymiseTableField() 0 20 5
A AnonymisePresets() 0 6 2
A setDatabaseActions() 0 3 1
1
<?php
2
3
namespace Sunnysideup\DatabaseShareCleanUp\Api;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Extensible;
7
use SilverStripe\Core\Injector\Injectable;
8
9
class Anonymiser
10
{
11
    use Injectable;
12
    use Configurable;
13
    use Extensible;
14
15
    public $databaseActions;
16
17
    protected $databaseAction;
18
19
    /**
20
     * @var array
21
     */
22
    private static $fields_to_anonymise = [
23
        'Email',
24
        'Username',
25
        'City',
26
        'Suburb',
27
        'Address',
28
        'Street',
29
        'Company',
30
        'CompanyName',
31
        'Address2',
32
        'Phone',
33
        'Mobile',
34
        'FirstName',
35
        'Surname',
36
        'LastName',
37
        'Fax',
38
        'Dob',
39
        'DOB',
40
        'DateOfBirth',
41
        'Ip',
42
        'IP',
43
        'IpAddress',
44
        'Proxy',
45
    ];
46
47
    /**
48
     * @var array
49
     */
50
    private static $tables_to_remove = [
51
        'MemberPassword',
52
        'LoginAttempt',
53
    ];
54
55
    /**
56
     * @var array
57
     */
58
    private static $tables_to_keep = [];
59
60
    /**
61
     * specify tables with fields that are not to be deleted
62
     * e.g.
63
     *    MyTable.MyField,
64
     *    MyTable2.MyField2,.
65
     *
66
     * @var array
67
     */
68
    private static $keep_table_field_combos = [];
69
70
    /**
71
     * @var array
72
     */
73
    private static $also_remove_table_field_combos = [];
74
75
    public function setDatabaseActions($databaseActions)
76
    {
77
        $this->databaseActions = $databaseActions;
78
    }
79
80
    public function AnonymiseTable(string $tableName): bool
81
    {
82
        $tables = $this->Config()->get('tables_to_remove');
83
        if (in_array($tableName, $tables, true)) {
84
            $this->databaseActions->truncateTable($tableName);
85
86
            return true;
87
        }
88
89
        return false;
90
    }
91
92
    public function AnonymiseTableField(string $tableName, string $fieldName): bool
93
    {
94
        if (in_array($tableName, $this->Config()->get('tables_to_keep'), true)) {
95
            return false;
96
        }
97
        $keepCombos = $this->Config()->get('keep_table_field_combos');
98
99
        // $combo = $tableName . '.' . $fieldName;
100
        if (in_array($fieldName, $keepCombos, true)) {
101
            return false;
102
        }
103
104
        $fieldsToDelete = $this->Config()->get('fields_to_anonymise');
105
        foreach ($fieldsToDelete as $fieldTest) {
106
            if (false !== strpos($fieldName, $fieldTest)) {
107
                return $this->databaseActions->anonymiseField($tableName, $fieldName);
108
            }
109
        }
110
111
        return false;
112
    }
113
114
    public function AnonymisePresets()
115
    {
116
        $also = $this->Config()->get('also_remove_table_field_combos');
117
        foreach ($also as $combo) {
118
            list($tableName, $fieldName) = explode('.', $combo);
119
            $this->databaseActions->anonymiseField($tableName, $fieldName);
120
        }
121
    }
122
123
    // look for any field called Email / Phone / Address / FirstName /
124
}
125