SchemaHelper::getAuditTableIndices()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 29
c 0
b 0
f 0
rs 9.584
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Auditor\Provider\Doctrine\Persistence\Helper;
6
7
abstract class SchemaHelper
8
{
9
    /**
10
     * Return columns of audit tables.
11
     *
12
     * @return array{id: array{type: string, options: array{autoincrement: true, unsigned: true}}, type: array{type: string, options: array{notnull: true, length: int}}, object_id: array{type: string, options: array{notnull: true}}, discriminator: array{type: string, options: array{default: null, notnull: false}}, transaction_hash: array{type: string, options: array{notnull: false, length: int}}, diffs: array{type: string, options: array{default: null, notnull: false}}, blame_id: array{type: string, options: array{default: null, notnull: false}}, blame_user: array{type: string, options: array{default: null, notnull: false, length: int}}, blame_user_fqdn: array{type: string, options: array{default: null, notnull: false, length: int}}, blame_user_firewall: array{type: string, options: array{default: null, notnull: false, length: int}}, ip: array{type: string, options: array{default: null, notnull: false, length: int}}, created_at: array{type: string, options: array{notnull: true}}}
13
     */
14
    public static function getAuditTableColumns(): array
15
    {
16
        return [
17
            'id' => [
18
                'type' => DoctrineHelper::getDoctrineType('INTEGER'),
19
                'options' => [
20
                    'autoincrement' => true,
21
                    'unsigned' => true,
22
                ],
23
            ],
24
            'type' => [
25
                'type' => DoctrineHelper::getDoctrineType('STRING'),
26
                'options' => [
27
                    'notnull' => true,
28
                    'length' => 10,
29
                ],
30
            ],
31
            'object_id' => [
32
                'type' => DoctrineHelper::getDoctrineType('STRING'),
33
                'options' => [
34
                    'notnull' => true,
35
                ],
36
            ],
37
            'discriminator' => [
38
                'type' => DoctrineHelper::getDoctrineType('STRING'),
39
                'options' => [
40
                    'default' => null,
41
                    'notnull' => false,
42
                ],
43
            ],
44
            'transaction_hash' => [
45
                'type' => DoctrineHelper::getDoctrineType('STRING'),
46
                'options' => [
47
                    'notnull' => false,
48
                    'length' => 40,
49
                ],
50
            ],
51
            'diffs' => [
52
                'type' => DoctrineHelper::getDoctrineType('JSON'),
53
                'options' => [
54
                    'default' => null,
55
                    'notnull' => false,
56
                ],
57
            ],
58
            'blame_id' => [
59
                'type' => DoctrineHelper::getDoctrineType('STRING'),
60
                'options' => [
61
                    'default' => null,
62
                    'notnull' => false,
63
                ],
64
            ],
65
            'blame_user' => [
66
                'type' => DoctrineHelper::getDoctrineType('STRING'),
67
                'options' => [
68
                    'default' => null,
69
                    'notnull' => false,
70
                    'length' => 255,
71
                ],
72
            ],
73
            'blame_user_fqdn' => [
74
                'type' => DoctrineHelper::getDoctrineType('STRING'),
75
                'options' => [
76
                    'default' => null,
77
                    'notnull' => false,
78
                    'length' => 255,
79
                ],
80
            ],
81
            'blame_user_firewall' => [
82
                'type' => DoctrineHelper::getDoctrineType('STRING'),
83
                'options' => [
84
                    'default' => null,
85
                    'notnull' => false,
86
                    'length' => 100,
87
                ],
88
            ],
89
            'ip' => [
90
                'type' => DoctrineHelper::getDoctrineType('STRING'),
91
                'options' => [
92
                    'default' => null,
93
                    'notnull' => false,
94
                    'length' => 45,
95
                ],
96
            ],
97
            'created_at' => [
98
                'type' => DoctrineHelper::getDoctrineType('DATETIME_IMMUTABLE'),
99
                'options' => [
100
                    'notnull' => true,
101
                ],
102
            ],
103
        ];
104
    }
105
106
    /**
107
     * Return indices of an audit table.
108
     *
109
     * @return array{id: array{type: string}, type: array{type: string, name: string}, object_id: array{type: string, name: string}, discriminator: array{type: string, name: string}, transaction_hash: array{type: string, name: string}, blame_id: array{type: string, name: string}, created_at: array{type: string, name: string}}
110
     */
111
    public static function getAuditTableIndices(string $tablename): array
112
    {
113
        return [
114
            'id' => [
115
                'type' => 'primary',
116
            ],
117
            'type' => [
118
                'type' => 'index',
119
                'name' => 'type_'.md5($tablename).'_idx',
120
            ],
121
            'object_id' => [
122
                'type' => 'index',
123
                'name' => 'object_id_'.md5($tablename).'_idx',
124
            ],
125
            'discriminator' => [
126
                'type' => 'index',
127
                'name' => 'discriminator_'.md5($tablename).'_idx',
128
            ],
129
            'transaction_hash' => [
130
                'type' => 'index',
131
                'name' => 'transaction_hash_'.md5($tablename).'_idx',
132
            ],
133
            'blame_id' => [
134
                'type' => 'index',
135
                'name' => 'blame_id_'.md5($tablename).'_idx',
136
            ],
137
            'created_at' => [
138
                'type' => 'index',
139
                'name' => 'created_at_'.md5($tablename).'_idx',
140
            ],
141
        ];
142
    }
143
144
    public static function isValidPayload(array $payload): bool
145
    {
146
        foreach (array_keys(self::getAuditTableColumns()) as $columnName) {
147
            if ('id' !== $columnName && !\array_key_exists($columnName, $payload)) {
148
                return false;
149
            }
150
        }
151
152
        return true;
153
    }
154
}
155