Passed
Push — master ( 9187bd...555ece )
by steven
03:07
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\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 getById(UuidInterface $id): ?Question
66
    {
67
        /** @var QuestionEntity|null $questionEntity */
68
        $questionEntity = $this->objectRepository->findOneBy(
69
            [
70
                'id' => $id,
71
            ]
72
        );
73
74
        return $questionEntity ? $questionEntity->toQuestion() : null;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function getAll(): ?Questions
81
    {
82
        /** @var QuestionEntity[] $questionEntities */
83
        $questionEntities = $this->objectRepository->findAll();
84
85
        if (empty($questionEntities)) {
86
            return null;
87
        }
88
89
        return new Questions(
90
            ...array_map(
91
                function (QuestionEntity $questionEntity) {
92
                    return $questionEntity->toQuestion();
93
                },
94
                $questionEntities
95
            )
96
        );
97
    }
98
}
99