EntityID::getId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 1
nc 1
nop 0
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\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