Completed
Push — master ( 17976d...325209 )
by Portey
07:00
created

CharacterInterface   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 7
c 1
b 1
f 1
lcom 0
cbo 7
dl 0
loc 44
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 12 1
A getDescription() 0 4 1
A getName() 0 4 1
A resolveType() 0 17 4
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 12/6/15 11:15 PM
7
*/
8
9
namespace Youshido\Tests\StarWars\Schema;
10
11
12
use Youshido\GraphQL\Type\Config\Object\InterfaceTypeConfig;
13
use Youshido\GraphQL\Type\ListType\ListType;
14
use Youshido\GraphQL\Type\Object\AbstractInterfaceType;
15
use Youshido\GraphQL\Type\TypeMap;
16
17
class CharacterInterface extends AbstractInterfaceType
18
{
19
    protected function build(InterfaceTypeConfig $config)
20
    {
21
        $config
22
            ->addField('id', TypeMap::TYPE_ID, ['required' => true])
23
            ->addField('name', TypeMap::TYPE_STRING, ['required' => true])
24
            ->addField('friends', new ListType(new CharacterInterface()), [
25
                'resolve' => function($value) {
26
                    return $value['friends'];
27
                }
28
            ])
29
            ->addField('appearsIn', new ListType(new EpisodeEnum()));
30
    }
31
32
    public function getDescription()
33
    {
34
        return 'A character in the Star Wars Trilogy';
35
    }
36
37
    public function getName()
38
    {
39
        return 'Character';
40
    }
41
42
    public function resolveType($object)
43
    {
44
        $humans = StarWarsData::humans();
45
        $droids = StarWarsData::droids();
46
47
        $id = isset($object['id']) ? $object['id'] : $object;
48
49
        if (isset($humans[$id])) {
50
            return new HumanType();
51
        }
52
53
        if (isset($droids[$id])) {
54
            return new DroidType();
55
        }
56
57
        return null;
58
    }
59
60
}
61