ValidateListener   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B onKernelView() 0 20 7
A __construct() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\Validator\EventListener;
15
16
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
17
use ApiPlatform\Core\Metadata\Resource\ToggleableOperationAttributeTrait;
18
use ApiPlatform\Core\Util\RequestAttributesExtractor;
19
use ApiPlatform\Core\Validator\Exception\ValidationException;
20
use ApiPlatform\Core\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 <[email protected]>
28
 */
29
final class ValidateListener
30
{
31
    use ToggleableOperationAttributeTrait;
32
33
    public const OPERATION_ATTRIBUTE_KEY = 'validate';
34
35
    private $validator;
36
    private $resourceMetadataFactory;
37
38
    public function __construct(ValidatorInterface $validator, ResourceMetadataFactoryInterface $resourceMetadataFactory)
39
    {
40
        $this->validator = $validator;
41
        $this->resourceMetadataFactory = $resourceMetadataFactory;
42
    }
43
44
    /**
45
     * Validates data returned by the controller if applicable.
46
     *
47
     * @throws ValidationException
48
     */
49
    public function onKernelView(ViewEvent $event): void
50
    {
51
        $controllerResult = $event->getControllerResult();
52
        $request = $event->getRequest();
53
54
        if (
55
            $controllerResult instanceof Response
56
            || $request->isMethodSafe()
57
            || $request->isMethod('DELETE')
58
            || !($attributes = RequestAttributesExtractor::extractAttributes($request))
59
            || !$attributes['receive']
60
            || $this->isOperationAttributeDisabled($attributes, self::OPERATION_ATTRIBUTE_KEY)
61
        ) {
62
            return;
63
        }
64
65
        $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);
0 ignored issues
show
Bug introduced by
The method create() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        /** @scrutinizer ignore-call */ 
66
        $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
67
        $validationGroups = $resourceMetadata->getOperationAttribute($attributes, 'validation_groups', null, true);
68
        $this->validator->validate($controllerResult, ['groups' => $validationGroups]);
69
    }
70
}
71