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

api-platform / core / 18089937549

29 Sep 2025 07:56AM UTC coverage: 21.764% (-0.3%) from 22.093%
18089937549

Pull #7416

github

web-flow
Merge 061bcc790 into abe0438be
Pull Request #7416: fix(laravel): serializer attributes on Eloquent methods

0 of 151 new or added lines in 11 files covered. (0.0%)

5028 existing lines in 173 files now uncovered.

11889 of 54626 relevant lines covered (21.76%)

25.32 hits per line

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

80.85
/src/State/ParameterProvider/ReadLinkParameterProvider.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\State\ParameterProvider;
15

16
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\Link;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\Metadata\Parameter;
21
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
22
use ApiPlatform\State\Exception\ProviderNotFoundException;
23
use ApiPlatform\State\ParameterProviderInterface;
24
use ApiPlatform\State\ProviderInterface;
25
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26

27
/**
28
 * Checks if the linked resources have security attributes and prepares them for access checking.
29
 *
30
 * @experimental
31
 */
32
final class ReadLinkParameterProvider implements ParameterProviderInterface
33
{
34
    /**
35
     * @param ProviderInterface<object> $locator
36
     */
37
    public function __construct(
38
        private readonly ProviderInterface $locator,
39
        private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory,
40
    ) {
41
    }
14✔
42

43
    public function provide(Parameter $parameter, array $parameters = [], array $context = []): ?Operation
44
    {
45
        $operation = $context['operation'];
14✔
46
        $extraProperties = $parameter->getExtraProperties();
14✔
47

48
        if ($parameter instanceof Link) {
14✔
49
            $linkClass = $parameter->getFromClass() ?? $parameter->getToClass();
6✔
50
            $securityObjectName = $parameter->getSecurityObjectName() ?? $parameter->getToProperty() ?? $parameter->getFromProperty();
6✔
51
        }
52

53
        $securityObjectName ??= $parameter->getKey();
14✔
54

55
        $linkClass ??= $extraProperties['resource_class'] ?? $operation->getClass();
14✔
56

57
        if (!$linkClass) {
14✔
58
            return $operation;
×
59
        }
60

61
        $linkOperation = $this->resourceMetadataCollectionFactory
14✔
62
            ->create($linkClass)
14✔
63
            ->getOperation($operation->getExtraProperties()['parent_uri_template'] ?? $extraProperties['uri_template'] ?? null);
14✔
64

65
        $value = $parameter->getValue();
14✔
66

67
        if (\is_array($value) && array_is_list($value)) {
14✔
68
            $relation = [];
2✔
69

70
            foreach ($value as $v) {
2✔
71
                try {
72
                    $relation[] = $this->locator->provide($linkOperation, $this->getUriVariables($v, $parameter, $linkOperation), $context);
2✔
73
                } catch (ProviderNotFoundException) {
×
74
                }
75
            }
76
        } else {
77
            try {
78
                $relation = $this->locator->provide($linkOperation, $this->getUriVariables($value, $parameter, $linkOperation), $context);
12✔
79
            } catch (ProviderNotFoundException) {
×
80
                $relation = null;
×
81
            }
82
        }
83

84
        $parameter->setValue($relation);
14✔
85

86
        if (null === $relation && true === ($extraProperties['throw_not_found'] ?? true)) {
14✔
87
            throw new NotFoundHttpException('Relation for link security not found.');
2✔
88
        }
89

90
        $context['request']?->attributes->set($securityObjectName, $relation);
12✔
91

92
        if ($parameter instanceof Link) {
12✔
93
            $uriVariables = $operation->getUriVariables();
6✔
94
            $uriVariables[$parameter->getKey()] = $parameter;
6✔
95
            $operation = $operation->withUriVariables($uriVariables);
6✔
96
        }
97

98
        return $operation;
12✔
99
    }
100

101
    /**
102
     * @return array<string, string>
103
     */
104
    private function getUriVariables(mixed $value, Parameter $parameter, Operation $operation): array
105
    {
106
        $extraProperties = $parameter->getExtraProperties();
14✔
107

108
        if ($operation instanceof HttpOperation) {
14✔
109
            $links = $operation->getUriVariables();
14✔
UNCOV
110
        } elseif ($operation instanceof GraphQlOperation) {
×
UNCOV
111
            $links = $operation->getLinks();
×
112
        } else {
UNCOV
113
            $links = [];
×
114
        }
115

116
        if (!\is_array($value)) {
14✔
117
            $uriVariables = [];
14✔
118

119
            foreach ($links as $key => $link) {
14✔
120
                if (!\is_string($key)) {
14✔
UNCOV
121
                    $key = $link->getParameterName() ?? $extraProperties['uri_variable'] ?? $link->getFromProperty();
×
122
                }
123

124
                if (!$key || !\is_string($key)) {
14✔
UNCOV
125
                    continue;
×
126
                }
127

128
                $uriVariables[$key] = $value;
14✔
129
            }
130

131
            return $uriVariables;
14✔
132
        }
133

134
        return $value;
2✔
135
    }
136
}
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