Completed
Push — master ( f8702a...461e07 )
by Alexandr
04:08
created

SchemaValidator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 76%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
c 3
b 0
f 0
lcom 1
cbo 10
dl 0
loc 56
rs 10
ccs 19
cts 25
cp 0.76

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 16 4
A assertInterfaceImplementationCorrect() 0 10 4
B assertFieldsIdentical() 0 17 5
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 4/30/16 9:11 PM
7
*/
8
9
namespace Youshido\GraphQL\Validator\SchemaValidator;
10
11
12
use Youshido\GraphQL\AbstractSchema;
13
use Youshido\GraphQL\Type\Field\Field;
14
use Youshido\GraphQL\Type\Object\AbstractObjectType;
15
use Youshido\GraphQL\Validator\ErrorContainer\ErrorContainerInterface;
16
use Youshido\GraphQL\Validator\ErrorContainer\ErrorContainerTrait;
17
use Youshido\GraphQL\Validator\Exception\ConfigurationException;
18
19
class SchemaValidator implements ErrorContainerInterface
20
{
21
    use ErrorContainerTrait;
22
23 19
    public function validate(AbstractSchema $schema)
24
    {
25
        try {
26 19
            foreach ($schema->getQueryType()->getConfig()->getFields() as $field) {
27 18
                if ($field->getType() instanceof AbstractObjectType) {
28 19
                    $this->assertInterfaceImplementationCorrect($field->getType());
29
                }
30
            }
31
        } catch (\Exception $e) {
32
            $this->addError($e);
33
34
            return false;
35
        }
36
37 19
        return true;
38
    }
39
40 18
    protected function assertInterfaceImplementationCorrect(AbstractObjectType $type)
41
    {
42 18
        if (!$type->getInterfaces()) return true;
43
44 7
        foreach ($type->getInterfaces() as $interface) {
45 7
            foreach ($interface->getConfig()->getFields() as $intField) {
46 7
                $this->assertFieldsIdentical($intField, $type->getConfig()->getField($intField->getName()), $interface);
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Youshido\GraphQL\Type\Field\Field. Did you maybe mean getNamedType()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
47
            }
48
        }
49 7
    }
50
51
    /**
52
     * @param Field $intField
53
     * @param Field $objField
54
     * @return bool
55
     * @throws ConfigurationException
56
     */
57 7
    protected function assertFieldsIdentical($intField, $objField, $interface)
58
    {
59 7
        $intType = $intField->getConfig()->getType();
60 7
        $objType = $objField->getConfig()->getType();
61
62 7
        $isValid = true;
63 7
        if ($intType->getName() != $objType->getName()) {
64
            $isValid = false;
65
        }
66 7
        if ($intType->isCompositeType() && ($intType->getNamedType()->getName() != $objType->getNamedType()->getName())) {
0 ignored issues
show
Bug introduced by
The method getNamedType() does not exist on Youshido\GraphQL\Type\TypeInterface. Did you maybe mean getName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
67
            $isValid = false;
68
        }
69
70 7
        if (!$isValid) {
71
            throw new ConfigurationException(sprintf('Implementation of %s is invalid for the field %s', $interface->getName(), $objField->getName()));
0 ignored issues
show
Bug introduced by
The method getName() does not exist on Youshido\GraphQL\Type\Field\Field. Did you maybe mean getNamedType()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
72
        }
73 7
    }
74
}
75