Completed
Push — master ( b2dfe1...1412d2 )
by Sam
02:32
created

Factory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A factory() 0 18 4
1
<?php
2
3
namespace Jalle19\StatusManager\Message;
4
5
use Jalle19\StatusManager\Exception\MalformedRequestException;
6
use Jalle19\StatusManager\Exception\UnknownRequestException;
7
8
/**
9
 * Factory for turning raw client messages into respective message objects.
10
 *
11
 * @package   Jalle19\StatusManager\Message
12
 * @copyright Copyright &copy; Sam Stenvall 2016-
13
 * @license   https://www.gnu.org/licenses/gpl.html The GNU General Public License v2.0
14
 */
15
class Factory
16
{
17
18
	/**
19
	 * @param string $json
20
	 *
21
	 * @return AbstractMessage
22
	 * @throws UnknownRequestException
23
	 * @throws MalformedRequestException
24
	 */
25
	public static function factory($json)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
26
	{
27
		$deserialized = json_decode($json);
28
29
		if (!isset($deserialized->type) || !isset($deserialized->payload))
30
			throw new MalformedRequestException('Missing "type" or "payload"');
31
32
		$type       = $deserialized->type;
33
		$parameters = $deserialized->payload;
34
35
		switch ($type)
36
		{
37
			case AbstractMessage::TYPE_STATISTICS_POPULAR_CHANNELS:
38
				return new StatisticsPopularChannelsMessage($type, $parameters);
39
			default:
40
				throw new UnknownRequestException('Unknown message "' . $type . '"');
41
		}
42
	}
43
44
}
45