Completed
Push — master ( c3a34a...d1b7ac )
by Christopher
02:13
created

ZeroMqServer   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 176
Duplicated Lines 13.07 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 83.87%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 21
c 4
b 0
f 0
lcom 1
cbo 3
dl 23
loc 176
ccs 52
cts 62
cp 0.8387
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 4 1
A unserialize() 0 4 1
A getLocation() 0 4 1
A __destruct() 0 4 1
A addListener() 0 10 2
A removeListener() 0 10 3
A tick() 0 14 3
A getSocket() 14 14 2
A dispatchEvent() 0 12 3
A emit() 0 6 1
A disconnect() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AsyncPHP\Remit\Server;
4
5
use AsyncPHP\Remit\Event;
6
use AsyncPHP\Remit\Event\InMemoryEvent;
7
use AsyncPHP\Remit\Location;
8
use AsyncPHP\Remit\Server;
9
use Closure;
10
use ZMQ;
11
use ZMQContext;
12
use ZMQSocket;
13
14
final class ZeroMqServer implements Server
15
{
16
    /**
17
     * @var Location
18
     */
19
    private $location;
20
21
    /**
22
     * @var ZMQSocket
23
     */
24
    private $socket;
25
26
    /**
27
     * @var array
28
     */
29
    private $listeners = [];
30
31
    /**
32
     * @param Location $location
33
     */
34 2
    public function __construct(Location $location)
35
    {
36 2
        $this->location = $location;
37 2
    }
38
39
    /**
40
     * @inheritdoc
41
     *
42
     * @param string $name
43
     * @param Closure $closure
44
     *
45
     * @return $this
46
     */
47 1
    public function addListener($name, Closure $closure)
48
    {
49 1
        if (empty($this->listeners[$name])) {
50 1
            $hash = spl_object_hash($closure);
51
52 1
            $this->listeners[$name][$hash] = $closure;
53 1
        }
54
55 1
        return $this;
56
    }
57
58
    /**
59
     * @inheritdoc
60
     *
61
     * @param string $name
62
     * @param Closure $closure
63
     */
64 1
    public function removeListener($name, Closure $closure)
65
    {
66 1
        $hash = spl_object_hash($closure);
67
68 1
        if (isset($this->listeners[$name]) && isset($this->listeners[$name][$hash])) {
69 1
            unset($this->listeners[$name][$hash]);
70 1
        }
71
72 1
        return $this;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 1
    public function tick()
79
    {
80 1
        $socket = $this->getSocket();
81
82 1
        if (!($event = $socket->recv(ZMQ::MODE_DONTWAIT))) {
83 1
            return;
84
        }
85
86 1
        $event = @unserialize($event);
87
88 1
        if ($event instanceof Event) {
89 1
            $this->dispatchEvent($event);
90 1
        }
91 1
    }
92
93
    /**
94
     * @return ZMQSocket
95
     */
96 1 View Code Duplication
    private function getSocket()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
97
    {
98 1
        if ($this->socket === null) {
99 1
            $context = new ZMQContext();
100
101 1
            $host = $this->location->getHost();
102 1
            $port = $this->location->getPort();
103
104 1
            $this->socket = new ZMQSocket($context, ZMQ::SOCKET_PULL, spl_object_hash($this));
105 1
            $this->socket->bind("tcp://{$host}:$port");
106 1
        }
107
108 1
        return $this->socket;
109
    }
110
111
    /**
112
     * @param Event $event
113
     *
114
     * @return $this
115
     */
116 1
    private function dispatchEvent(Event $event)
117
    {
118 1
        $name = $event->getName();
119
120 1
        if (isset($this->listeners[$name])) {
121 1
            foreach ($this->listeners[$name] as $closure) {
122 1
                call_user_func_array($closure, $event->getParameters());
123 1
            }
124 1
        }
125
126 1
        return $this;
127
    }
128
129
    /**
130
     * @inheritdoc
131
     *
132
     * @param string $name
133
     * @param array $parameters
134
     *
135
     * @return $this
136
     */
137
    public function emit($name, array $parameters = [])
138
    {
139
        return $this->dispatchEvent(
140
            new InMemoryEvent($name, $parameters)
141
        );
142
    }
143
144
    /**
145
     * @inheritdoc
146
     *
147
     * @return string
148
     */
149 1
    public function serialize()
150
    {
151 1
        return serialize($this->location);
152
    }
153
154
    /**
155
     * @inheritdoc
156
     *
157
     * @param string $serialized
158
     */
159 1
    public function unserialize($serialized)
160
    {
161 1
        $this->location = unserialize($serialized);
162 1
    }
163
164
    /**
165
     * @return Location
166
     */
167
    public function getLocation()
168
    {
169
        return $this->location;
170
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175 1 View Code Duplication
    public function disconnect()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
176
    {
177 1
        if ($this->socket) {
178
            $host = $this->location->getHost();
179
            $port = $this->location->getPort();
180
181
            $this->socket->disconnect("tcp://{$host}:$port");
182
        }
183 1
    }
184
185 1
    public function __destruct()
186
    {
187 1
        $this->disconnect();
188 1
    }
189
}
190