Skip to content

Commit 55f4c94

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 55f4c94

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
2323
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
2424
use ApiPlatform\Serializer\Filter\FilterInterface as SerializerFilterInterface;
25-
use ApiPlatform\State\OptionsInterface;
2625
use Psr\Container\ContainerInterface;
2726
use Symfony\Component\Validator\Constraints\Choice;
2827
use Symfony\Component\Validator\Constraints\Count;
@@ -55,16 +54,7 @@ public function create(string $resourceClass): ResourceMetadataCollection
5554
$resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
5655

5756
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-
}
67-
57+
$resourceClass = $resource->getClassFromStateOptions();
6858
$operations = $resource->getOperations();
6959

7060
$internalPriority = -1;

0 commit comments

Comments
 (0)