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

AbstractSchema::addMutationField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 2
crap 1
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