BrowserChannel.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Notifier\Channel;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\Notifier\Notification\Notification;
  13. use Symfony\Component\Notifier\Recipient\Recipient;
  14. /**
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. *
  17. * @experimental in 5.1
  18. */
  19. final class BrowserChannel implements ChannelInterface
  20. {
  21. private $stack;
  22. public function __construct(RequestStack $stack)
  23. {
  24. $this->stack = $stack;
  25. }
  26. public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void
  27. {
  28. if (null === $request = $this->stack->getCurrentRequest()) {
  29. return;
  30. }
  31. $message = $notification->getSubject();
  32. if ($notification->getEmoji()) {
  33. $message = $notification->getEmoji().' '.$message;
  34. }
  35. $request->getSession()->getFlashBag()->add('notification', $message);
  36. }
  37. public function supports(Notification $notification, Recipient $recipient): bool
  38. {
  39. return true;
  40. }
  41. }