Completed
Push — master ( a8025a...c0bd03 )
by Adam
03:33
created

Consumer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 10.68 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 15.38%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 11
loc 103
ccs 6
cts 39
cp 0.1538
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 2
A connect() 0 52 3
A close() 0 4 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
 * Consumer.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:WebSocketsZMQ!
9
 * @subpackage     Consumer
10
 * @since          1.0.0
11
 *
12
 * @date           28.02.17
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\WebSocketsZMQ\Consumer;
18
19
use Psr\Log;
20
21
use React;
22
use React\EventLoop;
23
use React\ZMQ;
24
25
use IPub;
26
use IPub\WebSocketsZMQ;
27
28
use IPub\WebSocketsWAMP\Application;
29
use IPub\WebSocketsWAMP\Entities;
30
use IPub\WebSocketsWAMP\PushMessages;
31
use IPub\WebSocketsWAMP\Serializers;
32
33
use IPub\WebSockets\Exceptions as WebSocketsExceptions;
34
35
/**
36
 * ZeroMQ consumer
37
 *
38
 * @package        iPublikuj:WebSocketsZMQ!
39
 * @subpackage     Consumer
40
 *
41
 * @author         Adam Kadlec <[email protected]>
42
 */
43 1
final class Consumer extends PushMessages\Consumer
44
{
45
	/**
46
	 * @var WebSocketsZMQ\Configuration
47
	 */
48
	private $configuration;
49
50
	/**
51
	 * @var  Serializers\PushMessageSerializer
52
	 */
53
	private $serializer;
54
55
	/**
56
	 * @var ZMQ\SocketWrapper
57
	 */
58
	private $socket;
59
60
	/**
61
	 * @var  Log\LoggerInterface
62
	 */
63
	private $logger;
64
65
	/**
66
	 * @param WebSocketsZMQ\Configuration $configuration
67
	 * @param Serializers\PushMessageSerializer $serializer
68
	 * @param Log\LoggerInterface|NULL $logger
69
	 */
70 View Code Duplication
	public function __construct(
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...
71
		WebSocketsZMQ\Configuration $configuration,
72
		Serializers\PushMessageSerializer $serializer,
73
		Log\LoggerInterface $logger = NULL
74
	) {
75 1
		parent::__construct('zmq');
76
77 1
		$this->configuration = $configuration;
78 1
		$this->serializer = $serializer;
79 1
		$this->logger = $logger === NULL ? new Log\NullLogger : $logger;
80 1
	}
81
82
	/**
83
	 * {@inheritdoc}
84
	 */
85
	public function connect(EventLoop\LoopInterface $loop, Application\IApplication $application)
86
	{
87
		$context = new ZMQ\Context($loop);
88
89
		$this->socket = $context->getSocket(\ZMQ::SOCKET_PULL);
90
91
		$this->logger->info(sprintf(
92
			'ZMQ transport listening on %s:%s',
93
			$this->configuration->getHost(),
94
			$this->configuration->getPort()
95
		));
96
97
		$this->socket->bind($this->configuration->getProtocol() . '://' . $this->configuration->getHost() . ':' . $this->configuration->getPort());
98
99
		$this->socket->on('message', function ($data) use ($application) {
100
			try {
101
				/** @var Entities\PushMessages\IMessage $message */
102
				$message = $this->serializer->deserialize($data);
103
104
				$application->handlePush($message, $this->getName());
105
106
				$this->onSuccess($this, $data);
107
108
			} catch (WebSocketsExceptions\TerminateException $ex) {
109
				throw $ex;
110
111
			} catch (\Exception $ex) {
112
				$this->logger->error(
113
					'ZMQ socket failed to ack message', [
114
						'exception_message' => $ex->getMessage(),
115
						'file'              => $ex->getFile(),
116
						'line'              => $ex->getLine(),
117
						'message'           => $data,
118
					]
119
				);
120
121
				$this->onFail($this, $data);
122
			}
123
		});
124
125
		$this->socket->on('error', function (\Exception $ex) use ($application) {
126
			$this->logger->error(
127
				'ZMQ socket failed to receive message', [
128
					'exception_message' => $ex->getMessage(),
129
					'file'              => $ex->getFile(),
130
					'line'              => $ex->getLine(),
131
				]
132
			);
133
134
			$this->onFail($this);
135
		});
136
	}
137
138
	/**
139
	 * {@inheritdoc}
140
	 */
141
	public function close() : void
142
	{
143
		$this->socket ?: $this->socket->close();
144
	}
145
}
146