Completed
Push — master ( f9f0ef...24a695 )
by Portey
05:15
created

QueryType   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 61.36%

Importance

Changes 8
Bugs 3 Features 1
Metric Value
wmc 14
c 8
b 3
f 1
lcom 1
cbo 12
dl 0
loc 75
ccs 27
cts 44
cp 0.6136
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 9 2
A getName() 0 4 1
C build() 0 52 11
1
<?php
2
/**
3
 * Date: 03.12.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Definition;
9
10
use Youshido\GraphQL\Definition\Traits\TypeCollectorTrait;
11
use Youshido\GraphQL\Schema;
12
use Youshido\GraphQL\Type\Config\TypeConfigInterface;
13
use Youshido\GraphQL\Type\Field\Field;
14
use Youshido\GraphQL\Type\ListType\ListType;
15
use Youshido\GraphQL\Type\Object\AbstractObjectType;
16
use Youshido\GraphQL\Type\TypeInterface;
17
use Youshido\GraphQL\Type\TypeMap;
18
19
class QueryType extends AbstractObjectType
20
{
21
    use TypeCollectorTrait;
22
23 3
    public function resolve($value = null, $args = [])
24
    {
25
        /** @var Schema|Field $value */
26 3
        if ($value instanceof Schema) {
27 3
            return $value->getQueryType();
28
        }
29
30
        return $value->getConfig()->getType();
31
    }
32
33
    /**
34
     * @return String type name
35
     */
36 6
    public function getName()
37
    {
38 6
        return '__Type';
39
    }
40
41 6
    protected function build(TypeConfigInterface $config)
42
    {
43
        $config
44 6
            ->addField('name', TypeMap::TYPE_STRING)
45 6
            ->addField('kind', TypeMap::TYPE_STRING)
46 6
            ->addField('description', TypeMap::TYPE_STRING)
47 6
            ->addField('ofType', new QueryType(), [
48
                'resolve' => function ($value) {
49 2
                    if ($value->getKind() == TypeMap::KIND_LIST) {
50 1
                        return $value->getConfig()->getItem();
51
                    }
52
53 2
                    return null;
54
                }
55 6
            ])
56 6
            ->addField('inputFields', new InputValueListType())
57 6
            ->addField('enumValues', new EnumValueListType())
58 6
            ->addField('fields', new FieldListType())
59 6
            ->addField('interfaces', new InterfaceListType())
60 6
            ->addField('possibleTypes', new ListType(new QueryType()), [
61 2
                'resolve' => function ($value) {
62 2
                    if ($value) {
63 2
                        if ($value->getKind() == TypeMap::KIND_INTERFACE) {
64
                            $this->collectTypes(SchemaType::$schema->getQueryType());
65
66
                            $possibleTypes = [];
67
                            foreach ($this->types as $type) {
68
                                /** @var $type TypeInterface */
69
                                if ($type->getKind() == TypeMap::KIND_OBJECT) {
70
                                    $interfaces = $type->getConfig()->getInterfaces();
71
72
                                    if ($interfaces) {
73
                                        foreach ($interfaces as $interface) {
74
                                            if (get_class($interface) == get_class($value)) {
75
                                                $possibleTypes[] = $type;
76
                                            }
77
                                        }
78
                                    }
79
                                }
80
                            }
81
82
                            return $possibleTypes ?: [];
83 2
                        } elseif ($value->getKind() == TypeMap::KIND_UNION) {
84
                            return $value->getTypes();
85
                        }
86
87 2
                    }
88
89 2
                    return null;
90
                }
91 6
            ]);
92
    }
93
}