1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AsyncPHP\Remit\Client; |
4
|
|
|
|
5
|
|
|
use AsyncPHP\Remit\Client; |
6
|
|
|
use AsyncPHP\Remit\Event; |
7
|
|
|
use AsyncPHP\Remit\Event\InMemoryEvent; |
8
|
|
|
use AsyncPHP\Remit\Location; |
9
|
|
|
use ZMQ; |
10
|
|
|
use ZMQContext; |
11
|
|
|
use ZMQSocket; |
12
|
|
|
|
13
|
|
|
final class ZeroMqClient implements Client |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Location |
17
|
|
|
*/ |
18
|
|
|
private $location; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ZMQSocket |
22
|
|
|
*/ |
23
|
|
|
private $socket; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Location $location |
27
|
|
|
*/ |
28
|
2 |
|
public function __construct(Location $location) |
29
|
|
|
{ |
30
|
2 |
|
$this->location = $location; |
31
|
2 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $name |
35
|
|
|
* @param array $parameters |
36
|
|
|
*/ |
37
|
1 |
|
public function emit($name, array $parameters = []) |
38
|
|
|
{ |
39
|
1 |
|
$socket = $this->getSocket(); |
40
|
|
|
|
41
|
|
|
$event = $this->newEvent($name, $parameters); |
42
|
|
|
|
43
|
|
|
$socket->send(serialize($event), ZMQ::MODE_DONTWAIT); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $name |
48
|
|
|
* @param array $parameters |
49
|
|
|
* |
50
|
|
|
* @return Event |
51
|
|
|
*/ |
52
|
|
|
private function newEvent($name, array $parameters = []) |
53
|
|
|
{ |
54
|
|
|
return new InMemoryEvent($name, $parameters); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return ZMQSocket |
59
|
|
|
*/ |
60
|
1 |
View Code Duplication |
private function getSocket() |
|
|
|
|
61
|
|
|
{ |
62
|
1 |
|
if ($this->socket === null) { |
63
|
1 |
|
$context = new ZMQContext(); |
64
|
|
|
|
65
|
|
|
$host = $this->location->getHost(); |
66
|
|
|
$port = $this->location->getPort(); |
67
|
|
|
|
68
|
|
|
$this->socket = new ZMQSocket($context, ZMQ::SOCKET_PUSH, spl_object_hash($this)); |
69
|
|
|
$this->socket->connect("tcp://{$host}:$port"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->socket; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
1 |
|
public function serialize() |
81
|
|
|
{ |
82
|
1 |
|
return serialize($this->location); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
* |
88
|
|
|
* @param string $serialized |
89
|
|
|
*/ |
90
|
1 |
|
public function unserialize($serialized) |
91
|
|
|
{ |
92
|
1 |
|
$this->location = unserialize($serialized); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return Location |
97
|
|
|
*/ |
98
|
|
|
public function getLocation() |
99
|
|
|
{ |
100
|
|
|
return $this->location; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritdoc |
105
|
|
|
*/ |
106
|
1 |
View Code Duplication |
public function disconnect() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
try { |
109
|
1 |
|
$host = $this->location->getHost(); |
110
|
1 |
|
$port = $this->location->getPort(); |
111
|
|
|
|
112
|
1 |
|
$this->socket->disconnect("tcp://{$host}:{$port}"); |
113
|
1 |
|
} catch (Exception $exception) { |
|
|
|
|
114
|
|
|
// TODO: find an elegant way to deal with this |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
public function __destruct() |
119
|
|
|
{ |
120
|
1 |
|
$this->disconnect(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.