Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| AddAuditLog | |
100.00% |
13 / 13 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSubscribedEvents | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onApieResourceCreated | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\Common\Events; |
| 3 | |
| 4 | use Apie\Common\Other\AuditLog; |
| 5 | use Apie\Core\Attributes\Auditable; |
| 6 | use Apie\Core\BoundedContext\BoundedContextId; |
| 7 | use Apie\Core\ContextConstants; |
| 8 | use Apie\Core\Datalayers\ApieDatalayer; |
| 9 | use Apie\Core\ValueObjects\EntityReference; |
| 10 | use Apie\Core\ValueObjects\IdFriendlyEntityReference; |
| 11 | use Apie\Serializer\PropertySerializer\PropertySerializer; |
| 12 | use ReflectionClass; |
| 13 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 14 | |
| 15 | class AddAuditLog implements EventSubscriberInterface |
| 16 | { |
| 17 | public function __construct( |
| 18 | private readonly ApieDatalayer $datalayer, |
| 19 | private readonly PropertySerializer $propertySerializer, |
| 20 | ) { |
| 21 | } |
| 22 | |
| 23 | public static function getSubscribedEvents(): array |
| 24 | { |
| 25 | return [ApieResourceCreated::class => 'onApieResourceCreated']; |
| 26 | } |
| 27 | |
| 28 | public function onApieResourceCreated(ApieResourceCreated $event): void |
| 29 | { |
| 30 | foreach ((new ReflectionClass($event->resource))->getAttributes(Auditable::class) as $auditable) { |
| 31 | $reference = IdFriendlyEntityReference::createFromContext($event->context); |
| 32 | |
| 33 | if ($reference instanceof IdFriendlyEntityReference) { |
| 34 | $auditLog = new AuditLog( |
| 35 | $reference, |
| 36 | $this->propertySerializer->toJson($event->resource) |
| 37 | ); |
| 38 | $this->datalayer->persistNew( |
| 39 | $auditLog, |
| 40 | new BoundedContextId($event->context->getContext(ContextConstants::BOUNDED_CONTEXT_ID)) |
| 41 | ); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |