DataCollectingValidator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 6.06%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 84
ccs 2
cts 33
cp 0.0606
rs 10
wmc 15

12 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 3 1
A inContext() 0 3 1
A getSubscribedEvents() 0 4 1
A startContext() 0 3 1
A getLastErrors() 0 3 1
A clearLastErrors() 0 3 1
A __construct() 0 4 1
A getMetadataFor() 0 3 1
A validatePropertyValue() 0 3 1
A onKernelRequest() 0 14 4
A validateProperty() 0 3 1
A hasMetadataFor() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Liip/FunctionalTestBundle
7
 *
8
 * (c) Lukas Kahwe Smith <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Liip\FunctionalTestBundle\Validator;
15
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18
use Symfony\Component\HttpKernel\KernelEvents;
19
use Symfony\Component\Validator\ConstraintViolationList;
20
use Symfony\Component\Validator\ConstraintViolationListInterface;
21
use Symfony\Component\Validator\Context\ExecutionContextInterface;
22
use Symfony\Component\Validator\Mapping\MetadataInterface;
23
use Symfony\Component\Validator\Validator\ContextualValidatorInterface;
24
use Symfony\Component\Validator\Validator\ValidatorInterface;
25
26
class DataCollectingValidator implements ValidatorInterface, EventSubscriberInterface
27
{
28
    /**
29
     * @var ValidatorInterface
30
     */
31
    protected $wrappedValidator;
32
33
    /**
34
     * @var ConstraintViolationListInterface
35
     */
36
    protected $lastErrors;
37
38
    public function __construct(ValidatorInterface $wrappedValidator)
39
    {
40
        $this->wrappedValidator = $wrappedValidator;
41
        $this->clearLastErrors();
42
    }
43
44
    public function clearLastErrors(): void
45
    {
46
        $this->lastErrors = new ConstraintViolationList();
47
    }
48
49
    public function getLastErrors(): ConstraintViolationListInterface
50
    {
51
        return $this->lastErrors;
52
    }
53
54
    public function getMetadataFor($value): MetadataInterface
55
    {
56
        return $this->wrappedValidator->getMetadataFor($value);
57
    }
58
59
    public function hasMetadataFor($value): bool
60
    {
61
        return $this->wrappedValidator->hasMetadataFor($value);
62
    }
63
64
    public function validate($value, $constraints = null, $groups = null): ConstraintViolationListInterface
65
    {
66
        return $this->lastErrors = $this->wrappedValidator->validate($value, $constraints, $groups);
67
    }
68
69
    public function validateProperty($object, $propertyName, $groups = null): ConstraintViolationListInterface
70
    {
71
        return $this->wrappedValidator->validateProperty($object, $propertyName, $groups);
72
    }
73
74
    public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null): ConstraintViolationListInterface
75
    {
76
        return $this->wrappedValidator->validatePropertyValue($objectOrClass, $propertyName, $value, $groups);
77
    }
78
79
    public function startContext(): ContextualValidatorInterface
80
    {
81
        return $this->wrappedValidator->startContext();
82
    }
83
84
    public function inContext(ExecutionContextInterface $context): ContextualValidatorInterface
85
    {
86
        return $this->wrappedValidator->inContext($context);
87
    }
88
89
    public function onKernelRequest(GetResponseEvent $event): void
90
    {
91
        // The isMainRequest method has been added in Symfony 5.3
92
        if (method_exists($event, 'isMainRequest')) {
93
            if ($event->isMainRequest()) {
94
                $this->clearLastErrors();
95
            }
96
97
            return;
98
        }
99
100
        // For Symfony < 5.3, call the legacy method
101
        if ($event->isMasterRequest()) {
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\HttpKe...vent::isMasterRequest() has been deprecated: since symfony/http-kernel 5.3, use isMainRequest() instead ( Ignorable by Annotation )

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

101
        if (/** @scrutinizer ignore-deprecated */ $event->isMasterRequest()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
102
            $this->clearLastErrors();
103
        }
104
    }
105
106 1
    public static function getSubscribedEvents(): array
107
    {
108
        return [
109 1
            KernelEvents::REQUEST => ['onKernelRequest', 99999],
110
        ];
111
    }
112
}
113