Completed
Pull Request — master (#13)
by Rafael
06:46
created

UnionDefinitionType   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
eloc 36
dl 0
loc 57
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 52 9
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Type\Definition;
12
13
use GraphQL\Type\Definition\Type;
14
use GraphQL\Type\Definition\UnionType;
15
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
16
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
17
use Ynlo\GraphQLBundle\Definition\InterfaceDefinition;
18
use Ynlo\GraphQLBundle\Definition\UnionDefinition;
19
use Ynlo\GraphQLBundle\Definition\UnionTypeDefinition;
20
use Ynlo\GraphQLBundle\Type\Registry\TypeRegistry;
21
use Ynlo\GraphQLBundle\Util\TypeUtil;
22
23
class UnionDefinitionType extends UnionType implements ContainerAwareInterface, EndpointAwareInterface
24
{
25
    use ContainerAwareTrait;
26
    use EndpointAwareTrait;
27
28
    public function __construct(UnionDefinition $definition)
29
    {
30
        parent::__construct(
31
            [
32
                'name' => $definition->getName(),
33
                'description' => $definition->getDescription(),
34
                'types' => function () use ($definition) {
35
                    $types = [];
36
                    $unionTypes = $definition->getTypes();
37
38
                    // expand interface types
39
                    foreach ($unionTypes as $index => $unionType) {
40
                        $typeDefinition = $this->endpoint->getType($unionType->getType());
41
                        if ($typeDefinition instanceof InterfaceDefinition) {
42
                            $interfaceTypes = $typeDefinition->getImplementors();
43
                            unset($unionTypes[$index]);
44
                            foreach ($interfaceTypes as $interfaceType) {
45
                                $newUnionType = new UnionTypeDefinition();
46
                                $newUnionType->setType($interfaceType);
47
                                $newUnionType->setList($unionType->isList());
48
                                $newUnionType->setNonNull($unionType->isNonNull());
49
                                $newUnionType->setNonNullList($unionType->isNonNullList());
50
                                $unionTypes[] = $newUnionType;
51
                            }
52
                        }
53
                    }
54
55
                    foreach ($unionTypes as $unionType) {
56
                        $type = TypeRegistry::get($unionType->getType());
57
                        if ($unionType->isList()) {
58
                            if ($unionType->isNonNullList()) {
59
                                $type = Type::nonNull($type);
60
                            }
61
                            $type = Type::listOf($type);
62
                        }
63
64
                        if ($unionType->isNonNull()) {
65
                            $type = Type::nonNull($type);
66
                        }
67
68
                        $types[] = $type;
69
                    }
70
71
                    return $types;
72
                },
73
                'resolveType' => function ($value) {
74
                    $type = TypeUtil::resolveObjectType($this->endpoint, $value);
75
                    if (!$type) {
76
                        throw new \RuntimeException(sprintf('Can`t resolve a valid type for object of class %s.', get_class($value)));
77
                    }
78
79
                    return TypeRegistry::get($type);
80
                },
81
            ]
82
        );
83
    }
84
}
85