diff --git a/Exception/DiscriminatorMapException.php b/Exception/DiscriminatorMapException.php new file mode 100644 index 0000000..415d00c --- /dev/null +++ b/Exception/DiscriminatorMapException.php @@ -0,0 +1,39 @@ +getMessage(), $previous->getCode(), $previous); + } + + public static function missingProperty(string $className, string $propertyName, ?\Throwable $previous = null): self + { + $message = sprintf(self::MISSING_PARAMETER_ERROR_MESSAGE, $propertyName, $className); + + return new self($message, previous: $previous); + } + + public static function missingTypeProperty(string $className, string $typeProperty, array $mapping, ?\Throwable $previous = null): self + { + $map = HashMap::collect($mapping) + ->map(static fn (Entry $entry) => "{$entry->key}: {$entry->value}") + ->toArrayList() + ->mkString() + ; + + $message = sprintf(self::MISSING_TYPE_PROPERTY_ERROR_MESSAGE, $typeProperty, $className, $map); + + return new self($message, previous: $previous); + } +} diff --git a/Helper/ClassHelper.php b/Helper/ClassHelper.php new file mode 100644 index 0000000..7cd1974 --- /dev/null +++ b/Helper/ClassHelper.php @@ -0,0 +1,68 @@ +getTypeProperty(); + $mapping = $discriminatorMap->getMapping(); + $parameter = $targetPayload[$property] ?? throw DiscriminatorMapException::missingProperty($targetClass, $property); + $targetClass = $mapping[$parameter] ?? throw DiscriminatorMapException::missingTypeProperty($targetClass, $property, $mapping); + } + + return $targetClass; + } + + /** + * @template T + * + * @param class-string $attributeClass + * + * @return null|T + */ + public static function getAttribute(string|object $target, string $attributeClass): ?object + { + return ArrayList::collect(self::getAttributes($target, $attributeClass)) + ->firstElement() + ->get() + ; + } + + /** + * @template T + * + * @param class-string $attributeClass + * + * @return array + */ + public static function getAttributes(string|object $target, string $attributeClass): array + { + try { + $attributes = (new \ReflectionClass($target))->getAttributes($attributeClass); + } catch (\ReflectionException) { + $attributes = []; + } + + return ArrayList::collect($attributes) + ->map(static fn (\ReflectionAttribute $a) => $a->newInstance()) + ->toArray() + ; + } +} diff --git a/Integration/MessageSerializer.php b/Integration/MessageSerializer.php index 64003ae..377a1b5 100644 --- a/Integration/MessageSerializer.php +++ b/Integration/MessageSerializer.php @@ -4,6 +4,7 @@ namespace FRZB\Component\Messenger\Bridge\Kafka\Integration; +use FRZB\Component\Messenger\Bridge\Kafka\Helper\ClassHelper; use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Transport\Serialization\Serializer; @@ -26,6 +27,8 @@ private static function getBody(array $decodedEnvelope): ?string private static function getHeaders(array $decodedEnvelope): array { - return array_merge($decodedEnvelope['headers'] ?? [], ['type' => static::getMessageType()]); + $className = ClassHelper::getClassName(static::getMessageType(), self::getBody($decodedEnvelope)); + + return [...$decodedEnvelope['headers'] ?? [], ...['type' => $className]]; } }