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

api-platform / core / 11182119487

04 Oct 2024 02:50PM UTC coverage: 7.837% (+0.4%) from 7.441%
11182119487

push

github

soyuka
Merge 4.0

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

9900 existing lines in 360 files now uncovered.

12939 of 165109 relevant lines covered (7.84%)

27.02 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
    {
UNCOV
31
    }
2,687✔
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
    {
UNCOV
54
        $identifiers = $this->getIdentifiersFromResourceClass($resourceClass = $operation->getClass());
99✔
55

UNCOV
56
        if (!$identifiers) {
99✔
UNCOV
57
            return [];
26✔
58
        }
59

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

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

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

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

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

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

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

UNCOV
92
        return $links;
82✔
93
    }
94

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

UNCOV
106
                foreach ($reflectionProperty->getAttributes(Link::class) as $attributeLink) {
79✔
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
            }
UNCOV
119
        } catch (\ReflectionException) {
23✔
120
        }
121

UNCOV
122
        return $links;
82✔
123
    }
124

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

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

UNCOV
138
        return $link;
48✔
139
    }
140

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

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

UNCOV
152
            if ($isIdentifier) {
84✔
UNCOV
153
                $identifiers[] = $property;
78✔
154
            }
155
        }
156

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

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

UNCOV
165
        return $identifiers;
97✔
166
    }
167

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

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

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