Skip to content

Commit 833f4fb

Browse files
committed
chore: add custom audit logger per entity ( SummitMemberSchedule )
1 parent e656ec5 commit 833f4fb

File tree

4 files changed

+88
-6
lines changed

4 files changed

+88
-6
lines changed

app/Audit/AuditEventListener.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@ class AuditEventListener
2626

2727
public function onFlush(OnFlushEventArgs $eventArgs): void
2828
{
29+
if (app()->environment('testing')){
30+
return;
31+
}
2932
$em = $eventArgs->getObjectManager();
3033
$uow = $em->getUnitOfWork();
3134
// Strategy selection based on environment configuration
3235
$strategy = $this->getAuditStrategy($em);
33-
$ctx = $this->buildAuditContext();
3436
if (!$strategy) {
3537
return; // No audit strategy enabled
3638
}
3739

40+
$ctx = $this->buildAuditContext();
41+
3842
try {
3943
foreach ($uow->getScheduledEntityInsertions() as $entity) {
4044
$strategy->audit($entity, [], IAuditStrategy::EVENT_ENTITY_CREATION, $ctx);

app/Audit/AuditLogFormatterFactory.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@
2121
class AuditLogFormatterFactory implements IAuditLogFormatterFactory
2222
{
2323

24+
private array $config;
25+
26+
public function __construct()
27+
{
28+
// cache the config so we don't hit config() repeatedly
29+
$this->config = config('audit_log', []);
30+
}
31+
32+
public function getStrategyClass(object $subject, string $event_type): ?IAuditLogFormatter
33+
{
34+
$class = get_class($subject);
35+
$cls = $this->config['entities'][$class]['strategy'] ?? null;
36+
return !is_null($cls) ? new $cls($event_type):null;
37+
}
38+
2439
public function make($subject, $eventType): ?IAuditLogFormatter
2540
{
2641
$formatter = null;
@@ -37,15 +52,24 @@ public function make($subject, $eventType): ?IAuditLogFormatter
3752
$formatter = new EntityCollectionUpdateAuditLogFormatter($child_entity_formatter);
3853
break;
3954
case IAuditStrategy::EVENT_ENTITY_CREATION:
40-
$formatter = new EntityCreationAuditLogFormatter();
55+
$formatter = $this->getStrategyClass($subject, $eventType);
56+
if(is_null($formatter)) {
57+
$formatter = new EntityCreationAuditLogFormatter();
58+
}
4159
break;
4260
case IAuditStrategy::EVENT_ENTITY_DELETION:
43-
$child_entity_formatter = ChildEntityFormatterFactory::build($subject);
44-
$formatter = new EntityDeletionAuditLogFormatter($child_entity_formatter);
61+
$formatter = $this->getStrategyClass($subject, $eventType);
62+
if(is_null($formatter)) {
63+
$child_entity_formatter = ChildEntityFormatterFactory::build($subject);
64+
$formatter = new EntityDeletionAuditLogFormatter($child_entity_formatter);
65+
}
4566
break;
4667
case IAuditStrategy::EVENT_ENTITY_UPDATE:
47-
$child_entity_formatter = ChildEntityFormatterFactory::build($subject);
48-
$formatter = new EntityUpdateAuditLogFormatter($child_entity_formatter);
68+
$formatter = $this->getStrategyClass($subject, $eventType);
69+
if(is_null($formatter)) {
70+
$child_entity_formatter = ChildEntityFormatterFactory::build($subject);
71+
$formatter = new EntityUpdateAuditLogFormatter($child_entity_formatter);
72+
}
4973
break;
5074
}
5175
return $formatter;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php namespace App\Audit\ConcreteFormatters;
2+
/**
3+
* Copyright 2025 OpenStack Foundation
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
**/
14+
use App\Audit\IAuditLogFormatter;
15+
use App\Audit\Interfaces\IAuditStrategy;
16+
use models\main\SummitMemberSchedule;
17+
18+
class SummitMemberScheduleAuditLogFormatter implements IAuditLogFormatter
19+
{
20+
21+
private string $event_type;
22+
public function __construct(string $event_type)
23+
{
24+
$this->event_type = $event_type;
25+
}
26+
27+
/**
28+
* @param $subject
29+
* @param array $change_set
30+
* @return string|null
31+
*/
32+
public function format($subject, array $change_set): ?string
33+
{
34+
if(!$subject instanceof SummitMemberSchedule) return null;
35+
switch($this->event_type) {
36+
case IAuditStrategy::EVENT_ENTITY_DELETION:{
37+
return "Activity was removed from user custom schedule.";
38+
}
39+
case IAuditStrategy::EVENT_ENTITY_CREATION:{
40+
return sprintf("Activity %s (%s) was inserted onto user custom schedule.", $subject->getEvent()->getTitle(), $subject->getEvent()->getId());
41+
}
42+
}
43+
}
44+
}

config/audit_log.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return [
4+
'entities' => [
5+
\models\main\SummitMemberSchedule::class => [
6+
'enabled' => true,
7+
'strategy' => \App\Audit\ConcreteFormatters\SummitMemberScheduleAuditLogFormatter::class,
8+
]
9+
]
10+
];

0 commit comments

Comments
 (0)