InterfaceFromObjectType::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
cc 2
nc 1
nop 4
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 string $name The name of the interface
15
     * @param ObjectType $type
16
     * @param RecursiveTypeMapperInterface $typeMapper
17
     */
18
    public function __construct(string $name, ObjectType $type, ?ObjectType $subType, RecursiveTypeMapperInterface $typeMapper)
19
    {
20
        parent::__construct([
21
            'name' => $name,
22
            'fields' => function() use ($type) {
23
                return $type->getFields();
24
            },
25
            'description' => $type->description,
26
            'resolveType' => function($value) use ($typeMapper, $subType) {
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, $subType);
34
            }
35
        ]);
36
    }
37
}
38