AbstractEnumType::getValues()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 18
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 22
ccs 19
cts 19
cp 1
crap 7
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Andi\GraphQL\Type;
6
7
use Andi\GraphQL\Definition\Field\EnumValueInterface;
8
use Andi\GraphQL\Definition\Type\EnumTypeInterface;
9
use Andi\GraphQL\Exception\CantResolveEnumTypeException;
10
use Andi\GraphQL\Field\EnumValue;
11
12
abstract class AbstractEnumType implements EnumTypeInterface
13
{
14
    protected string $name;
15
    protected string $description;
16
    protected iterable $values;
17
18 4
    public function getName(): string
19
    {
20 4
        return $this->name;
21
    }
22
23 4
    public function getDescription(): ?string
24
    {
25
        /** @psalm-suppress RedundantPropertyInitializationCheck */
26 4
        return $this->description ?? null;
27
    }
28
29 4
    public function getValues(): iterable
30
    {
31 4
        foreach ($this->values as $name => $value) {
32 4
            if ($value instanceof EnumValueInterface) {
33 1
                yield $value;
34 4
            } elseif (\is_array($value)) {
35 3
                $valueName = $value['name'] ?? $name;
36 3
                if (! \is_string($valueName)) {
37 1
                    throw new CantResolveEnumTypeException('Can\'t resolve EnumValue: wrong value configuration');
38
                }
39 2
                yield new EnumValue(
40 2
                    name: $valueName,
41 2
                    value: $value['value'] ?? $valueName,
42 2
                    description: $value['description'] ?? null,
43 2
                    deprecationReason: $value['deprecationReason'] ?? null,
44 2
                );
45 2
            } elseif (\is_string($name)) {
46 1
                yield new EnumValue(name: $name, value: $value);
47 2
            } elseif (\is_string($value)) {
48 1
                yield new EnumValue(name: $value, value: $value);
49
            } else {
50 1
                throw new CantResolveEnumTypeException('Can\'t resolve EnumValue: wrong value configuration');
51
            }
52
        }
53
    }
54
}
55