Passed
Push — 1.x ( 5710fc...1fcc19 )
by Kevin
06:17
created

InteractsWithMessenger   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

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

2 Methods

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