|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Packagist. |
| 5 | + * |
| 6 | + * (c) Jordi Boggiano <[email protected]> |
| 7 | + * Nils Adermann <[email protected]> |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace App\Audit\Display; |
| 14 | + |
| 15 | +use App\Audit\AuditRecordType; |
| 16 | +use App\Entity\AuditRecord; |
| 17 | + |
| 18 | +class AuditLogDisplayFactory |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @param iterable<AuditRecord> $auditRecords |
| 22 | + * @return array<AuditLogDisplayInterface> |
| 23 | + */ |
| 24 | + public function build(iterable $auditRecords): array |
| 25 | + { |
| 26 | + $displays = []; |
| 27 | + foreach ($auditRecords as $record) { |
| 28 | + $displays[] = $this->buildSingle($record); |
| 29 | + } |
| 30 | + |
| 31 | + return $displays; |
| 32 | + } |
| 33 | + |
| 34 | + public function buildSingle(AuditRecord $record): AuditLogDisplayInterface |
| 35 | + { |
| 36 | + return match ($record->type) { |
| 37 | + AuditRecordType::PackageCreated => new PackageCreatedDisplay( |
| 38 | + $record->datetime, |
| 39 | + $record->attributes['name'], |
| 40 | + $record->attributes['repository'], |
| 41 | + $this->buildActor($record->attributes['actor']), |
| 42 | + ), |
| 43 | + AuditRecordType::PackageDeleted => new PackageDeletedDisplay( |
| 44 | + $record->datetime, |
| 45 | + $record->attributes['name'], |
| 46 | + $record->attributes['repository'], |
| 47 | + $this->buildActor($record->attributes['actor']), |
| 48 | + ), |
| 49 | + AuditRecordType::CanonicalUrlChanged => new CanonicalUrlChangedDisplay( |
| 50 | + $record->datetime, |
| 51 | + $record->attributes['name'], |
| 52 | + $record->attributes['repository_from'], |
| 53 | + $record->attributes['repository_to'], |
| 54 | + $this->buildActor($record->attributes['actor']), |
| 55 | + ), |
| 56 | + AuditRecordType::VersionDeleted => new VersionDeletedDisplay( |
| 57 | + $record->datetime, |
| 58 | + $record->attributes['name'], |
| 59 | + $record->attributes['version'], |
| 60 | + $this->buildActor($record->attributes['actor']), |
| 61 | + ), |
| 62 | + AuditRecordType::VersionReferenceChanged => new VersionReferenceChangedDisplay( |
| 63 | + $record->datetime, |
| 64 | + $record->attributes['name'], |
| 65 | + $record->attributes['version'], |
| 66 | + $record->attributes['source_from'] ?? null, |
| 67 | + $record->attributes['source_to'] ?? null, |
| 68 | + $record->attributes['dist_from'] ?? null, |
| 69 | + $record->attributes['dist_to'] ?? null, |
| 70 | + $this->buildActor($record->attributes['actor'] ?? null), |
| 71 | + ), |
| 72 | + default => throw new \LogicException(sprintf('Unsupported audit record type: %s', $record->type->value)), |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param array{id: int, username: string}|string|null $actor |
| 78 | + */ |
| 79 | + private function buildActor(array|string|null $actor): ActorDisplay |
| 80 | + { |
| 81 | + if ($actor === null) { |
| 82 | + return new ActorDisplay(null, 'unknown'); |
| 83 | + } |
| 84 | + |
| 85 | + if (is_string($actor)) { |
| 86 | + return new ActorDisplay(null, $actor); |
| 87 | + } |
| 88 | + |
| 89 | + return new ActorDisplay($actor['id'], $actor['username']); |
| 90 | + } |
| 91 | +} |
0 commit comments