Completed
Push — master ( 2bf1e4...a81261 )
by Sam
02:14
created

Message::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Jalle19\StatusManager;
4
5
/**
6
 * Represents a message that can be serialized and sent to clients
7
 *
8
 * @package   Jalle19\StatusManager
9
 * @copyright Copyright &copy; Sam Stenvall 2016-
10
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
11
 */
12
class Message implements \JsonSerializable
13
{
14
15
	const TYPE_STATUS_UPDATES = 'statusUpdates';
16
17
	/**
18
	 * @var string
19
	 */
20
	private $_message;
21
22
	/**
23
	 * @var \JsonSerializable
24
	 */
25
	private $_payload;
26
27
	/**
28
	 * @var array
29
	 */
30
	private static $availableTypes = [
31
		self::TYPE_STATUS_UPDATES,
32
	];
33
34
35
	/**
36
	 * @param string            $message
37
	 * @param \JsonSerializable $payload
38
	 */
39
	public function __construct($message, \JsonSerializable $payload)
40
	{
41
		if (!in_array($message, self::$availableTypes))
42
			throw new \InvalidArgumentException('Invalid message type ' . $message);
43
44
		$this->_message = $message;
45
		$this->_payload = $payload;
46
	}
47
48
49
	/**
50
	 * @inheritdoc
51
	 */
52
	public function jsonSerialize()
53
	{
54
		return [
55
			'message' => $this->_message,
56
			'payload' => $this->_payload,
57
		];
58
	}
59
60
}
61