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( |
|
|
|
|
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); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function receive(EnvelopeInterface $envelope): bool |
55
|
|
|
{ |
56
|
|
|
return $this->getSubscription()->receive($envelope); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getKey(): string |
60
|
|
|
{ |
61
|
|
|
return $this->getSubscription()->getKey(); |
|
|
|
|
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
|
|
|
|
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
Change visible in outer-scope