LibraryUpdateListener::blockUntil()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 17
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 33
rs 9.3888
1
<?php
2
3
/**
4
 * Utility for triggering library updates through a WebSocket connection. A 
5
 * user can attach callbacks that are run when the scan is started and 
6
 * finished.
7
 *
8
 * @author Sam Stenvall <[email protected]>
9
 * @copyright Copyright &copy; Sam Stenvall 2015-
10
 * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
11
 */
12
class LibraryUpdateListener
13
{
14
15
	const METHOD_ON_SCAN_STARTED = 'VideoLibrary.OnScanStarted';
16
	const METHOD_ON_SCAN_FINISHED = 'VideoLibrary.OnScanFinished';
17
18
	/**
19
	 * @var Closure event handler for METHOD_ON_SCAN_STARTED
20
	 */
21
	public $onScanStarted;
22
23
	/**
24
	 * @var Closure event handler for METHOD_ON_SCAN_FINISHED
25
	 */
26
	public $onScanFinished;
27
28
	/**
29
	 * @var Hoa\Websocket\Client the Websocket client
30
	 */
31
	private $_client;
32
33
	/**
34
	 * Class constructor
35
	 * @param Backend $backend the backend to trigger the update on
36
	 */
37
	public function __construct($backend)
38
	{
39
		$hostname = $backend->hostname;
40
		$port = $backend->tcp_port;
41
42
		// Create the Websocket client
43
		$this->_client = new Hoa\Websocket\Client(
44
				new Hoa\Socket\Client('tcp://'.$hostname.':'.$port));
45
		$this->_client->setHost(gethostname());
46
	}
47
48
	/**
49
	 * Listens for new events until the specified event is received
50
	 * @param string the event to wait for
0 ignored issues
show
Bug introduced by
The type the was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
51
	 */
52
	public function blockUntil($event)
53
	{
54
		// Trigger a library update when the socket has been opened
55
		$this->_client->on('open', function()
56
		{
57
			Yii::app()->xbmc->sendNotification('VideoLibrary.Scan');
58
		});
59
60
		$this->_client->on('message', function(Hoa\Event\Bucket $bucket) use ($event)
61
		{
62
			$response = $this->parseResponse($bucket);
63
64
			if ($response === null)
65
				return;
66
67
			// Handle events
68
			switch ($response->method)
69
			{
70
				case self::METHOD_ON_SCAN_STARTED:
71
					$this->onScanStarted->__invoke();
72
					break;
73
				case self::METHOD_ON_SCAN_FINISHED:
74
					$this->onScanFinished->__invoke();
75
					break;
76
			}
77
78
			if ($response->method !== $event)
79
				$this->_client->receive();
80
		});
81
82
		// Start listening (wait for one message)
83
		$this->_client->connect();
84
		$this->_client->receive();
85
	}
86
87
	/**
88
	 * @param Hoa\Event\Bucket $bucket
89
	 * @return stdClass the response object, or null if the response is invalid
90
	 */
91
	private function parseResponse(Hoa\Event\Bucket $bucket)
92
	{
93
		$data = $bucket->getData();
94
		$response = json_decode($data['message']);
95
96
		return isset($response->method) ? $response : null;
97
	}
98
99
}
100