Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| WeeklyPictureEndpointTrait | |
0.00% |
0 / 22 |
|
0.00% |
0 / 5 |
110 | |
0.00% |
0 / 1 |
| getEntityData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| updateEntityWithData | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| persistUploads | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| editUploads | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getEntityById | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 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 | /** @return OlzWeeklyPictureData */ |
| 22 | public function getEntityData(WeeklyPicture $entity): array { |
| 23 | return [ |
| 24 | 'text' => $entity->getText() ?? '', |
| 25 | 'imageId' => $entity->getImageId() ? $entity->getImageId() : '-', |
| 26 | 'publishedDate' => IsoDate::fromDateTime($entity->getPublishedDate()), |
| 27 | ]; |
| 28 | } |
| 29 | |
| 30 | /** @param OlzWeeklyPictureData $input_data */ |
| 31 | public function updateEntityWithData(WeeklyPicture $entity, array $input_data): void { |
| 32 | $now = new \DateTime($this->dateUtils()->getIsoNow()); |
| 33 | $published_date = $input_data['publishedDate'] ?? $now; |
| 34 | |
| 35 | $valid_image_id = $this->uploadUtils()->getValidUploadId($input_data['imageId']); |
| 36 | if ($valid_image_id === null) { |
| 37 | throw new HttpError(400, "Kein gültiges Bild!"); |
| 38 | } |
| 39 | |
| 40 | $entity->setPublishedDate($published_date); |
| 41 | $entity->setText($input_data['text']); |
| 42 | $entity->setImageId($valid_image_id); |
| 43 | } |
| 44 | |
| 45 | public function persistUploads(WeeklyPicture $entity): void { |
| 46 | if ($entity->getImageId()) { |
| 47 | $this->persistOlzImages($entity, [$entity->getImageId()]); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function editUploads(WeeklyPicture $entity): void { |
| 52 | if ($entity->getImageId() !== null) { |
| 53 | $this->editOlzImages($entity, [$entity->getImageId()]); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | protected function getEntityById(int $id): WeeklyPicture { |
| 58 | $news_repo = $this->entityManager()->getRepository(WeeklyPicture::class); |
| 59 | $entity = $news_repo->findOneBy(['id' => $id]); |
| 60 | if (!$entity) { |
| 61 | throw new HttpError(404, "Nicht gefunden."); |
| 62 | } |
| 63 | return $entity; |
| 64 | } |
| 65 | } |