Passed
Pull Request — 2.4 (#2975)
by Han Hui
04:40
created

DispatchTrait::dispatch()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 16
rs 10
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\Messenger;
15
16
use Symfony\Component\Messenger\Envelope;
17
use Symfony\Component\Messenger\Exception\HandlerFailedException;
18
use Symfony\Component\Messenger\MessageBusInterface;
19
20
/**
21
 * @internal
22
 */
23
trait DispatchTrait
24
{
25
    /**
26
     * @var MessageBusInterface
27
     */
28
    private $messageBus;
29
30
    /**
31
     * @param object|Envelope $message
32
     */
33
    private function dispatch($message)
34
    {
35
        if (!class_exists(HandlerFailedException::class)) {
36
            return $this->messageBus->dispatch($message);
37
        }
38
39
        try {
40
            return $this->messageBus->dispatch($message);
41
        } catch (HandlerFailedException $e) {
42
            // unwrap the exception thrown in handler for Symfony Messenger >= 4.3
43
            while ($e instanceof HandlerFailedException) {
44
                /** @var \Throwable $e */
45
                $e = $e->getPrevious();
46
            }
47
48
            throw $e;
49
        }
50
    }
51
}
52