|
| 1 | +<?php namespace App\Audit; |
| 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\ConcreteFormatters\ChildEntityFormatters\ChildEntityFormatterFactory; |
| 15 | +use App\Audit\ConcreteFormatters\EntityCollectionUpdateAuditLogFormatter; |
| 16 | +use App\Audit\ConcreteFormatters\EntityCreationAuditLogFormatter; |
| 17 | +use App\Audit\ConcreteFormatters\EntityDeletionAuditLogFormatter; |
| 18 | +use App\Audit\ConcreteFormatters\EntityUpdateAuditLogFormatter; |
| 19 | +use App\Audit\Interfaces\IAuditStrategy; |
| 20 | +use Doctrine\ORM\PersistentCollection; |
| 21 | +use Illuminate\Support\Facades\Log; |
| 22 | +use Doctrine\ORM\Mapping\ClassMetadata; |
| 23 | +class AuditLogFormatterFactory implements IAuditLogFormatterFactory |
| 24 | +{ |
| 25 | + |
| 26 | + private array $config; |
| 27 | + |
| 28 | + public function __construct() |
| 29 | + { |
| 30 | + // cache the config so we don't hit config() repeatedly |
| 31 | + $this->config = config('audit_log', []); |
| 32 | + } |
| 33 | + |
| 34 | + public function make(AuditContext $ctx, $subject, $eventType): ?IAuditLogFormatter |
| 35 | + { |
| 36 | + $formatter = null; |
| 37 | + switch ($eventType) { |
| 38 | + case IAuditStrategy::EVENT_COLLECTION_UPDATE: |
| 39 | + $child_entity_formatter = null; |
| 40 | + |
| 41 | + if ($subject instanceof PersistentCollection) { |
| 42 | + $targetEntity = null; |
| 43 | + Log::debug |
| 44 | + ( |
| 45 | + sprintf |
| 46 | + ( |
| 47 | + "AuditLogFormatterFactory::make subject is a PersistentCollection isInitialized %b ?", |
| 48 | + $subject->isInitialized() |
| 49 | + ) |
| 50 | + ); |
| 51 | + if (method_exists($subject, 'getTypeClass')) { |
| 52 | + $type = $subject->getTypeClass(); |
| 53 | + // Your log shows this is ClassMetadata |
| 54 | + if ($type instanceof ClassMetadata) { |
| 55 | + // Doctrine supports either getName() or public $name |
| 56 | + $targetEntity = method_exists($type, 'getName') ? $type->getName() : ($type->name ?? null); |
| 57 | + } elseif (is_string($type)) { |
| 58 | + $targetEntity = $type; |
| 59 | + } |
| 60 | + Log::debug("AuditLogFormatterFactory::make getTypeClass targetEntity {$targetEntity}"); |
| 61 | + } |
| 62 | + elseif (method_exists($subject, 'getMapping')) { |
| 63 | + $mapping = $subject->getMapping(); |
| 64 | + $targetEntity = $mapping['targetEntity'] ?? null; |
| 65 | + Log::debug("AuditLogFormatterFactory::make getMapping targetEntity {$targetEntity}"); |
| 66 | + } else { |
| 67 | + // last-resort: read private association metadata (still no hydration) |
| 68 | + $ref = new \ReflectionObject($subject); |
| 69 | + foreach (['association', 'mapping', 'associationMapping'] as $propName) { |
| 70 | + if ($ref->hasProperty($propName)) { |
| 71 | + $prop = $ref->getProperty($propName); |
| 72 | + $prop->setAccessible(true); |
| 73 | + $mapping = $prop->getValue($subject); |
| 74 | + $targetEntity = $mapping['targetEntity'] ?? null; |
| 75 | + if ($targetEntity) break; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if ($targetEntity) { |
| 81 | + // IMPORTANT: build formatter WITHOUT touching collection items |
| 82 | + $child_entity_formatter = ChildEntityFormatterFactory::build($targetEntity); |
| 83 | + } |
| 84 | + Log::debug |
| 85 | + ( |
| 86 | + sprintf |
| 87 | + ( |
| 88 | + "AuditLogFormatterFactory::make subject is a PersistentCollection isInitialized %b ? ( final )", |
| 89 | + $subject->isInitialized() |
| 90 | + ) |
| 91 | + ); |
| 92 | + } elseif (is_array($subject)) { |
| 93 | + $child_entity = $subject[0] ?? null; |
| 94 | + $child_entity_formatter = $child_entity ? ChildEntityFormatterFactory::build($child_entity) : null; |
| 95 | + } elseif (is_object($subject) && method_exists($subject, 'getSnapshot')) { |
| 96 | + $snap = $subject->getSnapshot(); // only once |
| 97 | + $child_entity = $snap[0] ?? null; |
| 98 | + $child_entity_formatter = $child_entity ? ChildEntityFormatterFactory::build($child_entity) : null; |
| 99 | + } |
| 100 | + |
| 101 | + $formatter = new EntityCollectionUpdateAuditLogFormatter($child_entity_formatter); |
| 102 | + break; |
| 103 | + case IAuditStrategy::EVENT_ENTITY_CREATION: |
| 104 | + $formatter = $this->getFormatterByContext($subject, $eventType, $ctx); |
| 105 | + if(is_null($formatter)) { |
| 106 | + $formatter = new EntityCreationAuditLogFormatter(); |
| 107 | + } |
| 108 | + break; |
| 109 | + case IAuditStrategy::EVENT_ENTITY_DELETION: |
| 110 | + $formatter = $this->getFormatterByContext($subject, $eventType, $ctx); |
| 111 | + if(is_null($formatter)) { |
| 112 | + $child_entity_formatter = ChildEntityFormatterFactory::build($subject); |
| 113 | + $formatter = new EntityDeletionAuditLogFormatter($child_entity_formatter); |
| 114 | + } |
| 115 | + break; |
| 116 | + case IAuditStrategy::EVENT_ENTITY_UPDATE: |
| 117 | + $formatter = $this->getFormatterByContext($subject, $eventType, $ctx); |
| 118 | + if(is_null($formatter)) { |
| 119 | + $child_entity_formatter = ChildEntityFormatterFactory::build($subject); |
| 120 | + $formatter = new EntityUpdateAuditLogFormatter($child_entity_formatter); |
| 121 | + } |
| 122 | + break; |
| 123 | + } |
| 124 | + if ($formatter === null) return null; |
| 125 | + $formatter->setContext($ctx); |
| 126 | + return $formatter; |
| 127 | + } |
| 128 | + |
| 129 | + private function getFormatterByContext(object $subject, string $event_type, AuditContext $ctx): ?IAuditLogFormatter |
| 130 | + { |
| 131 | + $class = get_class($subject); |
| 132 | + $entity_config = $this->config['entities'][$class] ?? null; |
| 133 | + |
| 134 | + if (!$entity_config) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + if (isset($entity_config['strategies'])) { |
| 139 | + foreach ($entity_config['strategies'] as $strategy) { |
| 140 | + if (!$this->matchesStrategy($strategy, $ctx)) { |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + $formatter_class = $strategy['formatter'] ?? null; |
| 145 | + return $formatter_class ? new $formatter_class($event_type) : null; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if (isset($entity_config['strategy'])) { |
| 150 | + $strategy_class = $entity_config['strategy']; |
| 151 | + return new $strategy_class($event_type); |
| 152 | + } |
| 153 | + |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + private function matchesStrategy(array $strategy, AuditContext $ctx): bool |
| 158 | + { |
| 159 | + if (isset($strategy['route']) && !$this->routeMatches($strategy['route'], $ctx->rawRoute)) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + private function routeMatches(string $route, string $actual_route): bool |
| 167 | + { |
| 168 | + return strcmp($actual_route, $route) === 0; |
| 169 | + } |
| 170 | +} |
0 commit comments