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\Repositories\Entities\CategoryEntity; |
9
|
|
|
use VSV\GVQ_API\Question\Repositories\Entities\QuestionEntity; |
10
|
|
|
|
11
|
|
|
class QuestionDoctrineRepository extends AbstractDoctrineRepository implements QuestionRepository |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @inheritdoc |
15
|
|
|
*/ |
16
|
|
|
public function getRepositoryName(): string |
17
|
|
|
{ |
18
|
|
|
return QuestionEntity::class; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Question $question |
23
|
|
|
*/ |
24
|
|
|
public function save(Question $question): void |
25
|
|
|
{ |
26
|
|
|
$questionEntity = QuestionEntity::fromQuestion($question); |
27
|
|
|
|
28
|
|
|
/** @var CategoryEntity $categoryEntity */ |
29
|
|
|
$categoryEntity = $this->entityManager->find( |
30
|
|
|
CategoryEntity::class, |
31
|
|
|
$questionEntity->getCategoryEntity()->getId() |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
if ($categoryEntity == null) { |
35
|
|
|
throw new InvalidArgumentException( |
36
|
|
|
'Category with id: ' . |
37
|
|
|
$questionEntity->getCategoryEntity()->getId() . |
38
|
|
|
' and name: ' . |
39
|
|
|
$questionEntity->getCategoryEntity()->getName() . |
40
|
|
|
' not found.' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$questionEntity->setCategoryEntity($categoryEntity); |
45
|
|
|
|
46
|
|
|
$this->entityManager->persist($questionEntity); |
47
|
|
|
$this->entityManager->flush(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Question $question |
52
|
|
|
*/ |
53
|
|
|
public function update(Question $question): void |
54
|
|
|
{ |
55
|
|
|
$this->entityManager->merge( |
56
|
|
|
QuestionEntity::fromQuestion($question) |
57
|
|
|
); |
58
|
|
|
$this->entityManager->flush(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param UuidInterface $id |
63
|
|
|
* @return null|Question |
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
|
|
|
|