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

api-platform / core / 14954769666

11 May 2025 10:14AM UTC coverage: 0.0% (-8.5%) from 8.457%
14954769666

Pull #7135

github

web-flow
Merge bf21e0bc7 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

0 of 2 new or added lines in 2 files covered. (0.0%)

11040 existing lines in 370 files now uncovered.

0 of 48303 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/Metadata/Resource/Factory/LinkFactory.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\Metadata\Exception\RuntimeException;
17
use ApiPlatform\Metadata\Link;
18
use ApiPlatform\Metadata\Metadata;
19
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
20
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
21
use ApiPlatform\Metadata\ResourceClassResolverInterface;
22
use Symfony\Component\PropertyInfo\Type;
23

24
/**
25
 * @internal
26
 */
27
final class LinkFactory implements LinkFactoryInterface, PropertyLinkFactoryInterface
28
{
29
    /**
30
     * @var array<class-string, string[]>
31
     */
32
    private $localIdentifiersPerResourceClassCache = [];
33

34
    public function __construct(private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ResourceClassResolverInterface $resourceClassResolver)
35
    {
UNCOV
36
    }
×
37

38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function createLinkFromProperty(Metadata $operation, string $property): Link
42
    {
43
        $metadata = $this->propertyMetadataFactory->create($resourceClass = $operation->getClass(), $property);
×
44
        $relationClass = $this->getPropertyClassType($metadata->getBuiltinTypes());
×
45
        if (!$relationClass) {
×
46
            throw new RuntimeException(\sprintf('We could not find a class matching the uriVariable "%s" on "%s".', $property, $resourceClass));
×
47
        }
48

49
        $identifiers = $this->resourceClassResolver->isResourceClass($relationClass) ? $this->getIdentifiersFromResourceClass($relationClass) : ['id'];
×
50

51
        return new Link(fromClass: $relationClass, toProperty: $property, identifiers: $identifiers, parameterName: $property);
×
52
    }
53

54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function createLinksFromIdentifiers(Metadata $operation): array
58
    {
UNCOV
59
        $identifiers = $this->getIdentifiersFromResourceClass($resourceClass = $operation->getClass());
×
60

UNCOV
61
        if (!$identifiers) {
×
UNCOV
62
            return [];
×
63
        }
64

UNCOV
65
        $link = (new Link())->withFromClass($resourceClass)->withIdentifiers($identifiers);
×
UNCOV
66
        $parameterName = $identifiers[0];
×
UNCOV
67
        if ('value' === $parameterName && enum_exists($resourceClass)) {
×
UNCOV
68
            $parameterName = 'id';
×
69
        }
70

UNCOV
71
        if (1 < \count($identifiers)) {
×
72
            $parameterName = 'id';
×
73
            $link = $link->withCompositeIdentifier(true);
×
74
        }
75

UNCOV
76
        return [$link->withParameterName($parameterName)];
×
77
    }
78

79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function createLinksFromRelations(Metadata $operation): array
83
    {
UNCOV
84
        $links = [];
×
UNCOV
85
        foreach ($this->propertyNameCollectionFactory->create($resourceClass = $operation->getClass()) as $property) {
×
UNCOV
86
            $metadata = $this->propertyMetadataFactory->create($resourceClass, $property);
×
87

UNCOV
88
            if (!($relationClass = $this->getPropertyClassType($metadata->getBuiltinTypes())) || !$this->resourceClassResolver->isResourceClass($relationClass)) {
×
UNCOV
89
                continue;
×
90
            }
91

UNCOV
92
            $identifiers = $this->getIdentifiersFromResourceClass($resourceClass);
×
93

UNCOV
94
            $links[] = (new Link())->withFromProperty($property)->withFromClass($resourceClass)->withToClass($relationClass)->withIdentifiers($identifiers);
×
95
        }
96

UNCOV
97
        return $links;
×
98
    }
99

100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function createLinksFromAttributes(Metadata $operation): array
104
    {
UNCOV
105
        $links = [];
×
106
        try {
UNCOV
107
            $reflectionClass = new \ReflectionClass($resourceClass = $operation->getClass());
×
UNCOV
108
            foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
×
UNCOV
109
                $reflectionProperty = $reflectionClass->getProperty($property);
×
110

UNCOV
111
                foreach ($reflectionProperty->getAttributes(Link::class) as $attributeLink) {
×
112
                    $metadata = $this->propertyMetadataFactory->create($resourceClass, $property);
×
113

114
                    $attributeLink = $attributeLink->newInstance()
×
115
                        ->withFromProperty($property);
×
116

117
                    if (!$attributeLink->getFromClass()) {
×
118
                        $attributeLink = $attributeLink->withFromClass($resourceClass)->withToClass($this->getPropertyClassType($metadata->getBuiltinTypes()) ?? $resourceClass);
×
119
                    }
120

121
                    $links[] = $attributeLink;
×
122
                }
123
            }
UNCOV
124
        } catch (\ReflectionException) {
×
125
        }
126

UNCOV
127
        return $links;
×
128
    }
129

130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function completeLink(Link $link): Link
134
    {
UNCOV
135
        if (!$link->getIdentifiers()) {
×
UNCOV
136
            $link = $link->withIdentifiers($this->getIdentifiersFromResourceClass($link->getFromClass()));
×
137
        }
138

UNCOV
139
        if (1 < \count((array) $link->getIdentifiers())) {
×
140
            $link = $link->withCompositeIdentifier(true);
×
141
        }
142

UNCOV
143
        return $link;
×
144
    }
145

146
    /**
147
     * @param class-string $resourceClass
148
     *
149
     * @return string[]
150
     */
151
    private function getIdentifiersFromResourceClass(string $resourceClass): array
152
    {
UNCOV
153
        if (isset($this->localIdentifiersPerResourceClassCache[$resourceClass])) {
×
UNCOV
154
            return $this->localIdentifiersPerResourceClassCache[$resourceClass];
×
155
        }
156

UNCOV
157
        $hasIdProperty = false;
×
UNCOV
158
        $identifiers = [];
×
UNCOV
159
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
×
UNCOV
160
            $isIdentifier = $this->propertyMetadataFactory->create($resourceClass, $property)->isIdentifier();
×
161

UNCOV
162
            if (!$hasIdProperty && null === $isIdentifier) {
×
UNCOV
163
                $hasIdProperty = 'id' === $property;
×
164
            }
165

UNCOV
166
            if ($isIdentifier) {
×
UNCOV
167
                $identifiers[] = $property;
×
168
            }
169
        }
170

UNCOV
171
        if ($hasIdProperty && !$identifiers) {
×
172
            return $this->localIdentifiersPerResourceClassCache[$resourceClass] = ['id'];
×
173
        }
174

UNCOV
175
        if (!$hasIdProperty && !$identifiers && enum_exists($resourceClass)) {
×
UNCOV
176
            return $this->localIdentifiersPerResourceClassCache[$resourceClass] = ['value'];
×
177
        }
178

UNCOV
179
        return $this->localIdentifiersPerResourceClassCache[$resourceClass] = $identifiers;
×
180
    }
181

182
    /**
183
     * @param Type[]|null $types
184
     */
185
    private function getPropertyClassType(?array $types): ?string
186
    {
UNCOV
187
        foreach ($types ?? [] as $type) {
×
UNCOV
188
            if ($type->isCollection()) {
×
UNCOV
189
                return $this->getPropertyClassType($type->getCollectionValueTypes());
×
190
            }
191

UNCOV
192
            if ($class = $type->getClassName()) {
×
UNCOV
193
                return $class;
×
194
            }
195
        }
196

UNCOV
197
        return null;
×
198
    }
199
}
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