EntityID   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
dl 0
loc 33
rs 10
c 1
b 0
f 0
eloc 6
ccs 9
cts 9
cp 1

3 Methods

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