Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.20% |
46 / 51 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| CreateNewsEndpoint | |
90.20% |
46 / 51 |
|
0.00% |
0 / 1 |
9.08 | |
0.00% |
0 / 1 |
| handle | |
90.20% |
46 / 51 |
|
0.00% |
0 / 1 |
9.08 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\News\Endpoints; |
| 4 | |
| 5 | use Olz\Api\OlzCreateEntityTypedEndpoint; |
| 6 | use Olz\Entity\News\NewsEntry; |
| 7 | use Olz\Entity\Users\User; |
| 8 | use Symfony\Component\Mime\Email; |
| 9 | |
| 10 | /** |
| 11 | * @phpstan-import-type OlzNewsId from NewsEndpointTrait |
| 12 | * @phpstan-import-type OlzNewsData from NewsEndpointTrait |
| 13 | * |
| 14 | * TODO: Those should not be necessary! |
| 15 | * @phpstan-import-type OlzNewsFormat from NewsEndpointTrait |
| 16 | * |
| 17 | * @extends OlzCreateEntityTypedEndpoint<OlzNewsId, OlzNewsData, array{ |
| 18 | * captchaToken?: ?non-empty-string, |
| 19 | * }, array{ |
| 20 | * status: 'OK'|'DENIED'|'ERROR', |
| 21 | * }> |
| 22 | */ |
| 23 | class CreateNewsEndpoint extends OlzCreateEntityTypedEndpoint { |
| 24 | use NewsEndpointTrait; |
| 25 | |
| 26 | protected function handle(mixed $input): mixed { |
| 27 | $input_data = $input['data']; |
| 28 | $format = $input_data['format']; |
| 29 | |
| 30 | if ($format !== 'anonymous') { |
| 31 | $this->checkPermission('any'); |
| 32 | } |
| 33 | if ($format === 'kaderblog') { |
| 34 | $this->checkPermission('kaderblog'); |
| 35 | } |
| 36 | if ($format === 'aktuell') { |
| 37 | $this->checkIsStaff(); |
| 38 | } |
| 39 | |
| 40 | $token = $input['custom']['captchaToken'] ?? null; |
| 41 | $is_valid_token = $token ? $this->captchaUtils()->validateToken($token) : false; |
| 42 | if ($format === 'anonymous' && !$is_valid_token) { |
| 43 | return ['custom' => ['status' => 'DENIED'], 'id' => null]; |
| 44 | } |
| 45 | |
| 46 | $news_entry = new NewsEntry(); |
| 47 | $this->entityUtils()->createOlzEntity($news_entry, $input['meta']); |
| 48 | $this->updateEntityWithData($news_entry, $input['data']); |
| 49 | |
| 50 | $this->entityManager()->persist($news_entry); |
| 51 | $this->entityManager()->flush(); |
| 52 | $this->persistUploads($news_entry, $input['data']); |
| 53 | |
| 54 | if ($format === 'anonymous') { |
| 55 | $anonymous_user = new User(); |
| 56 | $anonymous_user->setEmail($input_data['authorEmail'] ?? null); |
| 57 | $anonymous_user->setFirstName($input_data['authorName'] ?? '-'); |
| 58 | $anonymous_user->setLastName(''); |
| 59 | |
| 60 | $delete_news_token = urlencode($this->emailUtils()->encryptEmailReactionToken([ |
| 61 | 'action' => 'delete_news', |
| 62 | 'news_id' => $news_entry->getId(), |
| 63 | ])); |
| 64 | $base_url = $this->envUtils()->getBaseHref(); |
| 65 | $code_href = $this->envUtils()->getCodeHref(); |
| 66 | $news_url = "{$base_url}{$code_href}news/{$news_entry->getId()}"; |
| 67 | $delete_news_url = "{$base_url}{$code_href}email_reaktion?token={$delete_news_token}"; |
| 68 | $text = <<<ZZZZZZZZZZ |
| 69 | Hallo {$anonymous_user->getFirstName()}, |
| 70 | |
| 71 | Du hast soeben auf [{$base_url}]({$base_url}) einen [anonymen Forumseintrag]({$news_url}) erstellt. |
| 72 | |
| 73 | Falls du deinen Eintrag wieder *löschen* willst, klicke [hier]({$delete_news_url}) oder auf folgenden Link: |
| 74 | |
| 75 | {$delete_news_url} |
| 76 | |
| 77 | ZZZZZZZZZZ; |
| 78 | $config = [ |
| 79 | 'no_unsubscribe' => true, |
| 80 | ]; |
| 81 | |
| 82 | try { |
| 83 | $email = (new Email())->subject("[OLZ] Dein Forumseintrag"); |
| 84 | $email = $this->emailUtils()->buildOlzEmail($email, $anonymous_user, $text, $config); |
| 85 | $this->emailUtils()->send($email); |
| 86 | $this->log()->info("Forumseintrag email sent to {$anonymous_user->getEmail()}."); |
| 87 | } catch (\Exception $exc) { |
| 88 | $message = $exc->getMessage(); |
| 89 | $this->log()->critical("Error sending Forumseintrag email to {$anonymous_user->getEmail()}.: {$message}"); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return [ |
| 94 | 'custom' => ['status' => 'OK'], |
| 95 | 'id' => $news_entry->getId(), |
| 96 | ]; |
| 97 | } |
| 98 | } |