1 | <?php declare(strict_types=1); |
||
20 | |||
21 | private Closure $factoryCallback; |
||
22 | |||
23 | public function __construct( |
||
24 | string $key, |
||
25 | Closure $transport, |
||
26 | Closure $messageHandlers, |
||
27 | Closure $guard = null, |
||
28 | Closure $metadataEnrichers = null |
||
29 | ) { |
||
30 | $this->key = $key; |
||
31 | $this->factoryCallback = fn(): SubscriptionInterface => |
||
|
|||
32 | new Subscription( |
||
33 | $this->key, |
||
34 | $transport(), |
||
35 | $messageHandlers(), |
||
36 | $guard, |
||
37 | $metadataEnrichers ? $metadataEnrichers() : null |
||
38 | ); |
||
39 | } |
||
40 | |||
41 | public function publish(EnvelopeInterface $envelope, MessageBusInterface $messageBus): void |
||
42 | { |
||
43 | if ($subscription = $this->getSubscription()) { |
||
44 | $subscription->publish($envelope, $messageBus); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | public function receive(EnvelopeInterface $envelope): void |
||
49 | { |
||
50 | if ($subscription = $this->getSubscription()) { |
||
51 | $subscription->receive($envelope); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | public function getKey(): string |
||
56 | { |
||
57 | return $this->key; |
||
58 | } |
||
59 | |||
60 | private function getSubscription(): SubscriptionInterface |
||
61 | { |
||
62 | /** @psalm-suppress TypeDoesNotContainType */ |
||
63 | if (!isset($this->compositeSubscription)) { |
||
64 | $this->compositeSubscription = call_user_func($this->factoryCallback); |
||
65 | unset($this->factoryCallback); |
||
66 | } |
||
67 | return $this->compositeSubscription; |
||
68 | } |
||
69 | } |
||
70 |