Skip to content

Commit 7f2128d

Browse files
committed
refactor(metadata): simplify state options handling in metadata factory
Replaced state options logic in `ParameterResourceMetadataCollectionFactory` with a new method `getClassFromStateOptions`. This simplifies the code by centralizing the logic in the `Metadata` class and improves readability.
1 parent 0affd52 commit 7f2128d

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/Metadata/Metadata.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
namespace ApiPlatform\Metadata;
1515

16+
use ApiPlatform\Doctrine\Odm\State\Options as DoctrineOdmOptions;
17+
use ApiPlatform\Doctrine\Orm\State\Options as DoctrineOrmOptions;
1618
use ApiPlatform\State\OptionsInterface;
1719

1820
/**
@@ -617,4 +619,21 @@ public function withExtraProperties(array $extraProperties = []): static
617619

618620
return $self;
619621
}
622+
623+
public function getClassFromStateOptions(): ?string
624+
{
625+
$stateOptions = $this->getStateOptions();
626+
627+
if ($stateOptions instanceof OptionsInterface) {
628+
if ($stateOptions instanceof DoctrineOrmOptions) {
629+
return $stateOptions->getEntityClass();
630+
}
631+
632+
if ($stateOptions instanceof DoctrineOdmOptions) {
633+
return $stateOptions->getDocumentClass();
634+
}
635+
}
636+
637+
return $this->getClass();
638+
}
620639
}

src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,7 @@ public function create(string $resourceClass): ResourceMetadataCollection
5555
$resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
5656

5757
foreach ($resourceMetadataCollection as $i => $resource) {
58-
$stateOptions = $resource->getStateOptions();
59-
if ($stateOptions instanceof OptionsInterface) {
60-
if ($stateOptions instanceof \ApiPlatform\Doctrine\Orm\State\Options) {
61-
$resourceClass = $stateOptions->getEntityClass();
62-
}
63-
if ($stateOptions instanceof \ApiPlatform\Doctrine\Odm\State\Options) {
64-
$resourceClass = $stateOptions->getDocumentClass();
65-
}
66-
}
58+
$resourceClass = $resource->getClassFromStateOptions();
6759

6860
$operations = $resource->getOperations();
6961

0 commit comments

Comments
 (0)