Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.14% |
34 / 35 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| GetAuthorInfoEndpoint | |
97.14% |
34 / 35 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
| handle | |
97.14% |
34 / 35 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\News\Endpoints; |
| 4 | |
| 5 | use Olz\Api\OlzTypedEndpoint; |
| 6 | use Olz\Entity\News\NewsEntry; |
| 7 | use PhpTypeScriptApi\HttpError; |
| 8 | |
| 9 | /** |
| 10 | * @phpstan-type OlzNewsId int |
| 11 | * @phpstan-type OlzAuthorInfoData array{ |
| 12 | * roleName?: ?non-empty-string, |
| 13 | * roleUsername?: ?non-empty-string, |
| 14 | * firstName: non-empty-string, |
| 15 | * lastName: string, |
| 16 | * email?: ?array<non-empty-string>, |
| 17 | * avatarImageId?: ?array<string, string>, |
| 18 | * } |
| 19 | * |
| 20 | * @extends OlzTypedEndpoint< |
| 21 | * array{id: OlzNewsId, captchaToken?: ?non-empty-string}, |
| 22 | * OlzAuthorInfoData |
| 23 | * > |
| 24 | */ |
| 25 | class GetAuthorInfoEndpoint extends OlzTypedEndpoint { |
| 26 | protected function handle(mixed $input): mixed { |
| 27 | $has_access = $this->authUtils()->hasPermission('any'); |
| 28 | $token = $input['captchaToken'] ?? null; |
| 29 | $is_valid_token = $token ? $this->captchaUtils()->validateToken($token) : false; |
| 30 | if (!$has_access && !$is_valid_token) { |
| 31 | throw new HttpError(403, 'Captcha token invalid'); |
| 32 | } |
| 33 | |
| 34 | $id = $input['id']; |
| 35 | $news_repo = $this->entityManager()->getRepository(NewsEntry::class); |
| 36 | $news_entry = $news_repo->findOneBy(['id' => $id]); |
| 37 | if (!$news_entry) { |
| 38 | throw new HttpError(404, "Nicht gefunden."); |
| 39 | } |
| 40 | $author_user = $news_entry->getAuthorUser(); |
| 41 | $author_role = $news_entry->getAuthorRole(); |
| 42 | $author_name = $news_entry->getAuthorName(); |
| 43 | $author_email = $news_entry->getAuthorEmail(); |
| 44 | |
| 45 | $first_name = $author_name ? $author_name : '-'; |
| 46 | $last_name = ''; |
| 47 | $email = $author_email; |
| 48 | $avatar = null; |
| 49 | if ($author_user) { |
| 50 | $first_name = $author_user->getFirstName(); |
| 51 | $last_name = $author_user->getLastName(); |
| 52 | $has_official_email = $this->authUtils()->hasPermission('user_email', $author_user); |
| 53 | $host = $this->envUtils()->getEmailForwardingHost(); |
| 54 | $email = $has_official_email |
| 55 | ? "{$author_user->getUsername()}@{$host}" |
| 56 | : ($author_user->getEmail() ? $author_user->getEmail() : null); |
| 57 | $avatar = $this->authUtils()->getUserAvatar($author_user); |
| 58 | } |
| 59 | |
| 60 | return [ |
| 61 | 'roleName' => $author_role?->getName() ?: null, |
| 62 | 'roleUsername' => $author_role?->getUsername() ?: null, |
| 63 | 'firstName' => $first_name ?: '-', |
| 64 | 'lastName' => $last_name, |
| 65 | 'email' => $this->emailUtils()->obfuscateEmail($email), |
| 66 | 'avatarImageId' => $avatar, |
| 67 | ]; |
| 68 | } |
| 69 | } |