-
-
Notifications
You must be signed in to change notification settings - Fork 964
Expand file tree
/
Copy pathDispatchTrait.php
More file actions
49 lines (40 loc) · 1.28 KB
/
DispatchTrait.php
File metadata and controls
49 lines (40 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Doctrine\Common\Messenger;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\MessageBusInterface;
/**
* @internal
*/
trait DispatchTrait
{
private ?MessageBusInterface $messageBus;
private function dispatch(object $message): Envelope
{
if (!$this->messageBus instanceof MessageBusInterface) {
throw new \InvalidArgumentException('The message bus is not set.');
}
if (!class_exists(HandlerFailedException::class)) {
return $this->messageBus->dispatch($message);
}
try {
return $this->messageBus->dispatch($message);
} catch (HandlerFailedException $e) {
// unwrap the exception thrown in handler for Symfony Messenger >= 4.3
while ($e instanceof HandlerFailedException) {
/** @var \Throwable $e */
$e = $e->getPrevious();
}
throw $e;
}
}
}