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

EntityID   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 53
rs 10
c 0
b 0
f 0
ccs 12
cts 12
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getEntity() 0 8 2
A getId() 0 3 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