Passed
Push — 1.x ( 774941...781dbf )
by Kevin
03:22 queued 01:44
created

InteractsWithMessenger   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A _resetMessengerTransports() 0 3 1
A messenger() 0 15 4
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
     * @after
17
     */
18
    final protected static function _resetMessengerTransports(): void
19
    {
20
        TestTransport::resetAll();
21
    }
22
23
    final protected function messenger(?string $transport = null): TestTransport
24
    {
25
        if (!$this instanceof KernelTestCase) {
26
            throw new \LogicException(\sprintf('The %s trait can only be used with %s.', __TRAIT__, KernelTestCase::class));
27
        }
28
29
        if (!self::$container) {
30
            self::bootKernel();
31
        }
32
33
        if (!self::$container->has(TestTransportRegistry::class)) {
34
            throw new \LogicException('Cannot access transport - is ZenstruckMessengerTestBundle enabled in your test environment?');
35
        }
36
37
        return self::$container->get(TestTransportRegistry::class)->get($transport);
38
    }
39
}
40