Passed
Pull Request — master (#79)
by David
02:19
created

TypeAnnotatedObjectType   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 20
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMappedClassName() 0 3 1
A __construct() 0 5 1
A createFromAnnotatedClass() 0 22 4
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Types;
5
6
use GraphQL\Type\Definition\ObjectType;
7
use TheCodingMachine\GraphQL\Controllers\Annotations\Type;
8
use TheCodingMachine\GraphQL\Controllers\FieldsBuilderFactory;
9
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface;
10
11
/**
12
 * An object type built from the Type annotation
13
 */
14
class TypeAnnotatedObjectType extends MutableObjectType
15
{
16
    /**
17
     * @var string
18
     */
19
    private $className;
20
21
    public function __construct(string $className, array $config)
22
    {
23
        $this->className = $className;
24
25
        parent::__construct($config);
26
    }
27
28
    public static function createFromAnnotatedClass(string $typeName, string $className, $annotatedObject, FieldsBuilderFactory $fieldsBuilderFactory, RecursiveTypeMapperInterface $recursiveTypeMapper): self
29
    {
30
        return new self($className, [
31
            'name' => $typeName,
32
            'fields' => function() use ($annotatedObject, $recursiveTypeMapper, $className, $fieldsBuilderFactory) {
33
                $parentClass = get_parent_class($className);
34
                $parentType = null;
35
                if ($parentClass !== false) {
36
                    if ($recursiveTypeMapper->canMapClassToType($parentClass)) {
37
                        $parentType = $recursiveTypeMapper->mapClassToType($parentClass, null);
38
                    }
39
                }
40
41
                $fieldProvider = $fieldsBuilderFactory->buildFieldsBuilder($recursiveTypeMapper);
42
                $fields = $fieldProvider->getFields($annotatedObject);
43
                if ($parentType !== null) {
44
                    $fields = $parentType->getFields() + $fields;
45
                }
46
                return $fields;
47
            },
48
            'interfaces' => function() use ($className, $recursiveTypeMapper) {
49
                return $recursiveTypeMapper->findInterfaces($className);
50
            }
51
        ]);
52
    }
53
54
    public function getMappedClassName(): string
55
    {
56
        return $this->className;
57
    }
58
}
59