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

api-platform / core / 5936124107

22 Aug 2023 08:06AM UTC coverage: 58.84% (-0.004%) from 58.844%
5936124107

push

github

web-flow
fix(metadata): notexposed no urivariables inheritance (#5765)

* fix(metadata): notexposed no urivariables inheritance

* use named arguments

Co-authored-by: Vincent <407859+vincentchalamon@users.noreply.github.com>

* fix tests

---------

Co-authored-by: Vincent <407859+vincentchalamon@users.noreply.github.com>

8 of 8 new or added lines in 3 files covered. (100.0%)

10856 of 18450 relevant lines covered (58.84%)

19.81 hits per line

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

50.0
/src/Api/IdentifiersExtractor.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\Api;
15

16
use ApiPlatform\Exception\RuntimeException;
17
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
21
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
24
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
25
use Symfony\Component\PropertyAccess\PropertyAccess;
26
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
27

28
/**
29
 * {@inheritdoc}
30
 *
31
 * @author Antoine Bluchet <soyuka@gmail.com>
32
 */
33
final class IdentifiersExtractor implements IdentifiersExtractorInterface
34
{
35
    use ResourceClassInfoTrait;
36
    private readonly PropertyAccessorInterface $propertyAccessor;
37

38
    public function __construct(ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, PropertyAccessorInterface $propertyAccessor = null)
39
    {
40
        $this->resourceMetadataFactory = $resourceMetadataFactory;
64✔
41
        $this->resourceClassResolver = $resourceClassResolver;
64✔
42
        $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor();
64✔
43
    }
44

45
    /**
46
     * {@inheritdoc}
47
     *
48
     * TODO: 3.0 identifiers should be stringable?
49
     */
50
    public function getIdentifiersFromItem(object $item, Operation $operation = null, array $context = []): array
51
    {
52
        if (!$this->isResourceClass($this->getObjectClass($item))) {
24✔
53
            return ['id' => $this->propertyAccessor->getValue($item, 'id')];
×
54
        }
55

56
        if ($operation && $operation->getClass()) {
24✔
57
            return $this->getIdentifiersFromOperation($item, $operation, $context);
20✔
58
        }
59

60
        $resourceClass = $this->getResourceClass($item, true);
4✔
61
        $operation ??= $this->resourceMetadataFactory->create($resourceClass)->getOperation(null, false, true);
4✔
62

63
        return $this->getIdentifiersFromOperation($item, $operation, $context);
4✔
64
    }
65

66
    private function getIdentifiersFromOperation(object $item, Operation $operation, array $context = []): array
67
    {
68
        if ($operation instanceof HttpOperation) {
24✔
69
            $links = $operation->getUriVariables();
24✔
70
        } elseif ($operation instanceof GraphQlOperation) {
×
71
            $links = $operation->getLinks();
×
72
        }
73

74
        $identifiers = [];
24✔
75
        foreach ($links ?? [] as $k => $link) {
24✔
76
            $linkIdentifiers = $link->getIdentifiers() ?? [$k];
20✔
77
            if (1 < \count($linkIdentifiers)) {
20✔
78
                $compositeIdentifiers = [];
×
79
                foreach ($linkIdentifiers as $identifier) {
×
80
                    $compositeIdentifiers[$identifier] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $identifier, $link->getParameterName());
×
81
                }
82

83
                $identifiers[$link->getParameterName()] = CompositeIdentifierParser::stringify($compositeIdentifiers);
×
84
                continue;
×
85
            }
86

87
            $parameterName = $link->getParameterName();
20✔
88
            $identifiers[$parameterName] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $linkIdentifiers[0], $parameterName, $link->getToProperty());
20✔
89
        }
90

91
        return $identifiers;
24✔
92
    }
93

94
    /**
95
     * Gets the value of the given class property.
96
     */
97
    private function getIdentifierValue(object $item, string $class, string $property, string $parameterName, string $toProperty = null): float|bool|int|string
98
    {
99
        if ($item instanceof $class) {
20✔
100
            try {
101
                return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
18✔
102
            } catch (NoSuchPropertyException $e) {
×
103
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
×
104
            }
105
        }
106

107
        if ($toProperty) {
2✔
108
            return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$toProperty.$property"), $parameterName);
2✔
109
        }
110

111
        $resourceClass = $this->getResourceClass($item, true);
×
112
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
×
113
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
×
114

115
            $types = $propertyMetadata->getBuiltinTypes();
×
116
            if (null === ($type = $types[0] ?? null)) {
×
117
                continue;
×
118
            }
119

120
            try {
121
                if ($type->isCollection()) {
×
122
                    $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
×
123

124
                    if (null !== $collectionValueType && $collectionValueType->getClassName() === $class) {
×
125
                        return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, sprintf('%s[0].%s', $propertyName, $property)), $parameterName);
×
126
                    }
127
                }
128

129
                if ($type->getClassName() === $class) {
×
130
                    return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$propertyName.$property"), $parameterName);
×
131
                }
132
            } catch (NoSuchPropertyException $e) {
×
133
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
×
134
            }
135
        }
136

137
        throw new RuntimeException('Not able to retrieve identifiers.');
×
138
    }
139

140
    /**
141
     * TODO: in 3.0 this method just uses $identifierValue instanceof \Stringable and we remove the weird behavior.
142
     *
143
     * @param mixed|\Stringable $identifierValue
144
     */
145
    private function resolveIdentifierValue(mixed $identifierValue, string $parameterName): float|bool|int|string
146
    {
147
        if (null === $identifierValue) {
20✔
148
            throw new RuntimeException('No identifier value found, did you forget to persist the entity?');
×
149
        }
150

151
        if (\is_scalar($identifierValue)) {
20✔
152
            return $identifierValue;
18✔
153
        }
154

155
        if ($identifierValue instanceof \Stringable) {
2✔
156
            return (string) $identifierValue;
×
157
        }
158

159
        if ($identifierValue instanceof \BackedEnum) {
2✔
160
            return (string) $identifierValue->value;
2✔
161
        }
162

163
        throw new RuntimeException(sprintf('We were not able to resolve the identifier matching parameter "%s".', $parameterName));
×
164
    }
165
}
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