Completed
Push — master ( 9e9a0d...740d0f )
by Rafael
04:13
created

SchemaCompiler::compile()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8.0109

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 32
nop 0
dl 0
loc 35
ccs 17
cts 18
cp 0.9444
crap 8.0109
rs 5.3846
c 0
b 0
f 0
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\Schema;
12
13
use GraphQL\Type\Schema;
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
16
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
17
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
18
use Ynlo\GraphQLBundle\Type\Types;
19
20
/**
21
 * GraphQL Schema compiler
22
 *
23
 * Compile all definitions into graphql-php schema
24
 */
25
class SchemaCompiler implements ContainerAwareInterface
26
{
27
    use ContainerAwareTrait;
28
29
    /**
30
     * @var DefinitionRegistry
31
     */
32
    protected $registry;
33
34
    /**
35
     * @var Endpoint
36
     */
37
    protected $endpoint;
38
39
    /**
40
     * SchemaCompiler constructor.
41
     *
42
     * @param DefinitionRegistry $registry
43
     */
44 20
    public function __construct(DefinitionRegistry $registry)
45
    {
46 20
        $this->registry = $registry;
47 20
    }
48
49
    /**
50
     * @return Schema
51
     */
52 20
    public function compile(): Schema
53
    {
54 20
        $this->endpoint = $this->registry->getEndpoint();
55 20
        Types::setUp($this->container, $this->endpoint);
56
57
        //automatically create all interface implementors
58
        //to avoid empty interfaces
59 20
        foreach ($this->endpoint->allInterfaces() as $type) {
60 20
            foreach ($type->getImplementors() as $implementor) {
61 20
                if (!Types::has($implementor)) {
62 20
                    Types::create($implementor);
63
                }
64
            }
65
        }
66
67
        $config = [
68 20
            'types' => Types::all(),
69 20
            'typeLoader' => function ($name) {
70 20
                return Types::get($name);
71 20
            },
72
        ];
73
74 20
        if ($this->endpoint->allQueries()) {
75 20
            $config['query'] = Types::get('Query');
76
        }
77
78 20
        if ($this->endpoint->allMutations()) {
79 20
            $config['mutation'] = Types::get('Mutation');
80
        }
81
82 20
        if (isset($config['query']) || isset($config['mutation'])) {
83 20
            return new Schema($config);
84
        }
85
86
        throw new \RuntimeException('Your GraphQL schema is empty. Create your first object and query and try again');
87
    }
88
}
89