Completed
Push — master ( 5eaa67...092141 )
by Adrien
02:45
created

EntityIDType::parseLiteral()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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 Doctrine\ORM\EntityRepository;
9
use GraphQL\Doctrine\Utils;
10
use GraphQL\Type\Definition\IDType;
11
12
/**
13
 * A specialized ID type that allows to fetch entity from DB
14
 */
15
class EntityIDType extends IDType
16
{
17
    /**
18
     * @var EntityManager
19
     */
20
    private $entityManager;
21
22
    /**
23
     * The entity class name
24
     *
25
     * @var string
26
     */
27
    private $className;
28
29 7
    public function __construct(EntityManager $entityManager, string $className)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
30
    {
31 7
        $this->entityManager = $entityManager;
32 7
        $this->className = $className;
33 7
        $this->name = Utils::getIDTypeName($className);
34 7
        $this->description = 'Automatically generated type to be used as input where an object of type `' . Utils::getTypeName($className) . '` is needed';
35
36 7
        parent::__construct();
37 7
    }
38
39
    /**
40
     * Serializes an internal value to include in a response.
41
     *
42
     * @param mixed $value
43
     *
44
     * @return string
45
     */
46 1
    public function serialize($value)
47
    {
48 1
        $id = $this->entityManager->getClassMetadata($this->className)->getIdentifierValues($value);
49
50 1
        return (string) reset($id);
51
    }
52
53
    /**
54
     * Parses an externally provided value (query variable) to use as an input
55
     *
56
     * @param mixed $value
57
     *
58
     * @return mixed A Doctrine entity
59
     */
60 1
    public function parseValue($value)
61
    {
62 1
        $value = parent::parseValue($value);
63
64 1
        return $this->getRepository()->find($value);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getRepository()->find($value); of type object|null adds the type object to the return on line 64 which is incompatible with the return type of the parent method GraphQL\Type\Definition\IDType::parseValue of type string.
Loading history...
65
    }
66
67
    /**
68
     * Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input
69
     *
70
     * @param \GraphQL\Language\AST\Node $valueNode
71
     *
72
     * @return mixed
73
     */
74 1
    public function parseLiteral($valueNode)
75
    {
76 1
        $value = parent::parseLiteral($valueNode);
77
78 1
        return $this->getRepository()->find($value);
1 ignored issue
show
Bug Compatibility introduced by
The expression $this->getRepository()->find($value); of type object|null adds the type object to the return on line 78 which is incompatible with the return type of the parent method GraphQL\Type\Definition\IDType::parseLiteral of type null|string.
Loading history...
79
    }
80
81
    /**
82
     * Get the repository for our entity
83
     *
84
     * @return EntityRepository
85
     */
86 2
    private function getRepository(): EntityRepository
87
    {
88 2
        return $this->entityManager->getRepository($this->className);
89
    }
90
}
91