2121class 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 ;
0 commit comments