Completed
Pull Request — master (#78)
by David
02:26
created

createFromAnnotatedClass()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 22
rs 9.7666
c 0
b 0
f 0
cc 4
nc 1
nop 5
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