SessionPhpBinarySerializer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 4 1
A unserialize() 0 18 2
1
<?php
2
/**
3
 * SessionPhpBinarySerializer.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        http://www.ipublikuj.eu
7
 * @author         Adam Kadlec http://www.ipublikuj.eu
8
 * @package        iPublikuj:WebSocketSession!
9
 * @subpackage     Serializers
10
 * @since          1.0.0
11
 *
12
 * @date           02.03.17
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\WebSocketsSession\Serializers;
18
19
use Nette;
20
21
use IPub;
22
use IPub\WebSocketsSession\Exceptions;
23
24
class SessionPhpBinarySerializer implements ISessionSerializer
25
{
26
	/**
27
	 * Implement nette smart magic
28
	 */
29
	use Nette\SmartObject;
30
31
	/**
32
	 * {@inheritdoc}
33
	 */
34
	public function serialize(array $data) : string
35
	{
36
		throw new Exceptions\RuntimeException('Serialize PhpHandler:serialize code not written yet, write me!');
37
	}
38
39
	/**
40
	 * {@inheritdoc}
41
	 *
42
	 * @link http://ca2.php.net/manual/en/function.session-decode.php#108037 Code from this comment on php.net
43
	 */
44
	public function unserialize(string $raw) : array
45
	{
46
		$returnData = [];
47
		$offset = 0;
48
49
		while ($offset < strlen($raw)) {
50
			$num = ord($raw[$offset]);
51
			$offset += 1;
52
			$varname = substr($raw, $offset, $num);
53
			$offset += $num;
54
			$data = unserialize(substr($raw, $offset));
55
56
			$returnData[$varname] = $data;
57
			$offset += strlen(serialize($data));
58
		}
59
60
		return $returnData;
61
	}
62
}
63