• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 13175309672

06 Feb 2025 09:04AM UTC coverage: 7.663% (-0.2%) from 7.841%
13175309672

push

github

web-flow
fix: ensure template files have a tpl file extension (#6826) (#6829)

2 of 5 new or added lines in 4 files covered. (40.0%)

3676 existing lines in 122 files now uncovered.

13073 of 170593 relevant lines covered (7.66%)

27.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.63
/src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Metadata\Resource\Factory;
15

16
use ApiPlatform\Doctrine\Odm\State\Options as DoctrineODMOptions;
17
use ApiPlatform\Doctrine\Orm\State\Options as DoctrineORMOptions;
18
use ApiPlatform\Metadata\ApiProperty;
19
use ApiPlatform\Metadata\FilterInterface;
20
use ApiPlatform\Metadata\JsonSchemaFilterInterface;
21
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
22
use ApiPlatform\Metadata\Operation;
23
use ApiPlatform\Metadata\Parameter;
24
use ApiPlatform\Metadata\ParameterProviderFilterInterface;
25
use ApiPlatform\Metadata\Parameters;
26
use ApiPlatform\Metadata\PropertiesAwareInterface;
27
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
28
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
29
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
30
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
31
use ApiPlatform\Serializer\Filter\FilterInterface as SerializerFilterInterface;
32
use Psr\Container\ContainerInterface;
33
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
34

35
/**
36
 * Prepares Parameters documentation by reading its filter details and declaring an OpenApi parameter.
37
 *
38
 * @experimental
39
 */
40
final class ParameterResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
41
{
42
    private array $localPropertyCache;
43

44
    public function __construct(
45
        private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory,
46
        private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory,
47
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
48
        private readonly ?ContainerInterface $filterLocator = null,
49
        private readonly ?NameConverterInterface $nameConverter = null,
50
    ) {
51
    }
2,729✔
52

53
    public function create(string $resourceClass): ResourceMetadataCollection
54
    {
55
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
174✔
56

57
        foreach ($resourceMetadataCollection as $i => $resource) {
174✔
58
            $operations = $resource->getOperations();
163✔
59

60
            $internalPriority = -1;
163✔
61
            foreach ($operations as $operationName => $operation) {
163✔
62
                $parameters = $this->getDefaultParameters($operation, $resourceClass, $internalPriority);
163✔
63
                if (\count($parameters) > 0) {
163✔
64
                    $operations->add($operationName, $operation->withParameters($parameters));
22✔
65
                }
66
            }
67

68
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
163✔
69

70
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
163✔
71
                continue;
80✔
72
            }
73

74
            $internalPriority = -1;
138✔
75
            foreach ($graphQlOperations as $operationName => $operation) {
138✔
76
                $parameters = $this->getDefaultParameters($operation, $resourceClass, $internalPriority);
138✔
77
                if (\count($parameters) > 0) {
138✔
78
                    $graphQlOperations[$operationName] = $operation->withParameters($parameters);
6✔
79
                }
80
            }
81

82
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
138✔
83
        }
84

85
        return $resourceMetadataCollection;
174✔
86
    }
87

88
    /**
89
     * @return array{propertyNames: string[], properties: array<string, ApiProperty>}
90
     */
91
    private function getProperties(string $resourceClass): array
92
    {
93
        if (isset($this->localPropertyCache[$resourceClass])) {
163✔
94
            return $this->localPropertyCache[$resourceClass];
141✔
95
        }
96

97
        $propertyNames = [];
163✔
98
        $properties = [];
163✔
99
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
163✔
100
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property);
148✔
101
            if ($propertyMetadata->isReadable()) {
148✔
102
                $propertyNames[] = $property;
148✔
103
                $properties[$property] = $propertyMetadata;
148✔
104
            }
105
        }
106

107
        $this->localPropertyCache = [$resourceClass => ['propertyNames' => $propertyNames, 'properties' => $properties]];
163✔
108

109
        return $this->localPropertyCache[$resourceClass];
163✔
110
    }
111

112
    private function getDefaultParameters(Operation $operation, string $resourceClass, int &$internalPriority): Parameters
113
    {
114
        ['propertyNames' => $propertyNames, 'properties' => $properties] = $this->getProperties($resourceClass);
163✔
115
        $parameters = $operation->getParameters() ?? new Parameters();
163✔
116
        foreach ($parameters as $key => $parameter) {
163✔
117
            if (':property' === $key) {
22✔
118
                foreach ($propertyNames as $property) {
6✔
119
                    $converted = $this->nameConverter?->denormalize($property) ?? $property;
6✔
120
                    $propertyParameter = $this->setDefaults($converted, $parameter, $resourceClass, $properties, $operation);
6✔
121
                    $priority = $propertyParameter->getPriority() ?? $internalPriority--;
6✔
122
                    $parameters->add($converted, $propertyParameter->withPriority($priority)->withKey($converted));
6✔
123
                }
124

125
                $parameters->remove($key, $parameter::class);
6✔
126
                continue;
6✔
127
            }
128

129
            $key = $parameter->getKey() ?? $key;
22✔
130

131
            if (str_contains($key, ':property') || (($f = $parameter->getFilter()) && is_a($f, PropertiesAwareInterface::class, true)) || $parameter instanceof PropertiesAwareInterface) {
22✔
132
                $p = [];
9✔
133
                foreach ($propertyNames as $prop) {
9✔
134
                    $p[$this->nameConverter?->denormalize($prop) ?? $prop] = $prop;
6✔
135
                }
136

137
                $parameter = $parameter->withExtraProperties($parameter->getExtraProperties() + ['_properties' => $p]);
9✔
138
            }
139

140
            $parameter = $this->setDefaults($key, $parameter, $resourceClass, $properties, $operation);
22✔
141
            $priority = $parameter->getPriority() ?? $internalPriority--;
22✔
142
            $parameters->add($key, $parameter->withPriority($priority));
22✔
143
        }
144

145
        return $parameters;
163✔
146
    }
147

148
    private function addFilterMetadata(Parameter $parameter): Parameter
149
    {
150
        if (!($filterId = $parameter->getFilter())) {
22✔
151
            return $parameter;
18✔
152
        }
153

154
        if (!\is_object($filterId) && !$this->filterLocator->has($filterId)) {
13✔
UNCOV
155
            return $parameter;
×
156
        }
157

158
        $filter = \is_object($filterId) ? $filterId : $this->filterLocator->get($filterId);
13✔
159

160
        if ($filter instanceof ParameterProviderFilterInterface) {
13✔
UNCOV
161
            $parameter = $parameter->withProvider($filter::getParameterProvider());
×
162
        }
163

164
        if (!$filter) {
13✔
UNCOV
165
            return $parameter;
×
166
        }
167

168
        if (null === $parameter->getSchema() && $filter instanceof JsonSchemaFilterInterface && $schema = $filter->getSchema($parameter)) {
13✔
169
            $parameter = $parameter->withSchema($schema);
6✔
170
        }
171

172
        if (null === $parameter->getOpenApi() && $filter instanceof OpenApiParameterFilterInterface && ($openApiParameter = $filter->getOpenApiParameters($parameter))) {
13✔
173
            $parameter = $parameter->withOpenApi($openApiParameter);
6✔
174
        }
175

176
        return $parameter;
13✔
177
    }
178

179
    /**
180
     * @param array<string, ApiProperty> $properties
181
     */
182
    private function setDefaults(string $key, Parameter $parameter, string $resourceClass, array $properties, Operation $operation): Parameter
183
    {
184
        if (null === $parameter->getKey()) {
22✔
185
            $parameter = $parameter->withKey($key);
22✔
186
        }
187

188
        $filter = $parameter->getFilter();
22✔
189
        if (\is_string($filter) && $this->filterLocator->has($filter)) {
22✔
190
            $filter = $this->filterLocator->get($filter);
13✔
191
        }
192

193
        if ($filter instanceof SerializerFilterInterface && null === $parameter->getProvider()) {
22✔
194
            $parameter = $parameter->withProvider('api_platform.serializer.filter_parameter_provider');
6✔
195
        }
196

197
        // Read filter description to populate the Parameter
198
        $description = $filter instanceof FilterInterface ? $filter->getDescription($this->getFilterClass($operation)) : [];
22✔
199
        if (($schema = $description[$key]['schema'] ?? null) && null === $parameter->getSchema()) {
22✔
UNCOV
200
            $parameter = $parameter->withSchema($schema);
×
201
        }
202

203
        if (($openapi = $description[$key]['openapi'] ?? null) && null === $parameter->getOpenApi() && $openapi instanceof OpenApiParameter) {
22✔
UNCOV
204
            $parameter = $parameter->withOpenApi($openapi);
×
205
        }
206

207
        $currentKey = $key;
22✔
208
        if (null === $parameter->getProperty() && isset($properties[$key])) {
22✔
209
            $parameter = $parameter->withProperty($key);
10✔
210
        }
211

212
        if (null === $parameter->getProperty() && $this->nameConverter && ($nameConvertedKey = $this->nameConverter->normalize($key)) && isset($properties[$nameConvertedKey])) {
22✔
UNCOV
213
            $parameter = $parameter->withProperty($key)->withExtraProperties(['_query_property' => $nameConvertedKey] + $parameter->getExtraProperties());
×
214
            $currentKey = $nameConvertedKey;
×
215
        }
216

217
        if ($this->nameConverter && $property = $parameter->getProperty()) {
22✔
218
            $parameter = $parameter->withProperty($this->nameConverter->normalize($property));
13✔
219
        }
220

221
        if (isset($properties[$currentKey]) && ($eloquentRelation = ($properties[$currentKey]->getExtraProperties()['eloquent_relation'] ?? null)) && isset($eloquentRelation['foreign_key'])) {
22✔
UNCOV
222
            $parameter = $parameter->withExtraProperties(['_query_property' => $eloquentRelation['foreign_key']] + $parameter->getExtraProperties());
×
223
        }
224

225
        if (null === $parameter->getRequired() && ($required = $description[$key]['required'] ?? null)) {
22✔
UNCOV
226
            $parameter = $parameter->withRequired($required);
×
227
        }
228

229
        return $this->addFilterMetadata($parameter);
22✔
230
    }
231

232
    private function getFilterClass(Operation $operation): ?string
233
    {
234
        $stateOptions = $operation->getStateOptions();
13✔
235
        if ($stateOptions instanceof DoctrineORMOptions) {
13✔
236
            return $stateOptions->getEntityClass();
9✔
237
        }
238
        if ($stateOptions instanceof DoctrineODMOptions) {
13✔
239
            return $stateOptions->getDocumentClass();
6✔
240
        }
241

242
        return $operation->getClass();
9✔
243
    }
244
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc