EventSource::close()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Nextcloud - Gallery
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Bart Visscher <[email protected]>
9
 * @author Felix Moeller <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Robin Appelman <[email protected]>
12
 * @author Thomas Müller <[email protected]>
13
 * @author Vincent Petry <[email protected]>
14
 * @author Olivier Paroz <[email protected]>
15
 *
16
 * @copyright Copyright (c) 2015, ownCloud, Inc.
17
 * @copyright Olivier Paroz 2017
18
 */
19
20
namespace OCA\Gallery\Utility;
21
22
/**
23
 * Class EventSource
24
 *
25
 * Wrapper for server side events (http://en.wikipedia.org/wiki/Server-sent_events)
26
 *
27
 * This version is tailored for the Gallery app, do not use elsewhere!
28
 * @link https://github.com/owncloud/core/blob/master/lib/private/eventsource.php
29
 *
30
 * @todo Replace with a library
31
 *
32
 * @package OCA\Gallery\Controller
33
 */
34
class EventSource implements \OCP\IEventSource {
0 ignored issues
show
Bug introduced by
The type OCP\IEventSource 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...
35
36
	/**
37
	 * @var bool
38
	 */
39
	private $started = false;
40
41
	protected function init() {
42
		if ($this->started) {
43
			return;
44
		}
45
		$this->started = true;
46
47
		// prevent php output buffering, caching and nginx buffering
48
		while (ob_get_level()) {
49
			ob_end_clean();
50
		}
51
		header('Cache-Control: no-cache');
52
		header('X-Accel-Buffering: no');
53
		header("Content-Type: text/event-stream");
54
		flush();
55
	}
56
57
	/**
58
	 * Sends a message to the client
59
	 *
60
	 * If only one parameter is given, a typeless message will be sent with that parameter as data
61
	 *
62
	 * @param string $type
63
	 * @param mixed $data
64
	 *
65
	 * @throws \BadMethodCallException
66
	 */
67
	public function send($type, $data = null) {
68
		$this->validateMessage($type, $data);
69
		$this->init();
70
		if (is_null($data)) {
71
			$data = $type;
72
			$type = null;
73
		}
74
75
		if (!empty($type)) {
76
			echo 'event: ' . $type . PHP_EOL;
77
		}
78
		echo 'data: ' . json_encode($data) . PHP_EOL;
79
80
		echo PHP_EOL;
81
		flush();
82
	}
83
84
	/**
85
	 * Closes the connection of the event source
86
	 *
87
	 * It's best to let the client close the stream
88
	 */
89
	public function close() {
90
		$this->send(
91
			'__internal__', 'close'
92
		);
93
	}
94
95
	/**
96
	 * Makes sure we have a message we can use
97
	 *
98
	 * @param string $type
99
	 * @param mixed $data
100
	 */
101
	private function validateMessage($type, $data) {
102
		if ($data && !preg_match('/^[A-Za-z0-9_]+$/', $type)) {
103
			throw new \BadMethodCallException('Type needs to be alphanumeric (' . $type . ')');
104
		}
105
	}
106
}
107