Completed
Push — feature/VSVGVQ-7-Entities ( cc0453 )
by Luc
02:51
created

QuestionDoctrineRepository::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\Question\Repositories;
4
5
use Ramsey\Uuid\UuidInterface;
6
use VSV\GVQ_API\Question\Models\Question;
7
use VSV\GVQ_API\Question\Repositories\Entities\CategoryEntity;
8
use VSV\GVQ_API\Question\Repositories\Entities\QuestionEntity;
9
10
class QuestionDoctrineRepository extends AbstractDoctrineRepository implements QuestionRepository
11
{
12
    /**
13
     * @inheritdoc
14
     */
15
    public function getRepositoryName(): string
16
    {
17
        return QuestionEntity::class;
18
    }
19
20
    /**
21
     * @param Question $question
22
     */
23
    public function save(Question $question): void
24
    {
25
        $questionEntity = QuestionEntity::fromQuestion($question);
26
27
        /** @var CategoryEntity $categoryEntity */
28
        $categoryEntity = $this->entityManager->merge(
29
            $questionEntity->getCategoryEntity()
30
        );
31
        $questionEntity->setCategoryEntity($categoryEntity);
32
33
        $this->entityManager->persist($questionEntity);
34
        $this->entityManager->flush();
35
    }
36
37
    /**
38
     * @param Question $question
39
     */
40
    public function update(Question $question): void
41
    {
42
        $this->entityManager->merge(
43
            QuestionEntity::fromQuestion($question)
44
        );
45
        $this->entityManager->flush();
46
    }
47
48
    /**
49
     * @param UuidInterface $id
50
     * @return null|Question
51
     */
52
    public function getById(UuidInterface $id): ?Question
53
    {
54
        /** @var QuestionEntity|null $question */
55
        $questionEntity = $this->objectRepository->findOneBy(
56
            [
57
                'id' => $id,
58
            ]
59
        );
60
61
        return $questionEntity ? $questionEntity->toQuestion() : null;
62
    }
63
}
64