Passed
Push — feature/initial-implementation ( fae671...591f29 )
by Fike
02:37
created

Loader::validate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.2
cc 4
eloc 9
nc 4
nop 2
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Entity;
4
5
use AmaTeam\ElasticSearch\API\Entity\AssemblerInterface;
6
use AmaTeam\ElasticSearch\API\Entity\Descriptor\ManagerInterface;
7
use AmaTeam\ElasticSearch\API\Entity\Loader\Context;
8
use AmaTeam\ElasticSearch\API\Entity\Loader\ContextInterface;
9
use AmaTeam\ElasticSearch\API\Entity\LoaderInterface;
10
use AmaTeam\ElasticSearch\API\Entity\NormalizerInterface;
11
use AmaTeam\ElasticSearch\API\Entity\ValidatorInterface;
12
use AmaTeam\ElasticSearch\API\EntityInterface;
13
use AmaTeam\ElasticSearch\API\Entity\Validation\ValidationException;
14
15
class Loader implements LoaderInterface
16
{
17
    /**
18
     * @var ManagerInterface
19
     */
20
    private $manager;
21
    /**
22
     * @var AssemblerInterface
23
     */
24
    private $assembler;
25
    /**
26
     * @var ValidatorInterface
27
     */
28
    private $validator;
29
    /**
30
     * @var NormalizerInterface
31
     */
32
    private $normalizer;
33
34
    /**
35
     * @param ManagerInterface $manager
36
     * @param AssemblerInterface|null $assembler
37
     * @param ValidatorInterface|null $validator
38
     * @param NormalizerInterface|null $normalizer
39
     */
40
    public function __construct(
41
        ManagerInterface $manager,
42
        AssemblerInterface $assembler = null,
43
        ValidatorInterface $validator = null,
44
        NormalizerInterface $normalizer = null
45
    ) {
46
        $this->manager = $manager;
47
        $this->assembler = $assembler ?? new Assembler($manager);
48
        $this->validator = $validator ?? new Validator();
49
        $this->normalizer = $normalizer ?? new Normalizer();
50
    }
51
52
53
    public function load(string $name, ContextInterface $context = null): ?EntityInterface
54
    {
55
        $descriptor = $this->manager->find($name);
56
        if (!$descriptor) {
57
            return null;
58
        }
59
        $context = $context ?? new Context();
60
        $entity = $this->assembler->assemble($descriptor);
61
        $entity = $this->normalize($entity, $context);
62
        $this->validate($entity, $context);
63
        return $entity;
64
    }
65
66
    private function normalize(EntityInterface $entity, ContextInterface $context): EntityInterface
67
    {
68
        if (!$context->shouldNormalize()) {
69
            return $entity;
70
        }
71
        return $this->normalizer->normalize($entity, $context);
0 ignored issues
show
Unused Code introduced by
The call to AmaTeam\ElasticSearch\AP...rInterface::normalize() has too many arguments starting with $context. ( Ignorable by Annotation )

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

71
        return $this->normalizer->/** @scrutinizer ignore-call */ normalize($entity, $context);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
72
    }
73
74
    private function validate(EntityInterface $entity, ContextInterface $context): void
75
    {
76
        if (!$context->shouldValidate()) {
77
            return;
78
        }
79
        $violations = $this->validator->validate($entity, $context);
80
        if ($violations->count() === 0) {
81
            return;
82
        }
83
        $lines = [sprintf('Entity metadata `%s` has failed validation:', $entity->getName())];
84
        foreach ($violations as $violation) {
85
            $lines[] = $violation->getPropertyPath() . ': ' . $violation->getMessage();
86
        }
87
        throw new ValidationException(implode(PHP_EOL, $lines), $violations);
88
    }
89
90
    public function exists(string $name): bool
91
    {
92
        return $this->manager->exists($name);
93
    }
94
}
95