Passed
Push — dev ( 40f0b5...3a2e98 )
by Fike
02:50
created

Validator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Indexing;
4
5
use AmaTeam\ElasticSearch\API\Indexing\Validation\ContextInterface;
6
use AmaTeam\ElasticSearch\API\Indexing\ValidatorInterface;
7
use AmaTeam\ElasticSearch\API\IndexingInterface;
8
use AmaTeam\ElasticSearch\Indexing\Option\Infrastructure\Registry;
9
use AmaTeam\ElasticSearch\Indexing\Validation\Constraint\ValidOptionName;
10
use Symfony\Component\Validator\Constraints\NotNull;
11
use Symfony\Component\Validator\ConstraintViolationListInterface;
12
use Symfony\Component\Validator\Validation;
13
14
class Validator implements ValidatorInterface
15
{
16
    /**
17
     * @var Registry
18
     */
19
    private $registry;
20
21
    /**
22
     * @param Registry $registry
23
     */
24
    public function __construct(Registry $registry = null)
25
    {
26
        $this->registry = $registry ?? Registry::getInstance();
27
    }
28
29
    public function validate(
30
        IndexingInterface $indexing,
31
        ContextInterface $context = null
32
    ): ConstraintViolationListInterface {
33
        $validator = Validation::createValidator()->startContext();
34
        foreach ($indexing->getOptions() as $name => $value) {
35
            $validator->atPath($name);
36
            $option = $this->registry->find($name);
37
            if (!$option && $context->shouldPreserveUnknownOptions()) {
0 ignored issues
show
Bug introduced by
The method shouldPreserveUnknownOptions() 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

37
            if (!$option && $context->/** @scrutinizer ignore-call */ shouldPreserveUnknownOptions()) {

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...
38
                continue;
39
            }
40
            $validator->validate($name, [new ValidOptionName()]);
41
            if (!$option) {
42
                continue;
43
            }
44
            $constraints = $option->getConstraints();
45
            if ($option->allowsNullValue()) {
46
                $constraints[] = new NotNull();
47
            }
48
            $validator->validate($value, $constraints);
49
        }
50
        return $validator->getViolations();
51
    }
52
}
53