Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| SnippetEndpointTrait | |
0.00% |
0 / 24 |
|
0.00% |
0 / 5 |
56 | |
0.00% |
0 / 1 |
| getEntityData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| updateEntityWithData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| persistUploads | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| editUploads | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getEntityById | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Snippets\Endpoints; |
| 4 | |
| 5 | use Olz\Entity\Snippets\Snippet; |
| 6 | use Olz\Utils\WithUtilsTrait; |
| 7 | use PhpTypeScriptApi\HttpError; |
| 8 | |
| 9 | /** |
| 10 | * @phpstan-type OlzSnippetId int |
| 11 | * @phpstan-type OlzSnippetData array{ |
| 12 | * text: string, |
| 13 | * imageIds: array<non-empty-string>, |
| 14 | * fileIds: array<non-empty-string>, |
| 15 | * } |
| 16 | */ |
| 17 | trait SnippetEndpointTrait { |
| 18 | use WithUtilsTrait; |
| 19 | |
| 20 | /** @return OlzSnippetData */ |
| 21 | public function getEntityData(Snippet $entity): array { |
| 22 | return [ |
| 23 | 'text' => $entity->getText() ?? '', |
| 24 | 'imageIds' => $entity->getStoredImageUploadIds(), |
| 25 | 'fileIds' => $entity->getStoredFileUploadIds(), |
| 26 | ]; |
| 27 | } |
| 28 | |
| 29 | /** @param OlzSnippetData $input_data */ |
| 30 | public function updateEntityWithData(Snippet $entity, array $input_data): void { |
| 31 | $entity->setText($input_data['text']); |
| 32 | } |
| 33 | |
| 34 | /** @param OlzSnippetData $input_data */ |
| 35 | public function persistUploads(Snippet $entity, array $input_data): void { |
| 36 | $this->persistOlzImages($entity, $input_data['imageIds']); |
| 37 | $this->persistOlzFiles($entity, $input_data['fileIds']); |
| 38 | } |
| 39 | |
| 40 | public function editUploads(Snippet $entity): void { |
| 41 | $image_ids = $this->uploadUtils()->getStoredUploadIds("{$entity->getImagesPathForStorage()}img/"); |
| 42 | $this->editOlzImages($entity, $image_ids); |
| 43 | $this->editOlzFiles($entity); |
| 44 | } |
| 45 | |
| 46 | protected function getEntityById(int $id): Snippet { |
| 47 | $repo = $this->entityManager()->getRepository(Snippet::class); |
| 48 | $entity = $repo->findOneBy(['id' => $id]); |
| 49 | if (!$entity) { |
| 50 | $has_access = $this->authUtils()->hasPermission("snippet_{$id}"); |
| 51 | if (!$has_access) { |
| 52 | throw new HttpError(404, "Nicht gefunden."); |
| 53 | } |
| 54 | $entity = new Snippet(); |
| 55 | $this->entityUtils()->createOlzEntity($entity, ['onOff' => true]); |
| 56 | $entity->setId($id); |
| 57 | $entity->setText(''); |
| 58 | $this->entityManager()->persist($entity); |
| 59 | $this->entityManager()->flush(); |
| 60 | } |
| 61 | return $entity; |
| 62 | } |
| 63 | } |