Passed
Push — master ( 328ed1...c52d79 )
by Thorsten
02:20
created

LazySubscription::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of the daikon-cqrs/message-bus project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Daikon\MessageBus\Channel\Subscription;
12
13
use Daikon\MessageBus\Channel\Subscription\MessageHandler\MessageHandlerList;
14
use Daikon\MessageBus\EnvelopeInterface;
15
use Daikon\MessageBus\MessageBusInterface;
16
use Daikon\MessageBus\Metadata\CallbackMetadataEnricher;
17
use Daikon\MessageBus\Metadata\Metadata;
18
use Daikon\MessageBus\Metadata\MetadataEnricherList;
19
20
final class LazySubscription implements SubscriptionInterface
21
{
22
    private $compositeSubscription;
23
24
    private $factoryCallback;
25
26
    public function __construct(
27
        string $key,
28
        callable $transport,
29
        callable $messageHandlers,
30
        callable $guard = null,
31
        MetadataEnricherList $metadataEnrichers = null
32
    ) {
33
        $this->factoryCallback = function () use ($key, $transport, $messageHandlers, $guard, $metadataEnrichers) {
34
            $metadataEnrichers = $metadataEnrichers->prepend(
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $metadataEnrichers, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
35
                new CallbackMetadataEnricher(function (Metadata $metadata): Metadata {
36
                    return $metadata->with(self::METADATA_KEY, $this->getKey());
37
                })
38
            );
39
            return new Subscription(
40
                $key,
41
                $transport(),
42
                $messageHandlers(),
43
                $guard,
44
                $metadataEnrichers
45
            );
46
        };
47
    }
48
49
    public function publish(EnvelopeInterface $envelope, MessageBusInterface $messageBus): bool
50
    {
51
        return $this->getSubscription()->publish($envelope);
0 ignored issues
show
Bug introduced by
The method publish() does not seem to exist on object<Daikon\MessageBus...ler\MessageHandlerList>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
    }
53
54
    public function receive(EnvelopeInterface $envelope): bool
55
    {
56
        return $this->getSubscription()->receive($envelope);
0 ignored issues
show
Bug introduced by
The method receive() does not seem to exist on object<Daikon\MessageBus...ler\MessageHandlerList>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
    }
58
59
    public function getKey(): string
60
    {
61
        return $this->getSubscription()->getKey();
0 ignored issues
show
Bug introduced by
The method getKey() does not seem to exist on object<Daikon\MessageBus...ler\MessageHandlerList>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
    }
63
64
    private function getSubscription(): MessageHandlerList
65
    {
66
        if (!$this->compositeSubscription) {
67
            $this->compositeSubscription = call_user_func($this->factoryCallback);
68
            $this->factoryCallback = null;
69
        }
70
        return $this->compositeSubscription;
71
    }
72
}
73