Completed
Push — master ( 981129...d76060 )
by Mehmet
02:32
created

RedisCache::unserialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Soupmix\Cache;
4
5
use Soupmix\Cache\Exceptions\InvalidArgumentException;
6
use Psr\SimpleCache\CacheInterface;
7
use Redis;
8
9
class RedisCache implements CacheInterface
10
{
11
    const PSR16_RESERVED_CHARACTERS = ['{','}','(',')','/','@',':'];
12
13
    private $handler = null;
14
15
    private $serializer = 'PHP';
16
17
    /**
18
     * Connect to Redis service
19
     *
20
     * @param Redis $handler Configuration values that has dbIndex name and host's IP address
21
     *
22
     */
23 8
    public function __construct(Redis $handler)
24
    {
25 8
        $this->handler = $handler;
26 8
        if (function_exists('igbinary_serialize')) {
27
            $this->serializer = 'IGBINARY';
28
        }
29 8
    }
30
31
    public function getConnection()
32
    {
33
        return $this->handler;
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39 2
    public function get($key, $default=null)
40
    {
41 2
        $value = $this->handler->get($key);
42 2
        return $value ? $this->unserialize($value) : $default;
43
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48 2
    public function set($key, $value, $ttl = null)
49
    {
50 2
        $ttl = (int) $ttl;
51 2
        $value = $this->serialize($value);
52 2
        if($ttl === 0 ){
53 2
            return $this->handler->set($key, $value);
54
        }
55
        return $this->handler->set($key, $value, $ttl);
56
    }
57
58 2
    private function serialize($value)
59
    {
60 2
        return ($this->serializer === 'IGBINARY') ? igbinary_serialize($value) : serialize($value);
61
    }
62
63 2
    private function unserialize($value)
64
    {
65 2
        return ($this->serializer === 'IGBINARY') ? igbinary_unserialize($value) : unserialize($value);
66
    }
67
    /**
68
     * {@inheritDoc}
69
     */
70 4
    public function delete($key)
71
    {
72 4
        return (bool) $this->handler->delete($key);
73
    }
74
    /**
75
     * {@inheritDoc}
76
     */
77 8
    public function clear()
78
    {
79 8
        return $this->handler->flushDb();
80
    }
81
    /**
82
     * {@inheritDoc}
83
     */
84 2
    public function getMultiple($keys, $default=null)
85
    {
86 2
        $defaults = array_fill(0, count($keys), $default);
87 2
        foreach ($keys as $key){
88 2
            $this->checkReservedCharacters($key);
89 2
        }
90 2
        return array_merge(array_combine($keys, $this->handler->mGet($keys)), $defaults);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge(array...et($keys)), $defaults); (array) is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::getMultiple of type Psr\SimpleCache\iterable.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
91
    }
92
    /**
93
     * {@inheritDoc}
94
     */
95 2
    public function setMultiple($values, $ttl = null)
96
    {
97 2
        foreach ($values as $key => $value){
98 2
            $this->checkReservedCharacters($key);
99 2
        }
100 2
        if (($ttl === null) || ($ttl === 0)) {
101 2
            return $this->handler->mSet($values);
102
        }
103
104
        $return =[];
105
        foreach ($values as $key=>$value) {
106
            $return[$key] =  $this->set($key, $value, $ttl);
107
        }
108
        return $return;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $return; (array) is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::setMultiple of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
109
    }
110
    /**
111
     * {@inheritDoc}
112
     */
113 2
    public function deleteMultiple($keys)
114
    {
115 2
        foreach ($keys as $key){
116 2
            $this->checkReservedCharacters($key);
117 2
        }
118 2
        $return =[];
119 2
        foreach ($keys as $key) {
120 2
            $return[$key] = (bool) $this->delete($key);
121 2
        }
122 2
        return $return;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $return; (array) is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::deleteMultiple of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
123
    }
124
    /**
125
     * Increment a value atomically in the cache by its step value, which defaults to 1
126
     *
127
     * @param string  $key  The cache item key
128
     * @param integer $step The value to increment by, defaulting to 1
129
     *
130
     * @return int|bool The new value on success and false on failure
131
     */
132 2
    public function increment($key, $step = 1)
133
    {
134 2
        return $this->handler->incr($key, $step);
135
    }
136
    /**
137
     * Decrement a value atomically in the cache by its step value, which defaults to 1
138
     *
139
     * @param string  $key  The cache item key
140
     * @param integer $step The value to decrement by, defaulting to 1
141
     *
142
     * @return int|bool The new value on success and false on failure
143
     */
144 2
    public function decrement($key, $step = 1)
145
    {
146 2
        return $this->handler->decr($key, $step);
147
    }
148
149
150
151
152
    /**
153
     * {@inheritDoc}
154
     */
155
    public function has($key) {
156
        $this->checkReservedCharacters($key);
157
        return $this->handler->exists($key);
158
    }
159
160 2
    private function checkReservedCharacters($key)
161
    {
162
163 2
        if (!is_string($key)) {
164
            $message = sprintf('key %s is not a string.', $key);
165
            throw new InvalidArgumentException($message);
166
        }
167 2
        foreach (self::PSR16_RESERVED_CHARACTERS as $needle) {
168 2
            if (strpos($key, $needle) !== false) {
169
                $message = sprintf('%s string is not a legal value.', $key);
170
                throw new InvalidArgumentException($message);
171
            }
172 2
        }
173 2
    }
174
}
175