Test Setup Failed
Push — master ( 1595cb...050b68 )
by Kirill
21:00
created

SchemaValidator::assertOperation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/**
4
 * This file is part of Railt package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Railt\SDL\Validator;
13
14
use GraphQL\Contracts\TypeSystem\DefinitionInterface;
15
use GraphQL\Contracts\TypeSystem\SchemaInterface;
16
use Phplrt\Source\Exception\NotAccessibleException;
17
use Railt\SDL\Exception\TypeErrorException;
18
19
/**
20
 * Class SchemaValidator
21
 */
22
class SchemaValidator extends Validator
23
{
24
    /**
25
     * @var string
26
     */
27
    private const ERROR_SCHEMA_OPERATION_TYPE = 'Schema %s operation should be defined by GraphQL Object type';
28
29
    /**
30
     * @param DefinitionInterface $type
31
     * @return bool
32
     */
33
    public function match(DefinitionInterface $type): bool
34
    {
35
        return $type instanceof SchemaInterface;
36
    }
37
38
    /**
39
     * @param DefinitionInterface|SchemaInterface $schema
40
     * @return void
41
     * @throws TypeErrorException
42
     * @throws NotAccessibleException
43
     * @throws \RuntimeException
44
     */
45
    public function assert(DefinitionInterface $schema): void
46
    {
47
        $this->assertOperation(fn() => $schema->getQueryType(), 'query');
0 ignored issues
show
Bug introduced by
The method getQueryType() does not exist on GraphQL\Contracts\TypeSystem\DefinitionInterface. It seems like you code against a sub-type of GraphQL\Contracts\TypeSystem\DefinitionInterface such as GraphQL\Contracts\TypeSystem\SchemaInterface or Railt\TypeSystem\Schema. ( Ignorable by Annotation )

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

47
        $this->assertOperation(fn() => $schema->/** @scrutinizer ignore-call */ getQueryType(), 'query');
Loading history...
48
        $this->assertOperation(fn() => $schema->getMutationType(), 'mutation');
0 ignored issues
show
Bug introduced by
The method getMutationType() does not exist on GraphQL\Contracts\TypeSystem\DefinitionInterface. It seems like you code against a sub-type of GraphQL\Contracts\TypeSystem\DefinitionInterface such as GraphQL\Contracts\TypeSystem\SchemaInterface or Railt\TypeSystem\Schema. ( Ignorable by Annotation )

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

48
        $this->assertOperation(fn() => $schema->/** @scrutinizer ignore-call */ getMutationType(), 'mutation');
Loading history...
49
        $this->assertOperation(fn() => $schema->getSubscriptionType(), 'subscription');
0 ignored issues
show
Bug introduced by
The method getSubscriptionType() does not exist on GraphQL\Contracts\TypeSystem\DefinitionInterface. It seems like you code against a sub-type of GraphQL\Contracts\TypeSystem\DefinitionInterface such as GraphQL\Contracts\TypeSystem\SchemaInterface or Railt\TypeSystem\Schema. ( Ignorable by Annotation )

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

49
        $this->assertOperation(fn() => $schema->/** @scrutinizer ignore-call */ getSubscriptionType(), 'subscription');
Loading history...
50
51
        foreach ($schema->getTypeMap() as $type) {
0 ignored issues
show
Bug introduced by
The method getTypeMap() does not exist on GraphQL\Contracts\TypeSystem\DefinitionInterface. It seems like you code against a sub-type of GraphQL\Contracts\TypeSystem\DefinitionInterface such as GraphQL\Contracts\TypeSystem\SchemaInterface or Railt\TypeSystem\Schema. ( Ignorable by Annotation )

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

51
        foreach ($schema->/** @scrutinizer ignore-call */ getTypeMap() as $type) {
Loading history...
52
            $this->validate($type);
53
        }
54
55
        foreach ($schema->getDirectives() as $directive) {
0 ignored issues
show
Bug introduced by
The method getDirectives() does not exist on GraphQL\Contracts\TypeSystem\DefinitionInterface. It seems like you code against a sub-type of GraphQL\Contracts\TypeSystem\DefinitionInterface such as GraphQL\Contracts\TypeSystem\SchemaInterface or Railt\TypeSystem\Schema. ( Ignorable by Annotation )

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

55
        foreach ($schema->/** @scrutinizer ignore-call */ getDirectives() as $directive) {
Loading history...
56
            $this->validate($directive);
57
        }
58
    }
59
60
    /**
61
     * @param \Closure $closure
62
     * @param string $operation
63
     * @return void
64
     * @throws TypeErrorException
65
     * @throws NotAccessibleException
66
     * @throws \RuntimeException
67
     */
68
    private function assertOperation(\Closure $closure, string $operation): void
69
    {
70
        try {
71
            $closure();
72
        } catch (\Throwable $e) {
73
            throw new TypeErrorException(\sprintf(self::ERROR_SCHEMA_OPERATION_TYPE, $operation));
74
        }
75
    }
76
}
77