Completed
Pull Request — master (#118)
by Christoffer
03:15
created

SchemaValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 11 2
1
<?php
2
3
namespace Digia\GraphQL\SchemaValidator;
4
5
use Digia\GraphQL\SchemaValidator\Rule\SupportedRules;
6
use Digia\GraphQL\Type\SchemaInterface;
7
8
class SchemaValidator implements SchemaValidatorInterface
9
{
10
    /**
11
     * @var ContextBuilderInterface
12
     */
13
    protected $contextBuilder;
14
15
    /**
16
     * @var SchemaValidatorInterface[]
17
     */
18
    protected $validators;
19
20
    /**
21
     * SchemaValidator constructor.
22
     * @param ContextBuilderInterface|null $contextBuilder
23
     */
24
    public function __construct(?ContextBuilderInterface $contextBuilder = null)
25
    {
26
        $this->contextBuilder = $contextBuilder ?? new ContextBuilder();
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function validate(SchemaInterface $schema, ?array $validators = null): array
33
    {
34
        $context = $this->contextBuilder->build($schema);
35
36
        $validators = $validators ?? SupportedRules::build();
37
38
        foreach ($validators as $validator) {
39
            $validator->validate($context);
40
        }
41
42
        return $context->getErrors();
43
    }
44
}
45