1
|
|
|
<?php declare(strict_types=1); |
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
|
|
|
namespace Daikon\MessageBus; |
10
|
|
|
|
11
|
|
|
use Daikon\MessageBus\Channel\ChannelInterface; |
12
|
|
|
use Daikon\MessageBus\Channel\ChannelMap; |
13
|
|
|
use Daikon\MessageBus\Exception\ChannelUnknown; |
14
|
|
|
use Daikon\MessageBus\Exception\EnvelopeNotAcceptable; |
15
|
|
|
use Daikon\Metadata\MetadataInterface; |
16
|
|
|
use Daikon\Metadata\Metadata; |
17
|
|
|
use Daikon\Metadata\MetadataEnricherInterface; |
18
|
|
|
use Daikon\Metadata\MetadataEnricherList; |
|
|
|
|
19
|
|
|
|
20
|
|
|
final class MessageBus implements MessageBusInterface |
21
|
|
|
{ |
22
|
|
|
private ChannelMap $channelMap; |
23
|
|
|
|
24
|
|
|
private MetadataEnricherList $metadataEnrichers; |
25
|
|
|
|
26
|
|
|
private string $envelopeType; |
27
|
|
|
|
28
|
5 |
|
public function __construct( |
29
|
|
|
ChannelMap $channelMap, |
30
|
|
|
MetadataEnricherList $metadataEnrichers = null, |
31
|
|
|
string $envelopeType = null |
32
|
|
|
) { |
33
|
5 |
|
$this->channelMap = $channelMap; |
34
|
5 |
|
$this->metadataEnrichers = $metadataEnrichers ?? new MetadataEnricherList; |
35
|
5 |
|
$this->envelopeType = $envelopeType ?? Envelope::class; |
36
|
5 |
|
} |
37
|
|
|
|
38
|
2 |
|
public function publish(MessageInterface $message, string $channelKey, MetadataInterface $metadata = null): void |
39
|
|
|
{ |
40
|
2 |
|
if (!$this->channelMap->has($channelKey)) { |
41
|
1 |
|
throw new ChannelUnknown("Channel '$channelKey' has not been registered on message bus."); |
42
|
|
|
} |
43
|
1 |
|
$metadata = $this->enrichMetadata($metadata ?? Metadata::makeEmpty()); |
44
|
|
|
/** @var EnvelopeInterface $envelope */ |
45
|
1 |
|
$envelope = $this->envelopeType::wrap($message, $metadata); |
46
|
|
|
/** @var ChannelInterface $channel */ |
47
|
1 |
|
$channel = $this->channelMap->get($channelKey); |
48
|
1 |
|
$channel->publish($envelope, $this); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
3 |
|
public function receive(EnvelopeInterface $envelope): void |
52
|
|
|
{ |
53
|
3 |
|
$this->verify($envelope); |
54
|
2 |
|
$channelKey = (string)$envelope->getMetadata()->get(ChannelInterface::METADATA_KEY); |
55
|
2 |
|
if (!$this->channelMap->has($channelKey)) { |
56
|
1 |
|
throw new ChannelUnknown("Channel '$channelKey' has not been registered on message bus."); |
57
|
|
|
} |
58
|
|
|
/** @var ChannelInterface $channel */ |
59
|
1 |
|
$channel = $this->channelMap->get($channelKey); |
60
|
1 |
|
$channel->receive($envelope); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
private function enrichMetadata(MetadataInterface $metadata): MetadataInterface |
64
|
|
|
{ |
65
|
1 |
|
return array_reduce( |
66
|
1 |
|
$this->metadataEnrichers->unwrap(), |
67
|
1 |
|
function (MetadataInterface $metadata, MetadataEnricherInterface $metadataEnricher): MetadataInterface { |
68
|
|
|
return $metadataEnricher->enrich($metadata); |
69
|
1 |
|
}, |
70
|
|
|
$metadata |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
private function verify(EnvelopeInterface $envelope): void |
75
|
|
|
{ |
76
|
3 |
|
$metadata = $envelope->getMetadata(); |
77
|
3 |
|
if (!$metadata->has(ChannelInterface::METADATA_KEY)) { |
78
|
1 |
|
throw new EnvelopeNotAcceptable( |
79
|
|
|
"Channel key '".ChannelInterface::METADATA_KEY."' missing in metadata of ". |
80
|
1 |
|
"Envelope '{$envelope->getUuid()->toString()}' received on message bus.", |
81
|
1 |
|
EnvelopeNotAcceptable::CHANNEL_KEY_MISSING |
82
|
|
|
); |
83
|
|
|
} |
84
|
2 |
|
} |
85
|
|
|
} |
86
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths