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

api-platform / core / 7644887434

24 Jan 2024 06:54PM UTC coverage: 61.704%. Remained the same
7644887434

push

github

web-flow
chore: subtree split token (#6125)

15671 of 25397 relevant lines covered (61.7%)

30.89 hits per line

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

41.18
/src/Symfony/EventListener/QueryParameterValidateListener.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\Symfony\EventListener;
15

16
use ApiPlatform\Doctrine\Odm\State\Options as ODMOptions;
17
use ApiPlatform\Doctrine\Orm\State\Options;
18
use ApiPlatform\Metadata\CollectionOperationInterface;
19
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20
use ApiPlatform\ParameterValidator\ParameterValidator;
21
use ApiPlatform\State\ProviderInterface;
22
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
23
use ApiPlatform\State\Util\RequestParser;
24
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
25
use Symfony\Component\HttpKernel\Event\RequestEvent;
26

27
/**
28
 * Validates query parameters depending on filter description.
29
 *
30
 * @author Julien Deniau <julien.deniau@mapado.com>
31
 */
32
final class QueryParameterValidateListener
33
{
34
    use OperationRequestInitiatorTrait;
35

36
    public const OPERATION_ATTRIBUTE_KEY = 'query_parameter_validate';
37
    private ?ParameterValidator $queryParameterValidator = null;
38
    private ?ProviderInterface $provider = null;
39

40
    public function __construct($queryParameterValidator, ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null)
41
    {
42
        if ($queryParameterValidator instanceof ProviderInterface) {
27✔
43
            $this->provider = $queryParameterValidator;
×
44
        } else {
45
            trigger_deprecation('api-platform/core', '3.3', 'Use a "%s" as first argument in "%s" instead of "%s".', ProviderInterface::class, self::class, ParameterValidator::class);
27✔
46
            $this->queryParameterValidator = $queryParameterValidator;
27✔
47
        }
48

49
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
27✔
50
    }
51

52
    public function onKernelRequest(RequestEvent $event): void
53
    {
54
        $request = $event->getRequest();
27✔
55
        $operation = $this->initializeOperation($request);
27✔
56

57
        if ($operation && $this->provider instanceof ProviderInterface) {
27✔
58
            if (null === $operation->getQueryParameterValidationEnabled()) {
×
59
                $operation = $operation->withQueryParameterValidationEnabled($request->isMethodSafe() && 'GET' === $request->getMethod() && $operation instanceof CollectionOperationInterface);
×
60
            }
61

62
            $this->provider->provide($operation, $request->attributes->get('_api_uri_variables') ?? [], [
×
63
                'request' => $request,
×
64
                'uri_variables' => $request->attributes->get('_api_uri_variables') ?? [],
×
65
                'resource_class' => $operation->getClass(),
×
66
            ]);
×
67

68
            return;
×
69
        }
70

71
        if (
72
            !$request->isMethodSafe()
27✔
73
            || !($attributes = RequestAttributesExtractor::extractAttributes($request))
27✔
74
            || 'GET' !== $request->getMethod()
27✔
75
            || $request->attributes->get('_api_platform_disable_listeners')
27✔
76
        ) {
77
            return;
17✔
78
        }
79

80
        if ('api_platform.symfony.main_controller' === $operation?->getController()) {
13✔
81
            return;
13✔
82
        }
83

84
        if (!($operation?->getQueryParameterValidationEnabled() ?? true) || !$operation instanceof CollectionOperationInterface) {
×
85
            return;
×
86
        }
87

88
        $queryString = RequestParser::getQueryString($request);
×
89
        $queryParameters = $queryString ? RequestParser::parseRequestParams($queryString) : [];
×
90

91
        $class = $attributes['resource_class'];
×
92

93
        if ($options = $operation->getStateOptions()) {
×
94
            if ($options instanceof Options && $options->getEntityClass()) {
×
95
                $class = $options->getEntityClass();
×
96
            }
97

98
            if ($options instanceof ODMOptions && $options->getDocumentClass()) {
×
99
                $class = $options->getDocumentClass();
×
100
            }
101
        }
102

103
        $this->queryParameterValidator->validateFilters($class, $operation->getFilters() ?? [], $queryParameters);
×
104
    }
105
}
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