Skip to content

Commit

Permalink
feat(feedback): Send a LINE Notify message when there is a feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
pan93412 committed Oct 6, 2024
1 parent b82f076 commit 987880b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/EventSubscriber/FeedbackCreatedListenerSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace App\EventSubscriber;

use App\Controller\Admin\FeedbackCrudController;
use App\Entity\Feedback;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Events;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\NotifierInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

#[AsEntityListener(event: Events::postPersist, method: 'onFeedbackCreated', entity: Feedback::class)]
final readonly class FeedbackCreatedListenerSubscriber
{
public function __construct(
private NotifierInterface $notifier,
private TranslatorInterface $translator,
private AdminUrlGenerator $adminUrlGenerator,
) {
}

public function onFeedbackCreated(Feedback $feedback): void
{
// FIXME: only optimized for LINE Notify

$notificationContent = $this->translator->trans('notification.on-feedback-created.content', [
'%id%' => $feedback->getId(),
'%account%' => $feedback->getSender()?->getUserIdentifier()
?? $this->translator->trans('notification.on-feedback-created.anonymous'),
'%subject%' => $feedback->getTitle(),
'%link%' => $this->adminUrlGenerator->unsetAll()
->setController(FeedbackCrudController::class)
->setAction(Action::DETAIL)
->setEntityId($feedback->getId())
->generateUrl(),
]);

$this->notifier->send((new Notification($notificationContent))->channels(['chat/linenotify']));
}
}
8 changes: 8 additions & 0 deletions translations/messages.zh_TW.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,11 @@ feedback:
closed: 已關閉

marked: 已經將選擇的回饋標記為「%status%」。

notification:
on-feedback-created:
content: |
使用者 %account% 提交了一份意見回饋,主旨是「%subject%」。
閱讀意見回饋 → %link%
anonymous: <匿名>

0 comments on commit 987880b

Please sign in to comment.