DoctrineOrmTypeGuesser   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
eloc 43
c 2
b 0
f 0
dl 0
loc 59
ccs 0
cts 49
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D guessType() 0 54 19
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Form\TypeGuesser;
12
13
use Doctrine\DBAL\Types\Type;
14
use Doctrine\ORM\Mapping\ClassMetadata;
15
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser as BaseDoctrineOrmTypeGuesser;
16
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
17
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
18
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
19
use Symfony\Component\Form\Extension\Core\Type\DateIntervalType;
20
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
21
use Symfony\Component\Form\Extension\Core\Type\NumberType;
22
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
23
use Symfony\Component\Form\Extension\Core\Type\TextType;
24
use Symfony\Component\Form\Extension\Core\Type\TimeType;
25
use Symfony\Component\Form\Guess\Guess;
26
use Symfony\Component\Form\Guess\TypeGuess;
27
use Ynlo\GraphQLBundle\Form\Type\GraphQLType;
28
use Ynlo\GraphQLBundle\Type\Types;
29
30
/**
31
 * DoctrineOrmTypeGuesser
32
 */
33
class DoctrineOrmTypeGuesser extends BaseDoctrineOrmTypeGuesser
34
{
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function guessType($class, $property)
39
    {
40
        if (!$ret = $this->getMetadata($class)) {
41
            return new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE);
42
        }
43
44
        /** @var ClassMetadata $metadata */
45
        list($metadata, $name) = $ret;
46
47
        if ($metadata->hasAssociation($property)) {
48
            $multiple = $metadata->isCollectionValuedAssociation($property);
49
            $mapping = $metadata->getAssociationMapping($property);
50
51
            return new TypeGuess(
52
                EntityType::class,
53
                [
54
                    'em' => $name,
55
                    'class' => $mapping['targetEntity'],
56
                    'multiple' => $multiple,
57
                ],
58
                Guess::HIGH_CONFIDENCE
59
            );
60
        }
61
62
        switch ($metadata->getTypeOfField($property)) {
63
            case Type::TARRAY:
64
                return new TypeGuess(CollectionType::class, [], Guess::MEDIUM_CONFIDENCE);
65
            case Type::BOOLEAN:
66
                return new TypeGuess(CheckboxType::class, [], Guess::HIGH_CONFIDENCE);
67
            case Type::DATETIME:
68
            case Type::DATETIMETZ:
69
                return new TypeGuess(TextType::class, ['graphql_type' => Types::DATETIME], Guess::HIGH_CONFIDENCE);
70
            case 'dateinterval':
71
                return new TypeGuess(DateIntervalType::class, [], Guess::HIGH_CONFIDENCE);
72
            case Type::DATE:
73
                return new TypeGuess(TextType::class, ['graphql_type' => Types::DATE], Guess::HIGH_CONFIDENCE);
74
            case Type::TIME:
75
                return new TypeGuess(TextType::class, ['graphql_type' => Types::TIME], Guess::HIGH_CONFIDENCE);
76
            case Type::DECIMAL:
77
            case Type::FLOAT:
78
                return new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE);
79
            case Type::INTEGER:
80
            case Type::BIGINT:
81
            case Type::SMALLINT:
82
                return new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE);
83
            case Type::STRING:
84
                return new TypeGuess(TextType::class, [], Guess::MEDIUM_CONFIDENCE);
85
            case Type::SIMPLE_ARRAY:
86
            case Type::JSON_ARRAY:
87
                return new TypeGuess(GraphQLType::class, ['graphql_type' => Types::listOf(Types::STRING) ], Guess::MEDIUM_CONFIDENCE);
88
            case Type::TEXT:
89
                return new TypeGuess(TextareaType::class, [], Guess::MEDIUM_CONFIDENCE);
90
            default:
91
                return new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE);
92
        }
93
    }
94
}
95