Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
27 / 27 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| QuestionEndpointTrait | |
100.00% |
27 / 27 |
|
100.00% |
5 / 5 |
9 | |
100.00% |
1 / 1 |
| getEntityData | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| updateEntityWithData | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| persistUploads | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| editUploads | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getEntityById | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Faq\Endpoints; |
| 4 | |
| 5 | use Olz\Entity\Faq\Question; |
| 6 | use Olz\Entity\Faq\QuestionCategory; |
| 7 | use Olz\Utils\WithUtilsTrait; |
| 8 | use PhpTypeScriptApi\HttpError; |
| 9 | |
| 10 | /** |
| 11 | * @phpstan-type OlzQuestionId int |
| 12 | * @phpstan-type OlzQuestionData array{ |
| 13 | * ident: non-empty-string, |
| 14 | * question: non-empty-string, |
| 15 | * categoryId?: ?int, |
| 16 | * positionWithinCategory?: ?float, |
| 17 | * answer: non-empty-string, |
| 18 | * imageIds: array<non-empty-string>, |
| 19 | * fileIds: array<non-empty-string>, |
| 20 | * } |
| 21 | */ |
| 22 | trait QuestionEndpointTrait { |
| 23 | use WithUtilsTrait; |
| 24 | |
| 25 | /** @return OlzQuestionData */ |
| 26 | public function getEntityData(Question $entity): array { |
| 27 | return [ |
| 28 | 'ident' => $entity->getIdent() ? $entity->getIdent() : '-', |
| 29 | 'question' => $entity->getQuestion() ? $entity->getQuestion() : '-', |
| 30 | 'categoryId' => $entity->getCategory()?->getId(), |
| 31 | 'positionWithinCategory' => $entity->getPositionWithinCategory(), |
| 32 | 'answer' => $entity->getAnswer() ? $entity->getAnswer() : '-', |
| 33 | 'imageIds' => $entity->getStoredImageUploadIds(), |
| 34 | 'fileIds' => $entity->getStoredFileUploadIds(), |
| 35 | ]; |
| 36 | } |
| 37 | |
| 38 | /** @param OlzQuestionData $input_data */ |
| 39 | public function updateEntityWithData(Question $entity, array $input_data): void { |
| 40 | $category_repo = $this->entityManager()->getRepository(QuestionCategory::class); |
| 41 | $category_id = $input_data['categoryId'] ?? null; |
| 42 | $category = $category_repo->findOneBy(['id' => $category_id]); |
| 43 | |
| 44 | $entity->setIdent($input_data['ident']); |
| 45 | $entity->setQuestion($input_data['question']); |
| 46 | $entity->setCategory($category); |
| 47 | $entity->setPositionWithinCategory($input_data['positionWithinCategory'] ?? 0); |
| 48 | $entity->setAnswer($input_data['answer']); |
| 49 | } |
| 50 | |
| 51 | /** @param OlzQuestionData $input_data */ |
| 52 | public function persistUploads(Question $entity, array $input_data): void { |
| 53 | $this->persistOlzImages($entity, $input_data['imageIds']); |
| 54 | $this->persistOlzFiles($entity, $input_data['fileIds']); |
| 55 | } |
| 56 | |
| 57 | public function editUploads(Question $entity): void { |
| 58 | $image_ids = $this->uploadUtils()->getStoredUploadIds("{$entity->getImagesPathForStorage()}img/"); |
| 59 | $this->editOlzImages($entity, $image_ids); |
| 60 | $this->editOlzFiles($entity); |
| 61 | } |
| 62 | |
| 63 | protected function getEntityById(int $id): Question { |
| 64 | $repo = $this->entityManager()->getRepository(Question::class); |
| 65 | $entity = $repo->findOneBy(['id' => $id]); |
| 66 | if (!$entity) { |
| 67 | throw new HttpError(404, "Nicht gefunden."); |
| 68 | } |
| 69 | return $entity; |
| 70 | } |
| 71 | } |