Completed
Push — master ( 46a77a...a5d155 )
by Fabio
53:55
created

TPageStateFormatter::unserialize()   C

Complexity

Conditions 12
Paths 26

Size

Total Lines 36

Duplication

Lines 16
Ratio 44.44 %

Importance

Changes 0
Metric Value
cc 12
nc 26
nop 2
dl 16
loc 36
rs 6.9666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * TPage class file
4
 *
5
 * @author Qiang Xue <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 * @package Prado\Web\UI
10
 */
11
12
namespace Prado\Web\UI;
13
14
/**
15
 * TPageStateFormatter class.
16
 *
17
 * TPageStateFormatter is a utility class to transform the page state
18
 * into and from a string that can be properly saved in persistent storage.
19
 *
20
 * Depending on the {@link TPage::getEnableStateValidation() EnableStateValidation}
21
 * and {@link TPage::getEnableStateEncryption() EnableStateEncryption},
22
 * TPageStateFormatter may do HMAC validation and encryption to prevent
23
 * the state data from being tampered or viewed.
24
 * The private keys and hashing/encryption methods are determined by
25
 * {@link TApplication::getSecurityManager() SecurityManager}.
26
 *
27
 * @author Qiang Xue <[email protected]>
28
 * @package Prado\Web\UI
29
 * @since 3.1
30
 */
31
class TPageStateFormatter
32
{
33
	/**
34
	 * @param TPage $page
35
	 * @param mixed $data state data
36
	 * @return string serialized data
37
	 */
38
	public static function serialize($page, $data)
39
	{
40
		$sm = $page->getApplication()->getSecurityManager();
41
		if($page->getEnableStateIGBinary() && extension_loaded('igbinary'))
42
		{
43
			if ($page->getEnableStateValidation()) {
44
				$str = $sm->hashData(igbinary_serialize($data));
45
			} else {
46
				$str = igbinary_serialize($data);
47
			}
48
		} else {
49
			if ($page->getEnableStateValidation()) {
50
				$str = $sm->hashData(serialize($data));
51
			} else {
52
				$str = serialize($data);
53
			}
54
		}
55
		if ($page->getEnableStateCompression() && extension_loaded('zlib')) {
56
			$str = gzcompress($str);
57
		}
58
		if ($page->getEnableStateEncryption()) {
59
			$str = $sm->encrypt($str);
60
		}
61
		return base64_encode($str);
62
	}
63
64
	/**
65
	 * @param TPage $page
66
	 * @param string $data serialized data
67
	 * @return mixed unserialized state data, null if data is corrupted
68
	 */
69
	public static function unserialize($page, $data)
70
	{
71
		$str = base64_decode($data);
72
		if ($str === '') {
73
			return null;
74
		}
75
		if ($str !== false) {
76
			$sm = $page->getApplication()->getSecurityManager();
77
			if ($page->getEnableStateEncryption()) {
78
				$str = $sm->decrypt($str);
79
			}
80
			if ($page->getEnableStateCompression() && extension_loaded('zlib')) {
81
				$str = @gzuncompress($str);
82
			}
83
84
			if($page->getEnableStateIGBinary() && extension_loaded('igbinary'))
85
			{
86 View Code Duplication
				if ($page->getEnableStateValidation()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
					if (($str = $sm->validateData($str)) !== false) {
88
						return igbinary_unserialize($str);
89
					}
90
				} else {
91
					return igbinary_unserialize($str);
92
				}
93 View Code Duplication
			} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
				if ($page->getEnableStateValidation()) {
95
					if (($str = $sm->validateData($str)) !== false) {
96
						return unserialize($str);
97
					}
98
				} else {
99
					return unserialize($str);
100
				}
101
			}
102
		}
103
		return null;
104
	}
105
}
106