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

TypeConverter::convertType()   B

Complexity

Conditions 11
Paths 10

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 20
nc 10
nop 7
dl 0
loc 26
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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