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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

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

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

61.54
/src/Metadata/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\Metadata;
15

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

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

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

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

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

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

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

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

75
        $identifiers = [];
943✔
76
        foreach ($links ?? [] as $k => $link) {
943✔
77
            if (1 < (is_countable($link->getIdentifiers()) ? \count($link->getIdentifiers()) : 0)) {
938✔
78
                $compositeIdentifiers = [];
×
79
                foreach ($link->getIdentifiers() 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();
938✔
88
            $identifiers[$parameterName] = $this->getIdentifierValue($item, $link->getFromClass() ?? $operation->getClass(), $link->getIdentifiers()[0] ?? $k, $parameterName, $link->getToProperty());
938✔
89
        }
90

91
        return $identifiers;
943✔
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) {
938✔
100
            try {
101
                return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, $property), $parameterName);
938✔
102
            } catch (NoSuchPropertyException $e) {
8✔
103
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
×
104
            }
105
        }
106

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

UNCOV
111
        $resourceClass = $this->getResourceClass($item, true);
11✔
112

UNCOV
113
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $propertyName) {
11✔
UNCOV
114
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName);
11✔
115

116
            // TODO: remove in 5.x
UNCOV
117
            if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
11✔
118
                $types = $propertyMetadata->getBuiltinTypes();
×
119
                if (null === ($type = $types[0] ?? null)) {
×
120
                    continue;
×
121
                }
122

123
                try {
124
                    if ($type->isCollection()) {
×
125
                        $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
×
126

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

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

UNCOV
140
            if (null === $type = $propertyMetadata->getNativeType()) {
11✔
141
                continue;
×
142
            }
143

144
            try {
UNCOV
145
                $collectionValueType = TypeHelper::getCollectionValueType($type);
11✔
146

UNCOV
147
                if ($collectionValueType?->isIdentifiedBy($class)) {
11✔
UNCOV
148
                    return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, \sprintf('%s[0].%s', $propertyName, $property)), $parameterName);
2✔
149
                }
150

UNCOV
151
                if ($type->isIdentifiedBy($class)) {
11✔
UNCOV
152
                    return $this->resolveIdentifierValue($this->propertyAccessor->getValue($item, "$propertyName.$property"), $parameterName);
11✔
153
                }
UNCOV
154
            } catch (NoSuchPropertyException $e) {
1✔
UNCOV
155
                throw new RuntimeException('Not able to retrieve identifiers.', $e->getCode(), $e);
1✔
156
            }
157
        }
158

UNCOV
159
        throw new RuntimeException('Not able to retrieve identifiers.');
3✔
160
    }
161

162
    /**
163
     * TODO: in 3.0 this method just uses $identifierValue instanceof \Stringable and we remove the weird behavior.
164
     *
165
     * @param mixed|\Stringable $identifierValue
166
     */
167
    private function resolveIdentifierValue(mixed $identifierValue, string $parameterName): float|bool|int|string
168
    {
169
        if (null === $identifierValue) {
938✔
170
            throw new RuntimeException('No identifier value found, did you forget to persist the entity?');
8✔
171
        }
172

173
        if (\is_scalar($identifierValue)) {
938✔
174
            return $identifierValue;
938✔
175
        }
176

177
        if ($identifierValue instanceof \Stringable) {
×
178
            return (string) $identifierValue;
×
179
        }
180

181
        if ($identifierValue instanceof \BackedEnum) {
×
182
            return (string) $identifierValue->value;
×
183
        }
184

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