Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.26% |
51 / 62 |
|
80.00% |
8 / 10 |
CRAP | |
0.00% |
0 / 1 |
| AuditLogEventMessage | |
82.26% |
51 / 62 |
|
80.00% |
8 / 10 |
13.94 | |
0.00% |
0 / 1 |
| getFallbackText | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
| createUnknownEvent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| createMigrationEvent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| createResourceCreatedEvent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| createResourceReplacedEvent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| createResourceMethodCalledEvent | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| createResourceRemovedEvent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| createResourceModifiedEvent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| createResourceRetrievedEvent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| createResourceRetrievedInListEvent | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Translator\ValueObjects; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\ContextConstants; |
| 6 | use Apie\Core\Identifiers\KebabCaseSlug; |
| 7 | use Apie\Core\Identifiers\SnakeCaseSlug; |
| 8 | use Apie\Core\Lists\ItemHashmap; |
| 9 | |
| 10 | final class AuditLogEventMessage extends AbstractTranslation |
| 11 | { |
| 12 | protected const MIDDLE_REGEX = 'audit_log\.(unknown_event|migration|created|replaced|method_called\.:method|migration|modified|read_list|read|removed)'; |
| 13 | |
| 14 | public function getFallbackText(): string |
| 15 | { |
| 16 | if (preg_match('/^audit_log\.method_called\.(?<method>[^.]+)($|\.)/', $this->middleSection, $matches)) { |
| 17 | return 'Called method ' . $matches['method']; |
| 18 | } |
| 19 | if (preg_match('/^audit_log\.(?<message>[^.]+)($|\.)/', $this->middleSection, $matches)) { |
| 20 | return ucfirst(SnakeCaseSlug::fromText($matches['message'])); |
| 21 | } |
| 22 | return 'Unknown event'; |
| 23 | } |
| 24 | |
| 25 | public static function createUnknownEvent(ApieContext $context): static |
| 26 | { |
| 27 | return new AuditLogEventMessage( |
| 28 | TranslationStringPrefix::fromApieContext($context), |
| 29 | 'audit_log.unknown_event', |
| 30 | new TranslationStringSuffix(), |
| 31 | new ItemHashmap() |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | public static function createMigrationEvent(ApieContext $context): static |
| 36 | { |
| 37 | return new AuditLogEventMessage( |
| 38 | TranslationStringPrefix::fromApieContext($context), |
| 39 | 'audit_log.migration', |
| 40 | new TranslationStringSuffix(), |
| 41 | new ItemHashmap() |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | public static function createResourceCreatedEvent(ApieContext $context): static |
| 46 | { |
| 47 | return new AuditLogEventMessage( |
| 48 | TranslationStringPrefix::fromApieContext($context), |
| 49 | 'audit_log.created', |
| 50 | new TranslationStringSuffix(), |
| 51 | new ItemHashmap() |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | public static function createResourceReplacedEvent(ApieContext $context): static |
| 56 | { |
| 57 | return new AuditLogEventMessage( |
| 58 | TranslationStringPrefix::fromApieContext($context), |
| 59 | 'audit_log.replaced', |
| 60 | new TranslationStringSuffix(), |
| 61 | new ItemHashmap() |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | public static function createResourceMethodCalledEvent(ApieContext $context, ?string $methodName): static |
| 66 | { |
| 67 | $methodName ??= '(unknown)'; |
| 68 | if ($context->hasContext(ContextConstants::METHOD_NAME)) { |
| 69 | $methodName = KebabCaseSlug::fromText($context->getContext(ContextConstants::METHOD_NAME))->toNative(); |
| 70 | } |
| 71 | return new AuditLogEventMessage( |
| 72 | TranslationStringPrefix::fromApieContext($context), |
| 73 | 'audit_log.method_called.' . $methodName, |
| 74 | new TranslationStringSuffix(), |
| 75 | new ItemHashmap(['method' => $methodName]) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | public static function createResourceRemovedEvent(ApieContext $context): static |
| 80 | { |
| 81 | return new AuditLogEventMessage( |
| 82 | TranslationStringPrefix::fromApieContext($context), |
| 83 | 'audit_log.removed', |
| 84 | new TranslationStringSuffix(), |
| 85 | new ItemHashmap() |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | public static function createResourceModifiedEvent(ApieContext $context): static |
| 90 | { |
| 91 | return new AuditLogEventMessage( |
| 92 | TranslationStringPrefix::fromApieContext($context), |
| 93 | 'audit_log.modified', |
| 94 | new TranslationStringSuffix(), |
| 95 | new ItemHashmap() |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | public static function createResourceRetrievedEvent(ApieContext $context): static |
| 100 | { |
| 101 | return new AuditLogEventMessage( |
| 102 | TranslationStringPrefix::fromApieContext($context), |
| 103 | 'audit_log.read', |
| 104 | new TranslationStringSuffix(), |
| 105 | new ItemHashmap() |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | public static function createResourceRetrievedInListEvent(ApieContext $context): static |
| 110 | { |
| 111 | return new AuditLogEventMessage( |
| 112 | TranslationStringPrefix::fromApieContext($context), |
| 113 | 'audit_log.read_list', |
| 114 | new TranslationStringSuffix(), |
| 115 | new ItemHashmap() |
| 116 | ); |
| 117 | } |
| 118 | } |