Chatter.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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;
  11. use Symfony\Component\EventDispatcher\Event;
  12. use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
  13. use Symfony\Component\Messenger\MessageBusInterface;
  14. use Symfony\Component\Notifier\Event\MessageEvent;
  15. use Symfony\Component\Notifier\Message\MessageInterface;
  16. use Symfony\Component\Notifier\Transport\TransportInterface;
  17. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  18. /**
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. *
  21. * @experimental in 5.1
  22. */
  23. final class Chatter implements ChatterInterface
  24. {
  25. private $transport;
  26. private $bus;
  27. private $dispatcher;
  28. public function __construct(TransportInterface $transport, MessageBusInterface $bus = null, EventDispatcherInterface $dispatcher = null)
  29. {
  30. $this->transport = $transport;
  31. $this->bus = $bus;
  32. $this->dispatcher = class_exists(Event::class) ? LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher;
  33. }
  34. public function __toString(): string
  35. {
  36. return 'chat';
  37. }
  38. public function supports(MessageInterface $message): bool
  39. {
  40. return $this->transport->supports($message);
  41. }
  42. public function send(MessageInterface $message): void
  43. {
  44. if (null === $this->bus) {
  45. $this->transport->send($message);
  46. return;
  47. }
  48. if (null !== $this->dispatcher) {
  49. $this->dispatcher->dispatch(new MessageEvent($message, true));
  50. }
  51. $this->bus->dispatch($message);
  52. }
  53. }