Completed
Push — master ( 5c6cf0...c4384e )
by Adrien
03:09
created

EntityID::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
eloc 3
nc 1
nop 3
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\Error\Error;
9
10
/**
11
 * A object used to fetch the entity from DB on demand
12
 */
13
class EntityID
14
{
15
    /**
16
     * @var EntityManager
17
     */
18
    private $entityManager;
19
20
    /**
21
     * The entity class name
22
     *
23
     * @var string
24
     */
25
    private $className;
26
27
    /**
28
     * The entity id
29
     *
30
     * @var null|string
31
     */
32
    private $id;
33
34 6
    public function __construct(EntityManager $entityManager, string $className, ?string $id)
35
    {
36 6
        $this->entityManager = $entityManager;
37 6
        $this->className = $className;
38 6
        $this->id = $id;
39 6
    }
40
41
    /**
42
     * Get the ID
43
     *
44
     * @return null|string
45
     */
46 2
    public function getId(): ?string
47
    {
48 2
        return $this->id;
49
    }
50
51
    /**
52
     * Get the entity from DB
53
     *
54
     * @throws Error
55
     *
56
     * @return mixed entity
57
     */
58 4
    public function getEntity()
59
    {
60 4
        $entity = $this->entityManager->getRepository($this->className)->find($this->id);
61 4
        if (!$entity) {
62 2
            throw new Error('Entity not found for class `' . $this->className . '` and ID `' . $this->id . '`.');
63
        }
64
65 2
        return $entity;
66
    }
67
}
68