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 delete(UuidInterface $id): void |
67
|
|
|
{ |
68
|
|
|
$questionEntity = $this->getEntityById($id); |
69
|
|
|
|
70
|
|
|
if ($questionEntity !== null) { |
71
|
|
|
$this->entityManager->merge($questionEntity); |
72
|
|
|
$this->entityManager->remove($questionEntity); |
73
|
|
|
$this->entityManager->flush(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
|
|
public function getById(UuidInterface $id): ?Question |
81
|
|
|
{ |
82
|
|
|
$questionEntity = $this->getEntityById($id); |
83
|
|
|
return $questionEntity ? $questionEntity->toQuestion() : null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public function getAll(): ?Questions |
90
|
|
|
{ |
91
|
|
|
/** @var QuestionEntity[] $questionEntities */ |
92
|
|
|
$questionEntities = $this->objectRepository->findAll(); |
93
|
|
|
|
94
|
|
|
if (empty($questionEntities)) { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return new Questions( |
99
|
|
|
...array_map( |
100
|
|
|
function (QuestionEntity $questionEntity) { |
101
|
|
|
return $questionEntity->toQuestion(); |
102
|
|
|
}, |
103
|
|
|
$questionEntities |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param UuidInterface $id |
110
|
|
|
* @return QuestionEntity |
111
|
|
|
*/ |
112
|
|
|
private function getEntityById(UuidInterface $id): ?QuestionEntity |
113
|
|
|
{ |
114
|
|
|
/** @var QuestionEntity|null $questionEntity */ |
115
|
|
|
$questionEntity = $this->objectRepository->findOneBy( |
116
|
|
|
[ |
117
|
|
|
'id' => $id, |
118
|
|
|
] |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
return $questionEntity; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|