InputTypeGenerator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 12
dl 0
loc 46
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A mapFactoryMethod() 0 12 2
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers;
5
6
use function get_parent_class;
7
use GraphQL\Type\Definition\InputObjectType;
8
use GraphQL\Type\Definition\ObjectType;
9
use phpDocumentor\Reflection\Fqsen;
10
use phpDocumentor\Reflection\Types\Object_;
11
use ReflectionClass;
12
use ReflectionMethod;
13
use ReflectionType;
14
use TheCodingMachine\GraphQL\Controllers\Annotations\Type;
15
use TheCodingMachine\GraphQL\Controllers\Hydrators\HydratorInterface;
16
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface;
17
use TheCodingMachine\GraphQL\Controllers\Types\ResolvableInputObjectType;
18
19
/**
20
 * This class is in charge of creating Webonix InputTypes from Factory annotations.
21
 */
22
class InputTypeGenerator
23
{
24
    /**
25
     * @var FieldsBuilderFactory
26
     */
27
    private $fieldsBuilderFactory;
28
    /**
29
     * @var array<string, InputObjectType>
30
     */
31
    private $cache = [];
32
    /**
33
     * @var HydratorInterface
34
     */
35
    private $hydrator;
36
    /**
37
     * @var InputTypeUtils
38
     */
39
    private $inputTypeUtils;
40
41
    public function __construct(InputTypeUtils $inputTypeUtils,
42
                                FieldsBuilderFactory $fieldsBuilderFactory,
43
                                HydratorInterface $hydrator)
44
    {
45
        $this->inputTypeUtils = $inputTypeUtils;
46
        $this->fieldsBuilderFactory = $fieldsBuilderFactory;
47
        $this->hydrator = $hydrator;
48
    }
49
50
    /**
51
     * @param object $factory
52
     * @param string $methodName
53
     * @param RecursiveTypeMapperInterface $recursiveTypeMapper
54
     * @return InputObjectType
55
     */
56
    public function mapFactoryMethod($factory, string $methodName, RecursiveTypeMapperInterface $recursiveTypeMapper): InputObjectType
57
    {
58
        $method = new ReflectionMethod($factory, $methodName);
59
60
        [$inputName, $className] = $this->inputTypeUtils->getInputTypeNameAndClassName($method);
61
62
        if (!isset($this->cache[$inputName])) {
63
            // TODO: add comment argument.
64
            $this->cache[$inputName] = new ResolvableInputObjectType($inputName, $this->fieldsBuilderFactory, $recursiveTypeMapper, $factory, $methodName, $this->hydrator, null);
65
        }
66
67
        return $this->cache[$inputName];
68
    }
69
}
70