InteractsWithMessenger   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 11
c 1
b 0
f 0
dl 0
loc 32
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A _resetMessengerTransports() 0 3 1
A messenger() 0 20 5
1
<?php
2
3
namespace Zenstruck\Messenger\Test;
4
5
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6
use Zenstruck\Messenger\Test\Transport\TestTransport;
7
use Zenstruck\Messenger\Test\Transport\TestTransportRegistry;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 */
12
trait InteractsWithMessenger
13
{
14
    /**
15
     * @internal
16
     * @before
17
     * @after
18
     */
19
    final protected static function _resetMessengerTransports(): void
20
    {
21
        TestTransport::resetAll();
22
    }
23
24
    final protected function messenger(?string $transport = null): TestTransport
25
    {
26
        if (!$this instanceof KernelTestCase) {
27
            throw new \LogicException(\sprintf('The %s trait can only be used with %s.', __TRAIT__, KernelTestCase::class));
28
        }
29
30
        if (!self::$booted) {
31
            self::bootKernel();
32
        }
33
34
        $container = \method_exists($this, 'getContainer') ? self::getContainer() : self::$container;
35
36
        if (!$container->has('zenstruck_messenger_test.transport_registry')) {
37
            throw new \LogicException('Cannot access transport - is ZenstruckMessengerTestBundle enabled in your test environment?');
38
        }
39
40
        /** @var TestTransportRegistry $registry */
41
        $registry = $container->get('zenstruck_messenger_test.transport_registry');
42
43
        return $registry->get($transport);
44
    }
45
}
46