Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
WeeklyPictureEndpointTrait | |
100.00% |
23 / 23 |
|
100.00% |
6 / 6 |
11 | |
100.00% |
1 / 1 |
configureWeeklyPictureEndpointTrait | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getEntityData | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
updateEntityWithData | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
persistUploads | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
editUploads | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
getEntityById | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Olz\Startseite\Endpoints; |
4 | |
5 | use Olz\Entity\Startseite\WeeklyPicture; |
6 | use Olz\Utils\WithUtilsTrait; |
7 | use PhpTypeScriptApi\HttpError; |
8 | use PhpTypeScriptApi\PhpStan\IsoDate; |
9 | |
10 | /** |
11 | * @phpstan-type OlzWeeklyPictureId int |
12 | * @phpstan-type OlzWeeklyPictureData array{ |
13 | * text: string, |
14 | * imageId: non-empty-string, |
15 | * publishedDate?: ?IsoDate, |
16 | * } |
17 | */ |
18 | trait WeeklyPictureEndpointTrait { |
19 | use WithUtilsTrait; |
20 | |
21 | public function configureWeeklyPictureEndpointTrait(): void { |
22 | $this->phpStanUtils->registerApiObject(IsoDate::class); |
23 | } |
24 | |
25 | /** @return OlzWeeklyPictureData */ |
26 | public function getEntityData(WeeklyPicture $entity): array { |
27 | return [ |
28 | 'text' => $entity->getText() ?? '', |
29 | 'imageId' => $entity->getImageId() ? $entity->getImageId() : '-', |
30 | 'publishedDate' => IsoDate::fromDateTime($entity->getPublishedDate()), |
31 | ]; |
32 | } |
33 | |
34 | /** @param OlzWeeklyPictureData $input_data */ |
35 | public function updateEntityWithData(WeeklyPicture $entity, array $input_data): void { |
36 | $now = new \DateTime($this->dateUtils()->getIsoNow()); |
37 | $published_date = $input_data['publishedDate'] ?? $now; |
38 | |
39 | $valid_image_id = $this->uploadUtils()->getValidUploadId($input_data['imageId']); |
40 | if ($valid_image_id === null) { |
41 | throw new HttpError(400, "Kein gültiges Bild!"); |
42 | } |
43 | |
44 | $entity->setPublishedDate($published_date); |
45 | $entity->setText($input_data['text']); |
46 | $entity->setImageId($valid_image_id); |
47 | } |
48 | |
49 | public function persistUploads(WeeklyPicture $entity): void { |
50 | if ($entity->getImageId()) { |
51 | $this->persistOlzImages($entity, [$entity->getImageId()]); |
52 | } |
53 | } |
54 | |
55 | public function editUploads(WeeklyPicture $entity): void { |
56 | if ($entity->getImageId() !== null) { |
57 | $this->editOlzImages($entity, [$entity->getImageId()]); |
58 | } |
59 | } |
60 | |
61 | protected function getEntityById(int $id): WeeklyPicture { |
62 | $news_repo = $this->entityManager()->getRepository(WeeklyPicture::class); |
63 | $entity = $news_repo->findOneBy(['id' => $id]); |
64 | if (!$entity) { |
65 | throw new HttpError(404, "Nicht gefunden."); |
66 | } |
67 | return $entity; |
68 | } |
69 | } |