Passed
Pull Request — master (#118)
by Christoffer
02:09
created

Validator::validate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 4
1
<?php
2
3
namespace Digia\GraphQL\Validation;
4
5
use Digia\GraphQL\Language\Node\DocumentNode;
6
use Digia\GraphQL\Language\Visitor\ParallelVisitor;
7
use Digia\GraphQL\Language\Visitor\TypeInfoVisitor;
8
use Digia\GraphQL\SchemaValidator\SchemaValidatorInterface;
9
use Digia\GraphQL\Type\SchemaInterface;
10
use function Digia\GraphQL\Util\invariant;
11
use Digia\GraphQL\Util\TypeInfo;
12
use Digia\GraphQL\Validation\Rule\RuleInterface;
13
use Digia\GraphQL\Validation\Rule\RulesBuilderInterface;
0 ignored issues
show
Bug introduced by
The type Digia\GraphQL\Validation...e\RulesBuilderInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Digia\GraphQL\Validation\Rule\SupportedRules;
15
16
class Validator implements ValidatorInterface
17
{
18
    /**
19
     * @var ContextBuilderInterface
20
     */
21
    protected $contextBuilder;
22
23
    /**
24
     * @var SchemaValidatorInterface
25
     */
26
    protected $schemaValidator;
27
28
    /**
29
     * Validator constructor.
30
     * @param ContextBuilderInterface  $contextBuilder
31
     * @param SchemaValidatorInterface $schemaValidator
32
     */
33
    public function __construct(ContextBuilderInterface $contextBuilder, SchemaValidatorInterface $schemaValidator)
34
    {
35
        $this->contextBuilder  = $contextBuilder;
36
        $this->schemaValidator = $schemaValidator;
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function validate(
43
        SchemaInterface $schema,
44
        DocumentNode $document,
45
        ?array $rules = null,
46
        ?TypeInfo $typeInfo = null
47
    ): array {
48
        invariant(null !== $document, 'Must provided document');
49
50
        $this->schemaValidator->assertValid($schema);
51
52
        $typeInfo = $typeInfo ?? new TypeInfo($schema);
53
        $rules    = $rules ?? SupportedRules::build();
54
55
        $context = $this->contextBuilder->build($schema, $document, $typeInfo);
56
57
        $visitors = \array_map(function (RuleInterface $rule) use ($context) {
58
            return $rule->setContext($context);
59
        }, $rules);
60
61
        $visitor = new TypeInfoVisitor($typeInfo, new ParallelVisitor($visitors));
62
63
        // Visit the whole document with each instance of all provided rules.
64
        $document->acceptVisitor($visitor);
65
66
        return $context->getErrors();
67
    }
68
}
69