Passed
Pull Request — master (#44)
by David
02:06
created

InterfaceFromObjectType   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 12
dl 0
loc 23
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 2
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Types;
5
6
7
use GraphQL\Type\Definition\ObjectType;
8
use GraphQL\Type\Definition\OutputType;
9
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface;
10
11
class InterfaceFromObjectType extends \GraphQL\Type\Definition\InterfaceType
12
{
13
    /**
14
     * @param ObjectType $type
15
     * @param RecursiveTypeMapperInterface $typeMapper
16
     */
17
    public function __construct(ObjectType $type, RecursiveTypeMapperInterface $typeMapper)
18
    {
19
        $name = $type->name.'Interface';
20
        $fields = $type->getFields();
21
22
        parent::__construct([
23
            'name' => $name,
24
            'fields' => $fields,
25
            'description' => $type->description,
26
            'resolveType' => function($value) use ($typeMapper) {
27
                if (!is_object($value)) {
28
                    throw new \InvalidArgumentException('Expected object for resolveType. Got: "'.gettype($value).'"');
29
                }
30
31
                $className = get_class($value);
32
33
                return $typeMapper->mapClassToType($className);
34
            }
35
        ]);
36
    }
37
}
38