Completed
Pull Request — master (#56)
by Frédéric G.
03:07
created

AbstractSchema   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 60
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
build() 0 1 ?
A addQueryField() 0 4 1
A addMutationField() 0 4 1
A getQueryType() 0 4 1
A getMutationType() 0 4 1
A getTypes() 0 4 1
A getName() 0 6 2
A __construct() 0 16 4
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 5/5/16 9:24 PM
7
*/
8
9
namespace Youshido\GraphQL\Schema;
10
11
12
use Youshido\GraphQL\Config\Schema\SchemaConfig;
13
use Youshido\GraphQL\Type\Object\ObjectType;
14
use Youshido\GraphQL\Type\TypeInterface;
15
16
abstract class AbstractSchema
17
{
18
19
    /** @var SchemaConfig */
20
    protected $config;
21
22 48
    public function __construct($config = [])
23
    {
24 48
        if (!array_key_exists('query', $config)) {
25 32
            $config['query'] = new InternalSchemaQueryObject(['name' => $this->getName() . 'Query']);
26
        }
27 48
        if (!array_key_exists('mutation', $config)) {
28 46
            $config['mutation'] = new InternalSchemaMutationObject(['name' => $this->getName() . 'Mutation']);
29
        }
30 48
        if (!array_key_exists('types', $config)) {
31 48
          $config['types'] = [];
32
        }
33
34 48
        $this->config = new SchemaConfig($config, $this);
35
36 48
        $this->build($this->config);
37 48
    }
38
39
    abstract public function build(SchemaConfig $config);
40
41 40
    public function addQueryField($field, $fieldInfo = null)
42
    {
43 40
        $this->getQueryType()->addField($field, $fieldInfo);
44 40
    }
45
46 2
    public function addMutationField($field, $fieldInfo = null)
47
    {
48 2
        $this->getMutationType()->addField($field, $fieldInfo);
49 2
    }
50
51 48
    final public function getQueryType()
52
    {
53 48
        return $this->config->getQuery();
54
    }
55
56 30
    final public function getMutationType()
57
    {
58 30
        return $this->config->getMutation();
59
    }
60
61
    /**
62
     * @return callable|mixed|null
63
     */
64 5
    public function getTypes()
65
    {
66 5
        return $this->config->getTypes();
67
    }
68
69 35
    public function getName()
70
    {
71 35
        $defaultName = 'RootSchema';
72
73 35
        return $this->config ? $this->config->get('name', $defaultName) : $defaultName;
74
    }
75
}
76