Completed
Push — master ( d1b7ac...fe3773 )
by Christopher
02:05
created

ZeroMqClient::getSocket()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 2.9766

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 14
loc 14
ccs 3
cts 8
cp 0.375
rs 9.4286
cc 2
eloc 8
nc 2
nop 0
crap 2.9766
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()
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
            $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()
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
        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) {
0 ignored issues
show
Bug introduced by
The class AsyncPHP\Remit\Client\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
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