ZeroMqClient   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 24.11 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 81.08%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 12
c 6
b 0
f 2
lcom 1
cbo 2
dl 27
loc 112
ccs 30
cts 37
cp 0.8108
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A emit() 0 8 1
A newEvent() 0 4 1
A getSocket() 14 14 2
A serialize() 0 4 1
A unserialize() 0 4 1
A getLocation() 0 4 1
A disconnect() 13 13 3
A __destruct() 0 4 1

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\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 Exception;
10
use ZMQ;
11
use ZMQContext;
12
use ZMQSocket;
13
14
final class ZeroMqClient implements Client
15
{
16
    /**
17
     * @var Location
18
     */
19
    private $location;
20
21
    /**
22
     * @var ZMQSocket
23
     */
24
    private $socket;
25
26
    /**
27
     * @param Location $location
28
     */
29 2
    public function __construct(Location $location)
30
    {
31 2
        $this->location = $location;
32 2
    }
33
34
    /**
35
     * @param string $name
36
     * @param array $parameters
37
     */
38 1
    public function emit($name, array $parameters = [])
39
    {
40 1
        $socket = $this->getSocket();
41
42 1
        $event = $this->newEvent($name, $parameters);
43
44 1
        $socket->send(serialize($event), ZMQ::MODE_DONTWAIT);
45 1
    }
46
47
    /**
48
     * @param string $name
49
     * @param array $parameters
50
     *
51
     * @return Event
52
     */
53 1
    private function newEvent($name, array $parameters = [])
54
    {
55 1
        return new InMemoryEvent($name, $parameters);
56
    }
57
58
    /**
59
     * @return ZMQSocket
60
     */
61 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...
62
    {
63 1
        if ($this->socket === null) {
64 1
            $context = new ZMQContext();
65
66 1
            $host = $this->location->getHost();
67 1
            $port = $this->location->getPort();
68
69 1
            $this->socket = new ZMQSocket($context, ZMQ::SOCKET_PUSH, spl_object_hash($this));
70 1
            $this->socket->connect("tcp://{$host}:$port");
71 1
        }
72
73 1
        return $this->socket;
74
    }
75
76
    /**
77
     * @inheritdoc
78
     *
79
     * @return string
80
     */
81 1
    public function serialize()
82
    {
83 1
        return serialize($this->location);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     *
89
     * @param string $serialized
90
     */
91 1
    public function unserialize($serialized)
92
    {
93 1
        $this->location = unserialize($serialized);
94 1
    }
95
96
    /**
97
     * @return Location
98
     */
99
    public function getLocation()
100
    {
101
        return $this->location;
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107 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...
108
    {
109 1
        if ($this->socket) {
110
            try {
111
                $host = $this->location->getHost();
112
                $port = $this->location->getPort();
113
114
                $this->socket->disconnect("tcp://{$host}:{$port}");
115
            } catch (Exception $exception) {
116
                // TODO: find an elegant way to deal with this
117
            }
118
        }
119 1
    }
120
121 1
    public function __destruct()
122
    {
123 1
        $this->disconnect();
124 1
    }
125
}
126