EntityID::getEntity()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
ccs 5
cts 5
cp 1
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine\Definition;
6
7
use Doctrine\ORM\EntityManager;
8
use GraphQL\Error\UserError;
9
10
/**
11
 * An object used to fetch the entity from DB on demand.
12
 *
13
 * @template T of object
14
 */
15
class EntityID
16
{
17
    /**
18
     * @param class-string<T> $className the entity class name
19
     * @param null|string $id the entity id
20
     */
21 6
    public function __construct(
22
        private readonly EntityManager $entityManager,
23
        private readonly string $className,
24
        private readonly ?string $id,
25 6
    ) {}
26
27
    /**
28
     * Get the ID.
29
     */
30 2
    public function getId(): ?string
31
    {
32 2
        return $this->id;
33
    }
34
35
    /**
36
     * Get the entity from DB.
37
     *
38
     * @return T entity
39
     */
40 4
    public function getEntity(): object
41
    {
42 4
        $entity = $this->entityManager->getRepository($this->className)->find($this->id);
43 4
        if (!$entity) {
44 2
            throw new UserError('Entity not found for class `' . $this->className . '` and ID `' . $this->id . '`.');
45
        }
46
47 2
        return $entity;
48
    }
49
}
50