Passed
Push — master ( 1ea166...6004d9 )
by Kévin
03:04
created

Validator::validate()   C

Complexity

Conditions 10
Paths 14

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 5.6534
c 0
b 0
f 0
cc 10
eloc 15
nc 14
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Bridge\Symfony\Validator;
15
16
use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
17
use ApiPlatform\Core\Validator\ValidatorInterface;
18
use Psr\Container\ContainerInterface;
19
use Symfony\Component\Validator\Constraints\GroupSequence;
20
use Symfony\Component\Validator\Validator\ValidatorInterface as SymfonyValidatorInterface;
21
22
/**
23
 * Validates an item using the Symfony validator component.
24
 *
25
 * @author Kévin Dunglas <[email protected]>
26
 */
27
class Validator implements ValidatorInterface
28
{
29
    private $validator;
30
    private $container;
31
32
    public function __construct(SymfonyValidatorInterface $validator, ContainerInterface $container = null)
33
    {
34
        $this->validator = $validator;
35
        $this->container = $container;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function validate($data, array $context = [])
42
    {
43
        if (null !== $validationGroups = $context['groups'] ?? null) {
44
            if (
45
                $this->container &&
46
                \is_string($validationGroups) &&
47
                $this->container->has($validationGroups) &&
48
                ($service = $this->container->get($validationGroups)) &&
49
                \is_callable($service)
50
            ) {
51
                $validationGroups = $service($data);
52
            } elseif (\is_callable($validationGroups)) {
53
                $validationGroups = $validationGroups($data);
54
            }
55
56
            if (!$validationGroups instanceof GroupSequence) {
57
                $validationGroups = (array) $validationGroups;
58
            }
59
        }
60
61
        $violations = $this->validator->validate($data, null, $validationGroups);
62
        if (0 !== \count($violations)) {
63
            throw new ValidationException($violations);
64
        }
65
    }
66
}
67