Completed
Push — feature/VSVGVQ-20 ( b9e4be...cc8b5a )
by Luc
05:56 queued 03:01
created

QuestionDoctrineRepository::getAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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