Completed
Push — master ( cbfed7...c39fb6 )
by Rafael
05:06
created

SchemaCompiler::compile()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 16
nop 1
dl 0
loc 31
ccs 16
cts 16
cp 1
crap 6
rs 8.439
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\DefinitionManager;
17
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry;
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 DefinitionManager
36
     */
37
    protected $manager;
38
39
    /**
40
     * SchemaCompiler constructor.
41
     *
42
     * @param DefinitionRegistry $registry
43
     */
44 14
    public function __construct(DefinitionRegistry $registry)
45
    {
46 14
        $this->registry = $registry;
47 14
    }
48
49
    /**
50
     * @param string $endpoint
51
     *
52
     * @return Schema
53
     */
54 14
    public function compile(string $endpoint = 'default'): Schema
55
    {
56 14
        $this->manager = $this->registry->getManager($endpoint);
57 14
        Types::setUp($this->container, $this->manager);
58
59
        //automatically create all interface implementors
60
        //to avoid empty interfaces
61 14
        foreach ($this->manager->allInterfaces() as $type) {
62 14
            foreach ($type->getImplementors() as $implementor) {
63 14
                if (!Types::has($implementor)) {
64 14
                    Types::create($implementor);
65
                }
66
            }
67
        }
68
69
        $config = [
70 14
            'types' => Types::all(),
71 14
            'typeLoader' => function ($name) {
72 14
                return Types::get($name);
73 14
            },
74
        ];
75
76 14
        if ($this->manager->allQueries()) {
77 14
            $config['query'] = Types::get('Query');
78
        }
79
80 14
        if ($this->manager->allMutations()) {
81 14
            $config['mutation'] = Types::get('Mutation');
82
        }
83
84 14
        return new Schema($config);
85
    }
86
}
87