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

api-platform / core / 10014117656

19 Jul 2024 08:44PM UTC coverage: 7.856% (-56.3%) from 64.185%
10014117656

push

github

soyuka
Merge branch 'sf/remove-flag'

0 of 527 new or added lines in 83 files covered. (0.0%)

10505 existing lines in 362 files now uncovered.

12705 of 161727 relevant lines covered (7.86%)

26.85 hits per line

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

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

16
use ApiPlatform\Metadata\Operation;
17
use ApiPlatform\State\Exception\ProviderNotFoundException;
18
use ApiPlatform\State\ParameterNotFound;
19
use ApiPlatform\State\ParameterProviderInterface;
20
use ApiPlatform\State\ProviderInterface;
21
use ApiPlatform\State\Util\ParameterParserTrait;
22
use ApiPlatform\State\Util\RequestParser;
23
use Psr\Container\ContainerInterface;
24

25
/**
26
 * Loops over parameters to:
27
 *   - compute its values set as extra properties from the Parameter object (`_api_values`)
28
 *   - call the Parameter::provider if any and updates the Operation
29
 *
30
 * @experimental
31
 */
32
final class ParameterProvider implements ProviderInterface
33
{
34
    use ParameterParserTrait;
35

36
    public function __construct(private readonly ?ProviderInterface $decorated = null, private readonly ?ContainerInterface $locator = null)
37
    {
UNCOV
38
    }
2,597✔
39

40
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
41
    {
UNCOV
42
        $request = $context['request'] ?? null;
2,517✔
UNCOV
43
        if ($request && null === $request->attributes->get('_api_query_parameters')) {
2,517✔
UNCOV
44
            $queryString = RequestParser::getQueryString($request);
2,152✔
UNCOV
45
            $request->attributes->set('_api_query_parameters', $queryString ? RequestParser::parseRequestParams($queryString) : []);
2,152✔
46
        }
47

UNCOV
48
        if ($request && null === $request->attributes->get('_api_header_parameters')) {
2,517✔
UNCOV
49
            $request->attributes->set('_api_header_parameters', $request->headers->all());
2,152✔
50
        }
51

UNCOV
52
        $context = ['operation' => $operation] + $context;
2,517✔
UNCOV
53
        $parameters = $operation->getParameters();
2,517✔
UNCOV
54
        foreach ($parameters ?? [] as $parameter) {
2,517✔
UNCOV
55
            $values = $this->getParameterValues($parameter, $request, $context);
825✔
UNCOV
56
            $value = $this->extractParameterValues($parameter, $values);
825✔
57

UNCOV
58
            if (($default = $parameter->getSchema()['default'] ?? false) && ($value instanceof ParameterNotFound || !$value)) {
825✔
UNCOV
59
                $value = $default;
477✔
60
            }
61

UNCOV
62
            if ($value instanceof ParameterNotFound) {
825✔
UNCOV
63
                continue;
812✔
64
            }
65

UNCOV
66
            $parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties(
728✔
UNCOV
67
                $parameter->getExtraProperties() + ['_api_values' => $value]
728✔
UNCOV
68
            ));
728✔
69

UNCOV
70
            if (null === ($provider = $parameter->getProvider())) {
728✔
UNCOV
71
                continue;
713✔
72
            }
73

UNCOV
74
            if (\is_callable($provider)) {
15✔
UNCOV
75
                if (($op = $provider($parameter, $values, $context)) instanceof Operation) {
6✔
UNCOV
76
                    $operation = $op;
3✔
77
                }
78

UNCOV
79
                continue;
3✔
80
            }
81

UNCOV
82
            if (!\is_string($provider) || !$this->locator->has($provider)) {
12✔
83
                throw new ProviderNotFoundException(sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
×
84
            }
85

86
            /** @var ParameterProviderInterface $providerInstance */
UNCOV
87
            $providerInstance = $this->locator->get($provider);
12✔
UNCOV
88
            if (($op = $providerInstance->provide($parameter, $values, $context)) instanceof Operation) {
12✔
UNCOV
89
                $operation = $op;
9✔
90
            }
91
        }
92

UNCOV
93
        if ($parameters) {
2,517✔
UNCOV
94
            $operation = $operation->withParameters($parameters);
1,187✔
95
        }
UNCOV
96
        $request?->attributes->set('_api_operation', $operation);
2,517✔
UNCOV
97
        $context['operation'] = $operation;
2,517✔
98

UNCOV
99
        return $this->decorated?->provide($operation, $uriVariables, $context);
2,517✔
100
    }
101
}
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