Passed
Push — master ( e52344...df819b )
by Rafael
09:21
created

DoctrineEnumLoader::loadDefinitions()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 9.0101

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
ccs 19
cts 20
cp 0.95
rs 4.909
cc 9
eloc 19
nc 6
nop 1
crap 9.0101
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\Definition\Loader;
12
13
use Doctrine\Bundle\DoctrineBundle\Registry;
14
use Doctrine\DBAL\Types\Type;
15
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType as Fresh_AbstractEnumType;
16
use Ynlo\GraphQLBundle\Definition\EnumDefinition;
17
use Ynlo\GraphQLBundle\Definition\EnumValueDefinition;
18
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint;
19
use Ynlo\GraphQLBundle\Doctrine\DBAL\Types\AbstractEnumType;
20
use Ynlo\GraphQLBundle\Util\TypeUtil;
21
22
/**
23
 * DoctrineEnumLoader
24
 */
25
class DoctrineEnumLoader implements DefinitionLoaderInterface
26
{
27
    /**
28
     * @var Registry
29
     */
30
    protected $registry;
31
32
    /**
33
     * @param Registry $registry
34
     */
35 1
    public function __construct(Registry $registry)
36
    {
37 1
        $this->registry = $registry;
38 1
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 1
    public function loadDefinitions(Endpoint $endpoint)
44
    {
45 1
        if (!class_exists('\Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType')) {
46
            return;
47
        }
48
49
        //registry connection should be called to
50
        //force doctrine load all registered DBAL types
51 1
        $this->registry->getConnection();
52 1
        $types = Type::getTypesMap();
53 1
        foreach ($types as $name => $class) {
54
            /** @var $class AbstractEnumType */
55 1
            if (is_subclass_of($class, Fresh_AbstractEnumType::class, true)) {
56 1
                $enum = new EnumDefinition();
57 1
                $enum->setName(TypeUtil::normalize($name));
58 1
                $enum->setClass($class);
59 1
                foreach ($class::getValues() as $value) {
60 1
                    $enumValue = new EnumValueDefinition();
61 1
                    $enumValue->setValue($value);
62 1
                    $enumValue->setName($value);
63
64 1
                    if (is_subclass_of($class, AbstractEnumType::class, true)) {
65 1
                        $enumValue->setName($class::getPublicName($value) ?: $value);
66 1
                        $enumValue->setDescription($class::getDescription($value) ?: null);
67 1
                        $enumValue->setDeprecationReason($class::getDeprecatedReason($value) ?: null);
68
                    }
69
70 1
                    $enum->addValue($enumValue);
71
                }
72
73 1
                $endpoint->addType($enum);
74
            }
75
        }
76 1
    }
77
}
78