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

api-platform / core / 11035928124

25 Sep 2024 03:10PM UTC coverage: 5.963% (-1.9%) from 7.833%
11035928124

push

github

dunglas
Merge branch '4.0'

2 of 55 new or added lines in 6 files covered. (3.64%)

2414 existing lines in 166 files now uncovered.

9834 of 164915 relevant lines covered (5.96%)

4.85 hits per line

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

74.6
/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
    }
429✔
32

33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function createLinkFromProperty(Metadata $operation, string $property): Link
37
    {
38
        $metadata = $this->propertyMetadataFactory->create($resourceClass = $operation->getClass(), $property);
×
39
        $relationClass = $this->getPropertyClassType($metadata->getBuiltinTypes());
×
40
        if (!$relationClass) {
×
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'];
×
45

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

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

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

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

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

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

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

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

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

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

92
        return $links;
51✔
93
    }
94

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

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

UNCOV
109
                    $attributeLink = $attributeLink->newInstance()
×
UNCOV
110
                        ->withFromProperty($property);
×
111

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

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

122
        return $links;
51✔
123
    }
124

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

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

138
        return $link;
30✔
139
    }
140

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

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

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

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

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

165
        return $identifiers;
66✔
166
    }
167

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

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

183
        return null;
48✔
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