SyncTransport.php 2.0 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\Messenger\Transport\Sync;
  11. use Symfony\Component\Messenger\Envelope;
  12. use Symfony\Component\Messenger\Exception\InvalidArgumentException;
  13. use Symfony\Component\Messenger\MessageBusInterface;
  14. use Symfony\Component\Messenger\Stamp\ReceivedStamp;
  15. use Symfony\Component\Messenger\Stamp\SentStamp;
  16. use Symfony\Component\Messenger\Transport\TransportInterface;
  17. /**
  18. * Transport that immediately marks messages as received and dispatches for handling.
  19. *
  20. * @author Ryan Weaver <ryan@symfonycasts.com>
  21. */
  22. class SyncTransport implements TransportInterface
  23. {
  24. private $messageBus;
  25. public function __construct(MessageBusInterface $messageBus)
  26. {
  27. $this->messageBus = $messageBus;
  28. }
  29. public function get(): iterable
  30. {
  31. throw new InvalidArgumentException('You cannot receive messages from the Messenger SyncTransport.');
  32. }
  33. public function stop(): void
  34. {
  35. throw new InvalidArgumentException('You cannot call stop() on the Messenger SyncTransport.');
  36. }
  37. public function ack(Envelope $envelope): void
  38. {
  39. throw new InvalidArgumentException('You cannot call ack() on the Messenger SyncTransport.');
  40. }
  41. public function reject(Envelope $envelope): void
  42. {
  43. throw new InvalidArgumentException('You cannot call reject() on the Messenger SyncTransport.');
  44. }
  45. public function send(Envelope $envelope): Envelope
  46. {
  47. /** @var SentStamp|null $sentStamp */
  48. $sentStamp = $envelope->last(SentStamp::class);
  49. $alias = null === $sentStamp ? 'sync' : ($sentStamp->getSenderAlias() ?: $sentStamp->getSenderClass());
  50. $envelope = $envelope->with(new ReceivedStamp($alias));
  51. return $this->messageBus->dispatch($envelope);
  52. }
  53. }