Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateNewsEndpoint
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 configure
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 handle
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Olz\News\Endpoints;
4
5use Olz\Api\OlzUpdateEntityTypedEndpoint;
6use PhpTypeScriptApi\HttpError;
7
8/**
9 * @phpstan-import-type OlzNewsId from NewsEndpointTrait
10 * @phpstan-import-type OlzNewsData from NewsEndpointTrait
11 *
12 * TODO: Those should not be necessary!
13 * @phpstan-import-type OlzNewsFormat from NewsEndpointTrait
14 *
15 * @extends OlzUpdateEntityTypedEndpoint<OlzNewsId, OlzNewsData>
16 */
17class UpdateNewsEndpoint extends OlzUpdateEntityTypedEndpoint {
18    use NewsEndpointTrait;
19
20    public function configure(): void {
21        parent::configure();
22        $this->configureNewsEndpointTrait();
23        $this->phpStanUtils->registerTypeImport(NewsEndpointTrait::class);
24    }
25
26    protected function handle(mixed $input): mixed {
27        $this->checkPermission('any');
28
29        $entity = $this->getEntityById($input['id']);
30
31        if (!$this->entityUtils()->canUpdateOlzEntity($entity, $input['meta'], 'news')) {
32            throw new HttpError(403, "Kein Zugriff!");
33        }
34
35        $this->entityUtils()->updateOlzEntity($entity, $input['meta']);
36        $this->updateEntityWithData($entity, $input['data']);
37
38        $this->entityManager()->persist($entity);
39        $this->entityManager()->flush();
40        $this->persistUploads($entity, $input['data']);
41
42        return [
43            'id' => $entity->getId() ?? 0,
44        ];
45    }
46}