Completed
Push — master ( 935b39...dc8f0b )
by Leny
79:34 queued 56:23
created

ViewReferenceRedisTool   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 24
lcom 0
cbo 0
dl 0
loc 115
rs 10
c 2
b 0
f 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A redislize() 0 9 3
A unredislize() 0 13 3
A redislizeArray() 0 11 3
A unredislizeArray() 0 9 2
A generateKey() 0 4 1
C isSerialized() 0 25 12
1
<?php
2
3
namespace Victoire\Bundle\ViewReferenceBundle\Connector\Redis;
4
5
/**
6
 * Class ViewReferenceRedisTool.
7
 */
8
class ViewReferenceRedisTool
9
{
10
    /**
11
     * This method generated a string that can be persisted for redis with data.
12
     *
13
     * @param $data
14
     *
15
     * @return string
16
     */
17
    public function redislize($data)
18
    {
19
        // Only serialize if it's not a string or a integer
20
        if (!is_string($data) && !is_int($data)) {
21
            return urlencode(serialize($data));
22
        }
23
        // Encode string to escape wrong saves
24
        return urlencode($data);
25
    }
26
27
    /**
28
     * This method unredislize a string.
29
     *
30
     * @param $data
31
     *
32
     * @return mixed|string
33
     */
34
    public function unredislize($data)
35
    {
36
        $data = urldecode($data);
37
38
        if (self::isSerialized($data)) {
39
            $unserializedData = @unserialize($data);
40
            if ($unserializedData !== false) {
41
                return $unserializedData;
42
            }
43
        }
44
45
        return $data;
46
    }
47
48
    /**
49
     * Redislize an array (key, valueToRedislize).
50
     *
51
     * @param array $data
52
     *
53
     * @return array
54
     */
55
    public function redislizeArray(array $data)
56
    {
57
        $result = [];
58
        foreach ($data as $key => $value) {
59
            if (!is_array($value)) {
60
                $result[$key] = $this->redislize($value);
61
            }
62
        }
63
64
        return $result;
65
    }
66
67
    /**
68
     * unredislize an array (key, valueToUnredislize).
69
     *
70
     * @param array $data
71
     *
72
     * @return array
73
     */
74
    public function unredislizeArray(array $data)
75
    {
76
        $result = [];
77
        foreach ($data as $key => $value) {
78
            $result[$key] = $this->unredislize($value);
79
        }
80
81
        return $result;
82
    }
83
84
    /**
85
     * This method generate a key hash.
86
     *
87
     * @param $alias
88
     * @param $id
89
     *
90
     * @return string
91
     */
92
    public function generateKey($alias, $id)
93
    {
94
        return $alias.':'.$id;
95
    }
96
97
    public static function isSerialized($data) {
98
        // if it isn't a string, it isn't serialized
99
        if ( !is_string( $data ) )
100
            return false;
101
        $data = trim( $data );
102
        if ( 'N;' == $data )
103
            return true;
104
        if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
105
            return false;
106
        switch ( $badions[1] ) {
107
            case 'a' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
108
            case 'O' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
109
            case 's' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
110
                if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
111
                    return true;
112
                break;
113
            case 'b' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
114
            case 'i' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
115
            case 'd' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
116
                if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
117
                    return true;
118
                break;
119
        }
120
        return false;
121
    }
122
}
123