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

ZeroMqClient::disconnect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.7462

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
ccs 3
cts 7
cp 0.4286
rs 9.6667
cc 2
eloc 5
nc 2
nop 0
crap 2.7462
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 1
        $event = $this->newEvent($name, $parameters);
42
43 1
        $socket->send(serialize($event), ZMQ::MODE_DONTWAIT);
44 1
    }
45
46
    /**
47
     * @param string $name
48
     * @param array $parameters
49
     *
50
     * @return Event
51
     */
52 1
    private function newEvent($name, array $parameters = [])
53
    {
54 1
        return new InMemoryEvent($name, $parameters);
55
    }
56
57
    /**
58
     * @return ZMQSocket
59
     */
60 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...
61
    {
62 1
        if ($this->socket === null) {
63 1
            $context = new ZMQContext();
64
65 1
            $host = $this->location->getHost();
66 1
            $port = $this->location->getPort();
67
68 1
            $this->socket = new ZMQSocket($context, ZMQ::SOCKET_PUSH, spl_object_hash($this));
69 1
            $this->socket->connect("tcp://{$host}:$port");
70 1
        }
71
72 1
        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()
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...
107
    {
108 1
        if ($this->socket) {
109
            $host = $this->location->getHost();
110
            $port = $this->location->getPort();
111
112
            $this->socket->disconnect("tcp://{$host}:$port");
113
        }
114 1
    }
115
116 1
    public function __destruct()
117
    {
118 1
        $this->disconnect();
119 1
    }
120
}
121