Passed
Push — master ( ec51ae...10f5e3 )
by Adrien
12:18
created

EntityIDType::createEntityID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine\Definition;
6
7
use Doctrine\ORM\EntityManager;
8
use GraphQL\Doctrine\Utils;
9
use GraphQL\Error\Error;
10
use GraphQL\Language\AST\IntValueNode;
11
use GraphQL\Language\AST\Node;
12
use GraphQL\Language\AST\StringValueNode;
13
use GraphQL\Type\Definition\ScalarType;
14
15
/**
16
 * A specialized ID type that allows to fetch entity from DB.
17
 */
18
final class EntityIDType extends ScalarType
19
{
20
    /**
21
     * @param class-string $className
22
     */
23 19
    public function __construct(private readonly EntityManager $entityManager, /**
24
     * The entity class name.
25
     */
26
    private readonly string $className, string $typeName)
27
    {
28 19
        $this->name = $typeName;
29 19
        $this->description = 'Automatically generated type to be used as input where an object of type `' . Utils::getTypeName($className) . '` is needed';
30
31 19
        parent::__construct();
32
    }
33
34
    /**
35
     * Serializes an internal value to include in a response.
36
     *
37
     * @param object $value
38
     */
39 1
    public function serialize($value): string
40
    {
41 1
        $id = $this->entityManager->getClassMetadata($this->className)->getIdentifierValues($value);
42
43
        // @phpstan-ignore-next-line
44 1
        return (string) reset($id);
45
    }
46
47
    /**
48
     * Parses an externally provided value (query variable) to use as an input.
49
     *
50
     * @param mixed $value
51
     */
52 4
    public function parseValue($value, ?array $variables = null): EntityID
53
    {
54 4
        if (!is_string($value) && !is_int($value)) {
55 1
            throw new Error('EntityID cannot represent value: ' . \GraphQL\Utils\Utils::printSafe($value));
56
        }
57
58 3
        return $this->createEntityID((string) $value);
59
    }
60
61
    /**
62
     * Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input.
63
     *
64
     * @param Node $valueNode
65
     */
66 4
    public function parseLiteral($valueNode, ?array $variables = null): EntityID
67
    {
68 4
        if ($valueNode instanceof StringValueNode || $valueNode instanceof IntValueNode) {
69 3
            return $this->createEntityID((string) $valueNode->value);
70
        }
71
72
        // Intentionally without message, as all information already in wrapped Exception
73 1
        throw new Error();
74
    }
75
76
    /**
77
     * Create EntityID to retrieve entity from DB later on.
78
     */
79 6
    private function createEntityID(string $id): EntityID
80
    {
81 6
        return new EntityID($this->entityManager, $this->className, $id);
82
    }
83
}
84