Passed
Pull Request — master (#78)
by David
01:57
created

TypeAnnotatedObjectType   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 20
dl 0
loc 48
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
    /**
22
     * @param object $annotatedObject
23
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
24
     * @param Type $typeField
25
     */
26
    public function __construct(string $className, array $config)
27
    {
28
        $this->className = $className;
29
30
        parent::__construct($config);
31
    }
32
33
    public static function createFromAnnotatedClass(string $typeName, string $className, $annotatedObject, FieldsBuilderFactory $fieldsBuilderFactory, RecursiveTypeMapperInterface $recursiveTypeMapper): self
34
    {
35
        return new self($className, [
36
            'name' => $typeName,
37
            'fields' => function() use ($annotatedObject, $recursiveTypeMapper, $className, $fieldsBuilderFactory) {
38
                $parentClass = get_parent_class($className);
39
                $parentType = null;
40
                if ($parentClass !== false) {
41
                    if ($recursiveTypeMapper->canMapClassToType($parentClass)) {
42
                        $parentType = $recursiveTypeMapper->mapClassToType($parentClass, null);
43
                    }
44
                }
45
46
                $fieldProvider = $fieldsBuilderFactory->buildFieldsBuilder($recursiveTypeMapper);
47
                $fields = $fieldProvider->getFields($annotatedObject);
48
                if ($parentType !== null) {
49
                    $fields = $parentType->getFields() + $fields;
50
                }
51
                return $fields;
52
            },
53
            'interfaces' => function() use ($className, $recursiveTypeMapper) {
54
                return $recursiveTypeMapper->findInterfaces($className);
55
            }
56
        ]);
57
    }
58
59
    public function getMappedClassName(): string
60
    {
61
        return $this->className;
62
    }
63
}
64