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

Pusher::close()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Pusher.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     Pusher
10
 * @since          1.0.0
11
 *
12
 * @date           01.03.17
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\WebSocketsZMQ\Pusher;
18
19
use Psr\Log;
20
21
use IPub;
22
use IPub\WebSocketsZMQ;
23
24
use IPub\WebSockets\Router;
25
26
use IPub\WebSocketsWAMP\PushMessages;
27
use IPub\WebSocketsWAMP\Serializers;
28
29
/**
30
 * ZeroMQ message pusher
31
 *
32
 * @package        iPublikuj:WebSocketsZMQ!
33
 * @subpackage     Pushers
34
 *
35
 * @author         Adam Kadlec <[email protected]>
36
 */
37 1
final class Pusher extends PushMessages\Pusher
38
{
39
	/**
40
	 * @var WebSocketsZMQ\Configuration
41
	 */
42
	private $configuration;
43
44
	/**
45
	 * @var Log\LoggerInterface
46
	 */
47
	private $logger;
48
49
	/**
50
	 * @var \ZMQSocket
51
	 */
52
	private $socket;
53
54
	/**
55
	 * @param WebSocketsZMQ\Configuration $configuration
56
	 * @param Log\LoggerInterface|NULL $logger
57
	 * @param Router\LinkGenerator $linkGenerator
58
	 * @param Serializers\PushMessageSerializer $serializer
59
	 */
60 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...
61
		WebSocketsZMQ\Configuration $configuration,
62
		Log\LoggerInterface $logger = NULL,
63
		Router\LinkGenerator $linkGenerator,
64
		Serializers\PushMessageSerializer $serializer
65
	) {
66 1
		parent::__construct('zmq', $serializer, $linkGenerator);
67
68 1
		$this->configuration = $configuration;
69 1
		$this->logger = $logger === NULL ? new Log\NullLogger : $logger;
70 1
	}
71
72
	/**
73
	 * {@inheritdoc}
74
	 */
75
	public function close() : void
76
	{
77
		if ($this->isConnected() === FALSE) {
78
			return;
79
		}
80
81
		$this->socket->disconnect($this->configuration->getHost() . ':' . $this->configuration->getPort());
82
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 */
87
	protected function doPush(string $data, array $context = []) : void
88
	{
89
		if ($this->isConnected() === FALSE) {
90
			if (!extension_loaded('zmq')) {
91
				throw new \RuntimeException(sprintf(
92
					'%s pusher require %s php extension',
93
					get_class($this),
94
					$this->getName()
95
				));
96
			}
97
98
			$context = new \ZMQContext(1, $this->configuration->isPersistent());
99
100
			$this->socket = new \ZMQSocket($context, \ZMQ::SOCKET_PUSH);
101
			$this->socket->connect($this->configuration->getProtocol() . '://' . $this->configuration->getHost() . ':' . $this->configuration->getPort());
102
103
			$this->setConnected();
104
		}
105
106
		$this->socket->send($data);
107
	}
108
}
109