EnumType::getValueLookup()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 2
nop 0
crap 3
1
<?php
2
3
namespace Fubhy\GraphQL\Type\Definition\Types;
4
5
use Fubhy\GraphQL\Language\Node;
6
use Fubhy\GraphQL\Language\Node\EnumValue;
7
use Fubhy\GraphQL\Type\Definition\EnumValueDefinition;
8
9
/**
10
 * Enum Type Definition.
11
 *
12
 * Some leaf values of requests and input values are Enums. GraphQL serializes
13
 * Enum values as strings, however internally Enums can be represented by any
14
 * kind of type, often integers.
15
 *
16
 * Note: If a value is not provided in a definition, the name of the enum value
17
 * will be used as it's internal value.
18
 */
19
class EnumType extends Type implements InputTypeInterface, OutputTypeInterface, LeafTypeInterface, NullableTypeInterface, UnmodifiedTypeInterface
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $values;
25
26
    /**
27
     * @var \Fubhy\GraphQL\Type\Definition\EnumValueDefinition[]
28
     */
29
    protected $valueMap;
30
31
    /**
32
     * @var \Fubhy\GraphQL\Type\Definition\EnumValueDefinition[]
33
     */
34
    protected $valueLookup;
35
36
    /**
37
     * @var \Fubhy\GraphQL\Type\Definition\EnumValueDefinition[]
38
     */
39
    protected $nameLookup;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param string $name
45
     * @param array $values
46
     * @param string|null $description
47
     */
48 75
    public function __construct($name, array $values = [], $description = NULL)
49
    {
50 75
        parent::__construct($name, $description);
51
52 75
        $this->values = $values;
53 75
    }
54
55
    /**
56
     * @return \Fubhy\GraphQL\Type\Definition\EnumValueDefinition[]
57
     */
58 18
    public function getValues()
59
    {
60 18
        if (!isset($this->valueMap)) {
61 12
            $this->valueMap = [];
62 12
            foreach ($this->values as $name => $value) {
63 12
                $value['name'] = $name;
64
65 12
                if (!array_key_exists('value', $value)) {
66
                    $value['value'] = $name;
67
                }
68
69 12
                $this->valueMap[$name] = new EnumValueDefinition($value);
70 12
            }
71 12
        }
72
73 18
        return $this->valueMap;
74
    }
75
76
    /**
77
     * @param mixed $value
78
     *
79
     * @return string|null
80
     */
81 27
    public function coerce($value)
82
    {
83 27
        $enumValue = $this->getValueLookup()[$value];
84 27
        return $enumValue ? $enumValue->getName() : NULL;
85
    }
86
87
    /**
88
     * @param \Fubhy\GraphQL\Language\Node $value
89
     *
90
     * @return string|null
91
     */
92
    public function coerceLiteral(Node $value)
93
    {
94
        if ($value instanceof EnumValue) {
95
            $key = $value->get('value');
96
            if (($lookup = $this->getNameLookup()) && isset($lookup[$key])) {
97
                return $lookup[$key] ? $lookup[$key]->getName() : NULL;
98
            }
99
        }
100
101
        return NULL;
102
    }
103
104
    /**
105
     * @return \Fubhy\GraphQL\Type\Definition\EnumValueDefinition[]
106
     */
107 27 View Code Duplication
    protected function getValueLookup()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109 27
        if (!isset($this->valueLookup)) {
110 6
            $this->valueLookup = [];
111 6
            foreach ($this->getValues() as $value) {
112 6
                $this->valueLookup[$value->getValue()] = $value;
113 6
            }
114 6
        }
115
116 27
        return $this->valueLookup;
117
    }
118
119
    /**
120
     * @return \Fubhy\GraphQL\Type\Definition\EnumValueDefinition[]
121
     */
122 View Code Duplication
    protected function getNameLookup()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        if (!isset($this->nameLookup)) {
125
            $this->nameLookup = [];
126
            foreach ($this->getValues() as $value) {
127
                $this->nameLookup[$value->getName()] = $value;
128
            }
129
        }
130
131
        return $this->nameLookup;
132
    }
133
}
134