Skip to content

Commit e84770f

Browse files
committed
[client] move stuff to separate class.
1 parent bb98a46 commit e84770f

3 files changed

Lines changed: 59 additions & 29 deletions

File tree

‎pkg/enqueue/Client/Producer.php‎

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Enqueue\Util\JSON;
66
use Enqueue\Util\UUID;
77

8-
class Producer implements ProducerInterface, ProducerV2Interface
8+
class Producer implements ProducerInterface
99
{
1010
/**
1111
* @var DriverInterface
@@ -18,8 +18,7 @@ class Producer implements ProducerInterface, ProducerV2Interface
1818
private $extension;
1919

2020
/**
21-
* @param DriverInterface $driver
22-
* @param ExtensionInterface|null $extension
21+
* @param DriverInterface $driver
2322
*/
2423
public function __construct(DriverInterface $driver, ExtensionInterface $extension = null)
2524
{
@@ -81,32 +80,6 @@ public function send($topic, $message)
8180
}
8281
}
8382

84-
/**
85-
* {@inheritdoc}
86-
*/
87-
public function sendEvent($topic, $message)
88-
{
89-
$this->send($topic, $message);
90-
}
91-
92-
/**
93-
* {@inheritdoc}
94-
*/
95-
public function sendCommand($command, $message)
96-
{
97-
if (false == $message instanceof Message) {
98-
$message = new Message($message);
99-
}
100-
101-
$message->setProperty(Config::PARAMETER_TOPIC_NAME, Config::COMMAND_TOPIC);
102-
$message->setProperty(Config::PARAMETER_PROCESSOR_NAME, $command);
103-
$message->setScope(Message::SCOPE_APP);
104-
105-
$this->send('__command__', $message);
106-
107-
// TODO add support of replies
108-
}
109-
11083
/**
11184
* @param Message $message
11285
*/

‎pkg/enqueue/Client/ProducerV2.php‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Enqueue\Client;
4+
5+
class ProducerV2 implements ProducerV2Interface
6+
{
7+
/**
8+
* @var ProducerInterface
9+
*/
10+
private $realProducer;
11+
12+
/**
13+
* @var RpcClient
14+
*/
15+
private $rpcClient;
16+
17+
/**
18+
* @param ProducerInterface $realProducer
19+
* @param RpcClient $rpcClient
20+
*/
21+
public function __construct(ProducerInterface $realProducer, RpcClient $rpcClient)
22+
{
23+
$this->realProducer = $realProducer;
24+
$this->rpcClient = $rpcClient;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function sendEvent($topic, $message)
31+
{
32+
$this->realProducer->send($topic, $message);
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function sendCommand($command, $message)
39+
{
40+
if (false == $message instanceof Message) {
41+
$message = new Message($message);
42+
}
43+
44+
$message->setProperty(Config::PARAMETER_TOPIC_NAME, Config::COMMAND_TOPIC);
45+
$message->setProperty(Config::PARAMETER_PROCESSOR_NAME, $command);
46+
$message->setScope(Message::SCOPE_APP);
47+
48+
if ($message->getReplyTo()) {
49+
return $this->rpcClient->callAsync(Config::COMMAND_TOPIC, $message, 60);
50+
}
51+
$this->realProducer->send(Config::COMMAND_TOPIC, $message);
52+
}
53+
}

‎pkg/enqueue/Client/ProducerV2Interface.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Enqueue\Client;
44

5+
use Enqueue\Rpc\Promise;
6+
57
interface ProducerV2Interface
68
{
79
/**
@@ -17,6 +19,8 @@ public function sendEvent($topic, $message);
1719
*
1820
* @param string $command
1921
* @param string|array|Message $message
22+
*
23+
* @return Promise|null the promise is returned if message has reply to set
2024
*/
2125
public function sendCommand($command, $message);
2226
}

0 commit comments

Comments
 (0)