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

ZeroMqClient   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 22.73 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 58.82%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 11
c 5
b 0
f 1
lcom 1
cbo 2
dl 25
loc 110
ccs 20
cts 34
cp 0.5882
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() 11 11 2
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 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