Completed
Push — master ( 6fe909...aff65e )
by Adrien
01:54
created

EntityIDType::serialize()   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
     * @var string
25
     */
26
    private $className;
27
28 4
    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...
29
    {
30 4
        $this->entityManager = $entityManager;
31 4
        $this->className = $className;
32 4
        $this->name = Utils::getIDTypeName($className);
33 4
        $this->description = 'Automatically generated type to be used as input where an object of type `' . Utils::getTypeName($className) . '` is needed';
34
35 4
        parent::__construct();
36 4
    }
37
38
    /**
39
     * Serializes an internal value to include in a response.
40
     *
41
     * @param mixed $value
42
     * @return mixed
43
     */
44 1
    public function serialize($value)
45
    {
46 1
        $id = $this->entityManager->getClassMetadata($this->className)->getIdentifierValues($value);
47
48 1
        return (string) reset($id);
49
    }
50
51
    /**
52
     * Parses an externally provided value (query variable) to use as an input
53
     *
54
     * @param mixed $value
55
     * @return mixed A Doctrine entity
56
     */
57 1
    public function parseValue($value)
58
    {
59 1
        $value = parent::parseValue($value);
60
61 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 61 which is incompatible with the return type of the parent method GraphQL\Type\Definition\IDType::parseValue of type string.
Loading history...
62
    }
63
64
    /**
65
     * Get the repository for our entity
66
     * @return EntityRepository
67
     */
68 1
    private function getRepository(): EntityRepository
69
    {
70 1
        return $this->entityManager->getRepository($this->className);
71
    }
72
}
73