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

api-platform / core / 10814494863

11 Sep 2024 03:11PM UTC coverage: 7.008% (-0.7%) from 7.679%
10814494863

push

github

web-flow
fix(state): remove resource_class change (#6607)

11516 of 164321 relevant lines covered (7.01%)

22.85 hits per line

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

96.83
/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
    public function __construct(private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, private readonly ResourceClassResolverInterface $resourceClassResolver)
30
    {
31
    }
2,252✔
32

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

44
        $identifiers = $this->resourceClassResolver->isResourceClass($relationClass) ? $this->getIdentifiersFromResourceClass($relationClass) : ['id'];
1✔
45

46
        return new Link(fromClass: $relationClass, toProperty: $property, identifiers: $identifiers, parameterName: $property);
1✔
47
    }
48

49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function createLinksFromIdentifiers(Metadata $operation): array
53
    {
54
        $identifiers = $this->getIdentifiersFromResourceClass($resourceClass = $operation->getClass());
33✔
55

56
        if (!$identifiers) {
33✔
57
            return [];
5✔
58
        }
59

60
        $link = (new Link())->withFromClass($resourceClass)->withIdentifiers($identifiers);
32✔
61
        $parameterName = $identifiers[0];
32✔
62
        if ('value' === $parameterName && enum_exists($resourceClass)) {
32✔
63
            $parameterName = 'id';
3✔
64
        }
65

66
        if (1 < \count($identifiers)) {
32✔
67
            $parameterName = 'id';
5✔
68
            $link = $link->withCompositeIdentifier(true);
5✔
69
        }
70

71
        return [$link->withParameterName($parameterName)];
32✔
72
    }
73

74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function createLinksFromRelations(Metadata $operation): array
78
    {
79
        $links = [];
31✔
80
        foreach ($this->propertyNameCollectionFactory->create($resourceClass = $operation->getClass()) as $property) {
31✔
81
            $metadata = $this->propertyMetadataFactory->create($resourceClass, $property);
31✔
82

83
            if (!($relationClass = $this->getPropertyClassType($metadata->getBuiltinTypes())) || !$this->resourceClassResolver->isResourceClass($relationClass)) {
31✔
84
                continue;
31✔
85
            }
86

87
            $identifiers = $this->getIdentifiersFromResourceClass($resourceClass);
16✔
88

89
            $links[] = (new Link())->withFromProperty($property)->withFromClass($resourceClass)->withToClass($relationClass)->withIdentifiers($identifiers);
16✔
90
        }
91

92
        return $links;
31✔
93
    }
94

95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function createLinksFromAttributes(Metadata $operation): array
99
    {
100
        $links = [];
31✔
101
        try {
102
            $reflectionClass = new \ReflectionClass($resourceClass = $operation->getClass());
31✔
103
            foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
31✔
104
                $reflectionProperty = $reflectionClass->getProperty($property);
31✔
105

106
                foreach ($reflectionProperty->getAttributes(Link::class) as $attributeLink) {
31✔
107
                    $metadata = $this->propertyMetadataFactory->create($resourceClass, $property);
3✔
108

109
                    $attributeLink = $attributeLink->newInstance()
3✔
110
                        ->withFromProperty($property);
3✔
111

112
                    if (!$attributeLink->getFromClass()) {
3✔
113
                        $attributeLink = $attributeLink->withFromClass($resourceClass)->withToClass($this->getPropertyClassType($metadata->getBuiltinTypes()) ?? $resourceClass);
3✔
114
                    }
115

116
                    $links[] = $attributeLink;
3✔
117
                }
118
            }
119
        } catch (\ReflectionException) {
8✔
120
        }
121

122
        return $links;
31✔
123
    }
124

125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function completeLink(Link $link): Link
129
    {
130
        if (!$link->getIdentifiers()) {
18✔
131
            $link = $link->withIdentifiers($this->getIdentifiersFromResourceClass($link->getFromClass()));
15✔
132
        }
133

134
        if (1 < \count((array) $link->getIdentifiers())) {
18✔
135
            $link = $link->withCompositeIdentifier(true);
×
136
        }
137

138
        return $link;
18✔
139
    }
140

141
    private function getIdentifiersFromResourceClass(string $resourceClass): array
142
    {
143
        $hasIdProperty = false;
33✔
144
        $identifiers = [];
33✔
145
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
33✔
146
            $isIdentifier = $this->propertyMetadataFactory->create($resourceClass, $property)->isIdentifier();
33✔
147

148
            if (!$hasIdProperty && null === $isIdentifier) {
33✔
149
                $hasIdProperty = 'id' === $property;
33✔
150
            }
151

152
            if ($isIdentifier) {
33✔
153
                $identifiers[] = $property;
30✔
154
            }
155
        }
156

157
        if ($hasIdProperty && !$identifiers) {
33✔
158
            return ['id'];
4✔
159
        }
160

161
        if (!$hasIdProperty && !$identifiers && enum_exists($resourceClass)) {
31✔
162
            return ['value'];
3✔
163
        }
164

165
        return $identifiers;
31✔
166
    }
167

168
    /**
169
     * @param Type[]|null $types
170
     */
171
    private function getPropertyClassType(?array $types): ?string
172
    {
173
        foreach ($types ?? [] as $type) {
31✔
174
            if ($type->isCollection()) {
31✔
175
                return $this->getPropertyClassType($type->getCollectionValueTypes());
14✔
176
            }
177

178
            if ($class = $type->getClassName()) {
31✔
179
                return $class;
22✔
180
            }
181
        }
182

183
        return null;
31✔
184
    }
185
}
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

© 2026 Coveralls, Inc