Completed
Pull Request — master (#7)
by Thomas
13:03
created

MemcacheIndexer::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix\Cache\Indexer;
14
15
use Apix\Cache\Memcache;
16
use Apix\Cache\Serializer;
17
18
/**
19
 * Memcached index.
20
 *
21
 * @author Franck Cassedanne <franck at ouarz.net>
22
 */
23
class MemcacheIndexer extends AbstractIndexer
24
{
25
26
    const DIRTINESS_THRESHOLD = 100;
27
28
    /**
29
     * Holds an instane of a serializer.
30
     * @var Serializer\Stringset
31
     */
32
    protected $serializer;
33
34
    /**
35
     * Holds the index store engine.
36
     * @var \Apix\Cache\Memcache
37
     */
38
    protected $engine;
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param string               $name
44
     * @param \Apix\Cache\Memcache $engine
45
     */
46
    public function __construct($name, Memcache $engine)
47
    {
48
        $this->name   = $name;
49
        $this->engine = $engine;
50
51
        $this->serializer = new Serializer\Stringset();
52
    }
53
54
    /**
55
     * Gets the adapter.
56
     *
57
     * @return \Memcache
58
     */
59
    public function getAdapter()
60
    {
61
        return $this->engine->getAdapter();
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 View Code Duplication
    public function add($elements)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        $str = $this->serializer->serialize((array) $elements);
70
71
        $success = $this->getAdapter()->append($this->name, $str);
72
73
        if (false === $success) {
74
            $success = $this->getAdapter()->add($this->name, $str);
75
        }
76
77
        return (boolean) $success;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (bool) $success; (boolean) is incompatible with the return type declared by the interface Apix\Cache\Indexer\Adapter::add of type Apix\Cache\Indexer\Returns.

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...
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function remove($elements)
84
    {
85
        $str = $this->serializer->serialize((array) $elements, '-');
86
87
        return (boolean) $this->getAdapter()->append($this->name, $str);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return (bool) $this->get...end($this->name, $str); (boolean) is incompatible with the return type declared by the interface Apix\Cache\Indexer\Adapter::remove of type Apix\Cache\Indexer\Returns.

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...
88
    }
89
90
    /**
91
     * Returns the indexed items.
92
     *
93
     * @return boolean Returns True on success or Null on failure.
94
     */
95
    public function load()
96
    {
97
        // &$cas_token holds the unique 64-bit float token generated
98
        // by memcache for the named item @see \Memcached::get
99
        $str = $this->engine->get($this->name);
100
101
        if (null === $str) {
102
            return null;
103
        }
104
105
        $this->items = $this->serializer->unserialize($str);
106
107
        return $this->items;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->items; (array) is incompatible with the return type declared by the interface Apix\Cache\Indexer\Adapter::load of type Apix\Cache\Indexer\Returns.

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...
108
    }
109
110
    /**
111
     * Purge atomically the index.
112
     *
113
     * @param double|null $cas_token
114
     * @return float $cas_token The Memcache CAS token.
115
     */
116
    protected function purge($cas_token)
117
    {
118
        $str = $this->serializer->serialize($this->items);
119
120
        return $this->getAdapter()->cas($cas_token, $this->name, $str);
121
    }
122
123
}
124