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

TPageStateFormatter   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 75
Duplicated Lines 21.33 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 16
loc 75
rs 10
c 0
b 0
f 0
wmc 20
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B serialize() 0 25 8
C unserialize() 16 36 12

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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