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

Message   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 49
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A jsonSerialize() 0 7 1
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