Passed
Push — master ( 4a62fb...07cec0 )
by Rafael
06:16
created

SchemaCompiler::compile()   D

Complexity

Conditions 9
Paths 64

Size

Total Lines 39
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9.081

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 18
cts 20
cp 0.9
rs 4.909
c 0
b 0
f 0
cc 9
eloc 19
nc 64
nop 2
crap 9.081
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\Type\Registry\TypeRegistry;
18
19
/**
20
 * GraphQL Schema compiler
21
 *
22
 * Compile all definitions into graphql-php schema
23
 */
24
class SchemaCompiler implements ContainerAwareInterface
25
{
26
    use ContainerAwareTrait;
27
28
    protected $registry;
29
30 22
    public function __construct(DefinitionRegistry $registry)
31
    {
32 22
        $this->registry = $registry;
33 22
    }
34
35
    /**
36
     * Compile the given endpoint name and return the current schema
37
     *
38
     * @param string $name       endpoint name
39
     * @param bool   $clearCache force clear cache before compile to get a fresh cache
40
     *                           this option should not be used in production environments.
41
     *
42
     * @return Schema
43
     */
44 22
    public function compile($name, $clearCache = false): Schema
45
    {
46 22
        if ($clearCache) {
47
            $this->registry->clearCache();
48
        }
49
50 22
        $endpoint = $this->registry->getEndpoint($name);
51 22
        TypeRegistry::setUp($this->container, $endpoint);
52
53
        //automatically create all interface implementors
54
        //to avoid empty interfaces
55 22
        foreach ($endpoint->allInterfaces() as $type) {
56 22
            foreach ($type->getImplementors() as $implementor) {
57 22
                if (!TypeRegistry::has($implementor)) {
58 22
                    TypeRegistry::create($implementor);
59
                }
60
            }
61
        }
62
63
        $config = [
64 22
            'types' => TypeRegistry::all(),
65 22
            'typeLoader' => function ($name) {
66 22
                return TypeRegistry::get($name);
67 22
            },
68
        ];
69
70 22
        if ($endpoint->allQueries()) {
71 22
            $config['query'] = TypeRegistry::get('Query');
72
        }
73
74 22
        if ($endpoint->allMutations()) {
75 22
            $config['mutation'] = TypeRegistry::get('Mutation');
76
        }
77
78 22
        if (isset($config['query']) || isset($config['mutation'])) {
79 22
            return new Schema($config);
80
        }
81
82
        throw new \RuntimeException('Your GraphQL schema is empty. Create your first object and query and try again');
83
    }
84
}
85