TypeAnnotatedObjectType   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 23
dl 0
loc 47
rs 10
c 0
b 0
f 0

3 Methods

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