Completed
Push — master ( 7a257e...017c35 )
by Leny
109:58 queued 102:36
created

ViewReferenceRedisTool::redislize()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6667
cc 3
eloc 4
nc 2
nop 1
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
        // unserialize data if we can
38
        $unserializedData = @unserialize($data);
39
        if ($unserializedData !== false) {
40
            return $unserializedData;
41
        }
42
43
        return $data;
44
    }
45
46
    /**
47
     * Redislize an array (key, valueToRedislize).
48
     *
49
     * @param array $data
50
     *
51
     * @return array
52
     */
53
    public function redislizeArray(array $data)
54
    {
55
        $result = [];
56
        foreach ($data as $key => $value) {
57
            if (!is_array($value)) {
58
                $result[$key] = $this->redislize($value);
59
            }
60
        }
61
62
        return $result;
63
    }
64
65
    /**
66
     * unredislize an array (key, valueToUnredislize).
67
     *
68
     * @param array $data
69
     *
70
     * @return array
71
     */
72
    public function unredislizeArray(array $data)
73
    {
74
        $result = [];
75
        foreach ($data as $key => $value) {
76
            $result[$key] = $this->unredislize($value);
77
        }
78
79
        return $result;
80
    }
81
82
    /**
83
     * This method generate a key hash.
84
     *
85
     * @param $alias
86
     * @param $id
87
     *
88
     * @return string
89
     */
90
    public function generateKey($alias, $id)
91
    {
92
        return $alias.':'.$id;
93
    }
94
}
95