/
Channel.php
117 lines (104 loc) · 4.29 KB
/
Channel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php declare(strict_types=1);
/**
* This file is part of the daikon-cqrs/message-bus project.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Daikon\MessageBus\Channel;
use Closure;
use Daikon\MessageBus\EnvelopeInterface;
use Daikon\MessageBus\MessageBusInterface;
use Daikon\MessageBus\Channel\Subscription\SubscriptionInterface;
use Daikon\MessageBus\Channel\Subscription\SubscriptionMap;
use Daikon\MessageBus\Exception\EnvelopeNotAcceptable;
use Daikon\MessageBus\Exception\SubscriptionUnknown;
use Daikon\Metadata\MetadataInterface;
use Daikon\Metadata\MetadataEnricherInterface;
use Daikon\Metadata\MetadataEnricherList;
final class Channel implements ChannelInterface
{
private string $key;
private SubscriptionMap $subscriptions;
private Closure $guard;
private MetadataEnricherList $metadataEnrichers;
public function __construct(
string $key,
SubscriptionMap $subscriptions,
Closure $guard = null,
MetadataEnricherList $metadataEnrichers = null
) {
$this->key = $key;
$this->subscriptions = $subscriptions;
$this->guard = $guard ?? fn(): bool => true;
$metadataEnrichers = $metadataEnrichers ?? new MetadataEnricherList;
$this->metadataEnrichers = $metadataEnrichers->enrichWith(self::METADATA_KEY, $this->key);
}
public function publish(EnvelopeInterface $envelope, MessageBusInterface $messageBus): void
{
$envelope = $this->enrichMetadata($envelope);
if ($this->accepts($envelope)) {
foreach ($this->subscriptions as $subscription) {
$subscription->publish($envelope, $messageBus);
}
}
}
public function receive(EnvelopeInterface $envelope): void
{
$this->verify($envelope);
$subscriptionKey = (string)$envelope->getMetadata()->get(SubscriptionInterface::METADATA_KEY);
if (!$this->subscriptions->has($subscriptionKey)) {
throw new SubscriptionUnknown(
"Channel '{$this->key}' has no subscription '$subscriptionKey' and thus ".
"Envelope '{$envelope->getUuid()->toString()}' cannot be handled."
);
}
/** @var SubscriptionInterface $subscription */
$subscription = $this->subscriptions->get($subscriptionKey);
$subscription->receive($envelope);
}
public function getKey(): string
{
return $this->key;
}
private function enrichMetadata(EnvelopeInterface $envelope): EnvelopeInterface
{
return $envelope->withMetadata(array_reduce(
$this->metadataEnrichers->unwrap(),
function (MetadataInterface $metadata, MetadataEnricherInterface $metadataEnricher): MetadataInterface {
return $metadataEnricher->enrich($metadata);
},
$envelope->getMetadata()
));
}
private function accepts(EnvelopeInterface $envelope): bool
{
return (bool)($this->guard)($envelope);
}
private function verify(EnvelopeInterface $envelope): void
{
$metadata = $envelope->getMetadata();
if (!$metadata->has(self::METADATA_KEY)) {
throw new EnvelopeNotAcceptable(
"Channel key '".self::METADATA_KEY."' missing in metadata of Envelope ".
"'{$envelope->getUuid()->toString()}' received on channel '{$this->key}'.",
EnvelopeNotAcceptable::CHANNEL_KEY_MISSING
);
}
$channelKey = $metadata->get(self::METADATA_KEY);
if ($channelKey !== $this->key) {
throw new EnvelopeNotAcceptable(
"Channel '{$this->key}' inadvertently received Envelope ".
"'{$envelope->getUuid()->toString()}' for channel '$channelKey'.",
EnvelopeNotAcceptable::CHANNEL_KEY_UNEXPECTED
);
}
if (!$metadata->has(SubscriptionInterface::METADATA_KEY)) {
throw new EnvelopeNotAcceptable(
"Subscription key '".SubscriptionInterface::METADATA_KEY."' missing in metadata of ".
"Envelope '{$envelope->getUuid()->toString()}' received on channel '{$this->key}'.",
EnvelopeNotAcceptable::SUBSCRIPTION_KEY_MISSING
);
}
}
}