Completed
Push — master ( b9db06...c93a65 )
by Mehmet
02:22
created

RedisCache::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
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
        if (defined('Redis::SERIALIZER_IGBINARY') && extension_loaded('igbinary')) {
26
            $handler->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY);
27
        }
28 8
        $this->handler = $handler;
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 ? $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 = $value;
0 ignored issues
show
Bug introduced by
Why assign $value to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
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
    /**
59
     * {@inheritDoc}
60
     */
61 4
    public function delete($key)
62
    {
63 4
        return (bool) $this->handler->delete($key);
64
    }
65
    /**
66
     * {@inheritDoc}
67
     */
68 8
    public function clear()
69
    {
70 8
        return $this->handler->flushDb();
71
    }
72
    /**
73
     * {@inheritDoc}
74
     */
75 2
    public function getMultiple($keys, $default = null)
76
    {
77 2
        $defaults = array_fill(0, count($keys), $default);
78 2
        foreach ($keys as $key) {
79 2
            $this->checkReservedCharacters($key);
80 2
        }
81 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...
82
    }
83
    /**
84
     * {@inheritDoc}
85
     */
86 2
    public function setMultiple($values, $ttl = null)
87
    {
88 2
        foreach ($values as $key => $value) {
89 2
            $this->checkReservedCharacters($key);
90 2
        }
91 2
        if (($ttl === null) || ($ttl === 0)) {
92 2
            return $this->handler->mSet($values);
93
        }
94
95
        $return =[];
96
        foreach ($values as $key => $value) {
97
            $return[$key] =  $this->set($key, $value, $ttl);
98
        }
99
        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...
100
    }
101
    /**
102
     * {@inheritDoc}
103
     */
104 2
    public function deleteMultiple($keys)
105
    {
106 2
        foreach ($keys as $key) {
107 2
            $this->checkReservedCharacters($key);
108 2
        }
109 2
        $return =[];
110 2
        foreach ($keys as $key) {
111 2
            $return[$key] = (bool) $this->delete($key);
112 2
        }
113 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...
114
    }
115
    /**
116
     * Increment a value atomically in the cache by its step value, which defaults to 1
117
     *
118
     * @param string  $key  The cache item key
119
     * @param integer $step The value to increment by, defaulting to 1
120
     *
121
     * @return int|bool The new value on success and false on failure
122
     */
123 2
    public function increment($key, $step = 1)
124
    {
125 2
        return $this->handler->incr($key, $step);
126
    }
127
    /**
128
     * Decrement a value atomically in the cache by its step value, which defaults to 1
129
     *
130
     * @param string  $key  The cache item key
131
     * @param integer $step The value to decrement by, defaulting to 1
132
     *
133
     * @return int|bool The new value on success and false on failure
134
     */
135 2
    public function decrement($key, $step = 1)
136
    {
137 2
        return $this->handler->decr($key, $step);
138
    }
139
140
141
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    public function has($key)
147
    {
148
        $this->checkReservedCharacters($key);
149
        return $this->handler->exists($key);
150
    }
151
152 2
    private function checkReservedCharacters($key)
153
    {
154
155 2
        if (!is_string($key)) {
156
            $message = sprintf('key %s is not a string.', $key);
157
            throw new InvalidArgumentException($message);
158
        }
159 2
        foreach (self::PSR16_RESERVED_CHARACTERS as $needle) {
160 2
            if (strpos($key, $needle) !== false) {
161
                $message = sprintf('%s string is not a legal value.', $key);
162
                throw new InvalidArgumentException($message);
163
            }
164 2
        }
165 2
    }
166
}
167