Passed
Push — feature/VSVGVQ-10 ( 87eafe )
by Luc
02:57
created

QuestionDoctrineRepository::getEntityById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 10
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 InvalidArgumentException;
6
use Ramsey\Uuid\UuidInterface;
7
use VSV\GVQ_API\Question\Models\Question;
8
use VSV\GVQ_API\Question\Models\Questions;
9
use VSV\GVQ_API\Question\Repositories\Entities\CategoryEntity;
10
use VSV\GVQ_API\Question\Repositories\Entities\QuestionEntity;
11
12
class QuestionDoctrineRepository extends AbstractDoctrineRepository implements QuestionRepository
13
{
14
    /**
15
     * @inheritdoc
16
     */
17
    public function getRepositoryName(): string
18
    {
19
        return QuestionEntity::class;
20
    }
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function save(Question $question): void
26
    {
27
        $questionEntity = QuestionEntity::fromQuestion($question);
28
29
        /** @var CategoryEntity $categoryEntity */
30
        $categoryEntity = $this->entityManager->find(
31
            CategoryEntity::class,
32
            $questionEntity->getCategoryEntity()->getId()
33
        );
34
35
        if ($categoryEntity == null) {
36
            throw new InvalidArgumentException(
37
                'Category with id: '.
38
                $questionEntity->getCategoryEntity()->getId().
39
                ' and name: '.
40
                $questionEntity->getCategoryEntity()->getName().
41
                ' not found.'
42
            );
43
        }
44
45
        $questionEntity->setCategoryEntity($categoryEntity);
46
47
        $this->entityManager->persist($questionEntity);
48
        $this->entityManager->flush();
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function update(Question $question): void
55
    {
56
        $this->entityManager->merge(
57
            QuestionEntity::fromQuestion($question)
58
        );
59
        $this->entityManager->flush();
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function delete(UuidInterface $id): void
66
    {
67
        $questionEntity = $this->getEntityById($id);
68
69
        if ($questionEntity !== null) {
70
            $this->entityManager->merge($questionEntity);
71
            $this->entityManager->remove($questionEntity);
72
            $this->entityManager->flush();
73
        }
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function getById(UuidInterface $id): ?Question
80
    {
81
        $questionEntity = $this->getEntityById($id);
82
        return $questionEntity ? $questionEntity->toQuestion() : null;
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function getAll(): ?Questions
89
    {
90
        /** @var QuestionEntity[] $questionEntities */
91
        $questionEntities = $this->objectRepository->findAll();
92
93
        if (empty($questionEntities)) {
94
            return null;
95
        }
96
97
        return new Questions(
98
            ...array_map(
99
                function (QuestionEntity $questionEntity) {
100
                    return $questionEntity->toQuestion();
101
                },
102
                $questionEntities
103
            )
104
        );
105
    }
106
107
    /**
108
     * @param UuidInterface $id
109
     * @return QuestionEntity
110
     */
111
    private function getEntityById(UuidInterface $id): ?QuestionEntity
112
    {
113
        /** @var QuestionEntity|null $questionEntity */
114
        $questionEntity = $this->objectRepository->findOneBy(
115
            [
116
                'id' => $id,
117
            ]
118
        );
119
120
        return $questionEntity;
121
    }
122
}
123