-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractAuditLogFormatter.php
More file actions
132 lines (110 loc) · 4.04 KB
/
AbstractAuditLogFormatter.php
File metadata and controls
132 lines (110 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace App\Audit;
use App\Audit\Utils\DateFormatter;
/**
* Copyright 2025 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
abstract class AbstractAuditLogFormatter implements IAuditLogFormatter
{
protected ?AuditContext $ctx = null;
protected string $event_type;
public function __construct(string $event_type)
{
$this->event_type = $event_type;
}
final public function setContext(AuditContext $ctx): void
{
$this->ctx = $ctx;
}
protected function getUserInfo(): string
{
if (!$this->ctx) {
return 'Unknown (unknown)';
}
$user_name = 'Unknown';
if ($this->ctx->userFirstName || $this->ctx->userLastName) {
$user_name = trim(sprintf("%s %s", $this->ctx->userFirstName ?? '', $this->ctx->userLastName ?? '')) ?: 'Unknown';
} elseif ($this->ctx->userEmail) {
$user_name = $this->ctx->userEmail;
}
$user_id = $this->ctx->userId ?? 'unknown';
return sprintf("%s (%s)", $user_name, $user_id);
}
protected function formatAuditDate($date, string $format = 'Y-m-d H:i:s'): string
{
return DateFormatter::format($date, $format);
}
protected function getIgnoredFields(): array
{
return [
'last_created',
'last_updated',
'last_edited',
'created_by',
'updated_by',
'created_at',
'updated_at',
];
}
protected function formatChangeValue($value): string
{
if (is_bool($value)) {
return $value ? 'true' : 'false';
}
if (is_null($value)) {
return 'null';
}
if ($value instanceof \DateTimeInterface) {
return $value->format('Y-m-d H:i:s');
}
if ($value instanceof \Doctrine\Common\Collections\Collection) {
$count = $value->count();
return sprintf('Collection[%d items]', $count);
}
if (is_object($value)) {
$className = get_class($value);
return sprintf('%s', $className);
}
if (is_array($value)) {
return sprintf('Array[%d items]', count($value));
}
return (string) $value;
}
protected function buildChangeDetails(array $change_set): string
{
$changed_fields = [];
$ignored_fields = $this->getIgnoredFields();
foreach ($change_set as $prop_name => $change_values) {
if (in_array($prop_name, $ignored_fields)) {
continue;
}
$old_value = $change_values[0] ?? null;
$new_value = $change_values[1] ?? null;
$formatted_change = $this->formatFieldChange($prop_name, $old_value, $new_value);
if ($formatted_change !== null) {
$changed_fields[] = $formatted_change;
}
}
if (empty($changed_fields)) {
return 'properties without changes registered';
}
$fields_summary = count($changed_fields) . ' field(s) modified: ';
return $fields_summary . implode(' | ', $changed_fields);
}
protected function formatFieldChange(string $prop_name, $old_value, $new_value): ?string
{
$old_display = $this->formatChangeValue($old_value);
$new_display = $this->formatChangeValue($new_value);
return sprintf("Property \"%s\" has changed from \"%s\" to \"%s\"", $prop_name, $old_display, $new_display);
}
abstract public function format(mixed $subject, array $change_set): ?string;
}