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

api-platform / core / 7582727293

19 Jan 2024 10:45AM UTC coverage: 61.96% (+2.8%) from 59.207%
7582727293

push

github

web-flow
feat(symfony): request and view kernel listeners (#6102)

133 of 266 new or added lines in 19 files covered. (50.0%)

447 existing lines in 32 files now uncovered.

17435 of 28139 relevant lines covered (61.96%)

32.39 hits per line

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

0.0
/src/Symfony/EventListener/ValidateListener.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\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
17
use ApiPlatform\State\ProviderInterface;
18
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
19
use ApiPlatform\Validator\Exception\ValidationException;
20
use ApiPlatform\Validator\ValidatorInterface;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpKernel\Event\ViewEvent;
23

24
/**
25
 * Validates data.
26
 *
27
 * @author Kévin Dunglas <dunglas@gmail.com>
28
 */
29
final class ValidateListener
30
{
31
    use OperationRequestInitiatorTrait;
32

33
    public const OPERATION_ATTRIBUTE_KEY = 'validate';
34

35
    private ValidatorInterface $validator;
36
    private ?ProviderInterface $provider = null;
37

38
    public function __construct(ProviderInterface|ValidatorInterface $validator, ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory)
39
    {
NEW
40
        if ($validator instanceof ProviderInterface) {
×
NEW
41
            $this->provider = $validator;
×
42
        } else {
NEW
43
            trigger_deprecation('api-platform/core', '3.3', 'Use a "%s" as first argument in "%s" instead of "%s".', ProviderInterface::class, self::class, ValidatorInterface::class);
×
NEW
44
            $this->validator = $validator;
×
45
        }
46

UNCOV
47
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
×
48
    }
49

50
    /**
51
     * Validates data returned by the controller if applicable.
52
     *
53
     * @throws ValidationException
54
     */
55
    public function onKernelView(ViewEvent $event): void
56
    {
57
        $controllerResult = $event->getControllerResult();
×
58
        $request = $event->getRequest();
×
59
        $operation = $this->initializeOperation($request);
×
60

NEW
61
        if ($operation && $this->provider instanceof ProviderInterface) {
×
NEW
62
            if (null === $operation->canValidate()) {
×
NEW
63
                $operation = $operation->withValidate(!$request->isMethodSafe() && !$request->isMethod('DELETE'));
×
64
            }
65

NEW
66
            $this->provider->provide($operation, $request->attributes->get('_api_uri_variables') ?? [], [
×
NEW
67
                'request' => $request,
×
NEW
68
                'uri_variables' => $request->attributes->get('_api_uri_variables') ?? [],
×
NEW
69
                'resource_class' => $operation->getClass(),
×
NEW
70
            ]);
×
71

NEW
72
            return;
×
73
        }
74

75
        if ('api_platform.symfony.main_controller' === $operation?->getController() || $request->attributes->get('_api_platform_disable_listeners')) {
×
76
            return;
×
77
        }
78

79
        if (
80
            $controllerResult instanceof Response
×
81
            || $request->isMethodSafe()
×
82
            || $request->isMethod('DELETE')
×
83
        ) {
84
            return;
×
85
        }
86

87
        if (!$operation || !($operation->canValidate() ?? true)) {
×
88
            return;
×
89
        }
90

91
        $this->validator->validate($controllerResult, $operation->getValidationContext() ?? []);
×
92
    }
93
}
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