Passed
Push — master ( 1d5f02...796f61 )
by Alan
05:53
created

TypeConverter   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 18

3 Methods

Rating   Name   Duplication   Size   Complexity  
B convertType() 0 26 11
A __construct() 0 4 1
A getResourceType() 0 18 6
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\GraphQl\Type;
15
16
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18
use GraphQL\Type\Definition\Type as GraphQLType;
19
use Symfony\Component\PropertyInfo\Type;
20
21
/**
22
 * Convert a built-in type to its GraphQL equivalent.
23
 *
24
 * @experimental
25
 *
26
 * @author Alan Poulain <[email protected]>
27
 */
28
final class TypeConverter implements TypeConverterInterface
29
{
30
    private $resourceMetadataFactory;
31
    private $typeBuilder;
32
33
    public function __construct(TypeBuilderInterface $typeBuilder, ResourceMetadataFactoryInterface $resourceMetadataFactory)
34
    {
35
        $this->resourceMetadataFactory = $resourceMetadataFactory;
36
        $this->typeBuilder = $typeBuilder;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function convertType(Type $type, bool $input, ?string $queryName, ?string $mutationName, string $resourceClass, ?string $property, int $depth)
43
    {
44
        switch ($type->getBuiltinType()) {
45
            case Type::BUILTIN_TYPE_BOOL:
46
                return GraphQLType::boolean();
47
            case Type::BUILTIN_TYPE_INT:
48
                return GraphQLType::int();
49
            case Type::BUILTIN_TYPE_FLOAT:
50
                return GraphQLType::float();
51
            case Type::BUILTIN_TYPE_STRING:
52
                return GraphQLType::string();
53
            case Type::BUILTIN_TYPE_ARRAY:
54
            case Type::BUILTIN_TYPE_ITERABLE:
55
                return 'Iterable';
56
            case Type::BUILTIN_TYPE_OBJECT:
57
                if ($input && $depth > 0) {
58
                    return GraphQLType::string();
59
                }
60
61
                if (is_a($type->getClassName(), \DateTimeInterface::class, true)) {
62
                    return GraphQLType::string();
63
                }
64
65
                return $this->getResourceType($type, $input, $queryName, $mutationName, $depth);
66
            default:
67
                return null;
68
        }
69
    }
70
71
    private function getResourceType(Type $type, bool $input, ?string $queryName, ?string $mutationName, int $depth): ?GraphQLType
72
    {
73
        $resourceClass = $this->typeBuilder->isCollection($type) && ($collectionValueType = $type->getCollectionValueType()) ? $collectionValueType->getClassName() : $type->getClassName();
74
        if (null === $resourceClass) {
75
            return null;
76
        }
77
78
        try {
79
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
80
            if ([] === ($resourceMetadata->getGraphql() ?? [])) {
81
                return null;
82
            }
83
        } catch (ResourceClassNotFoundException $e) {
84
            // Skip objects that are not resources for now
85
            return null;
86
        }
87
88
        return $this->typeBuilder->getResourceObjectType($resourceClass, $resourceMetadata, $input, $queryName, $mutationName, false, $depth);
89
    }
90
}
91