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

assertInterfaceImplementationCorrect()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4
Metric Value
dl 0
loc 10
rs 9.2
ccs 6
cts 6
cp 1
cc 4
eloc 5
nc 4
nop 1
crap 4
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