Completed
Push — master ( 8f740c...6b82b8 )
by Mehmet
02:29
created

MemcachedCache::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Soupmix\Cache;
4
5
use Soupmix\Cache\Exceptions\InvalidArgumentException;
6
use Psr\SimpleCache\CacheInterface;
7
use Memcached;
8
9
class MemcachedCache implements CacheInterface
10
{
11
12
    const PSR16_RESERVED_CHARACTERS = ['{','}','(',')','/','@',':'];
13
14
    public $handler;
15
    /**
16
     * Connect to Memcached service
17
     *
18
     * @param Memcached $handler Memcached handler object
19
     *
20
     */
21 4
    public function __construct(Memcached $handler)
22
    {
23 4
        $this->handler = $handler;
24 4
        if(Memcached::HAVE_IGBINARY){
25
            ini_set('memcached.serializer', 'igbinary');
26
        }
27 4
    }
28
29
    /**
30
     * {@inheritDoc}
31
     */
32 1
    public function get($key, $default=null)
33
    {
34
35 1
        $this->checkReservedCharacters($key);
36 1
        $value = $this->handler->get($key);
37 1
        return $value ?: $default;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 2
    public function set($key, $value, $ttl = null){
44
45 2
        $this->checkReservedCharacters($key);
46 2
        return $this->handler->set($key, $value, (int) $ttl);
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52 1
    public function delete($key){
53
54 1
        $this->checkReservedCharacters($key);
55 1
        return (bool) $this->handler->delete($key);
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 4
    public function clear(){
62 4
        return $this->handler->flush();
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 1
    public function getMultiple($keys, $default=null)
69
    {
70 1
        $defaults = array_fill(0, count($keys), $default);
71 1
        foreach ($keys as $key){
72 1
            $this->checkReservedCharacters($key);
73 1
        }
74 1
        return array_merge($this->handler->getMulti($keys), $defaults);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_merge($this...lti($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...
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80 1
    public function setMultiple($values, $ttl = null)
81
    {
82 1
        foreach ($values as $key => $value){
83 1
            $this->checkReservedCharacters($key);
84 1
        }
85 1
        return $this->handler->setMulti($values, (int) $ttl);
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 1
    public function deleteMultiple($keys)
92
    {
93 1
        foreach ($keys as $key){
94 1
            $this->checkReservedCharacters($key);
95 1
        }
96 1
        return $this->handler->deleteMulti($keys);
0 ignored issues
show
Bug introduced by
The method deleteMulti() does not exist on Memcached. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102 1
    public function increment($key, $step = 1)
103
    {
104 1
        return $this->handler->increment($key, $step);
105
    }
106
107
    /**
108
     * {@inheritDoc}
109
     */
110 1
    public function decrement($key, $step = 1)
111
    {
112 1
        return $this->handler->decrement($key, $step);
113
    }
114
115
    /**
116
     * {@inheritDoc}
117
     */
118
    public function has($key) {
119
        $this->checkReservedCharacters($key);
120
        $value = $this->handler->get($key);
121
        return Memcached::RES_NOTFOUND !== $value->getResultCode();
122
    }
123
124 3
    private function checkReservedCharacters($key)
125
    {
126 3
        if (!is_string($key)) {
127
            $message = sprintf('key %s is not a string.', $key);
128
            throw new InvalidArgumentException($message);
129
        }
130 3
        foreach (self::PSR16_RESERVED_CHARACTERS as $needle) {
131 3
            if (strpos($key, $needle) !== false) {
132
                $message = sprintf('%s string is not a legal value.', $key);
133
                throw new InvalidArgumentException($message);
134
            }
135 3
        }
136
    }
137
}