Completed
Push — master ( 6c3991...2e8b69 )
by Tomáš
07:24
created

getRoutingKeyFromMessageRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
namespace TomCizek\SymfonyProoph\AsynchronousMessages;
4
5
use Prooph\Common\Messaging\Message;
6
use Prooph\Common\Messaging\MessageConverter;
7
use Prooph\ServiceBus\Async\MessageProducer;
8
use Prooph\ServiceBus\Exception\RuntimeException;
9
use React\Promise\Deferred;
10
11
class AsynchronousMessageProducer implements MessageProducer
12
{
13
	/* @var AsynchronousMessageProducerBridge */
14
	private $producerBridge;
15
16
	/** @var MessageConverter */
17
	private $messageConverter;
18
19
	/** @var array */
20
	private $routes;
21
22 7
	public function __construct(AsynchronousMessageProducerBridge $producerBridge, MessageConverter $messageConverter)
23
	{
24 7
		$this->producerBridge = $producerBridge;
25 7
		$this->messageConverter = $messageConverter;
26 7
	}
27
28 5
	public function injectRoutes(array $routes)
29
	{
30 5
		$this->routes = $routes;
31 5
	}
32
33 4
	public function __invoke(Message $message, Deferred $deferred = null): void
34
	{
35 4
		if ($deferred !== null) {
36 1
			throw new RuntimeException(__CLASS__ . ' cannot handle query messages which require future responses.');
37
		}
38
39 3
		$routingKey = $this->getRoutingKeyFromMessageRoute($message);
40
41 2
		$this->producerBridge->publishWithRoutingKey($message, $routingKey);
42 2
	}
43
44 3
	private function getRoutingKeyFromMessageRoute(Message $message): string
45
	{
46 3
		if (empty($this->routes[$message->messageName()])) {
47 1
			throw new RuntimeException(
48 1
				sprintf(
49 1
					'Producer route key for message of name "%s" in asynchronous routing not found.',
50 1
					$message->messageName()
51
				)
52
			);
53
		}
54
55 2
		return $this->routes[$message->messageName()];
56
	}
57
}
58