Completed
Push — master ( bf7173...875f6a )
by Franck
13:42
created

Pool::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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\PsrCache;
14
15
use Apix\Cache\Adapter as CacheAdapter;
16
use Psr\Cache\CacheItemInterface as ItemInterface;
17
use Psr\Cache\CacheItemPoolInterface as ItemPoolInterface;
18
19
class Pool implements ItemPoolInterface
20
{
21
22
    /**
23
     *
24
     * @var CacheAdapter
25
     */
26
    protected $cache_adapter;
27
28
    /**
29
     * Deferred cache items to be saved later.
30
     *
31
     * @var array   Collection of \Apix\PsrCache\Item.
32
     */
33
    protected $deferred = array();
34
35
    /**
36
     * Constructor.
37
     */
38 374 View Code Duplication
    public function __construct(CacheAdapter $cache_adapter)
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...
39
    {
40 374
        $this->cache_adapter = $cache_adapter;
41
42
        $options = array(
43
            'tag_enable' => false // wether to enable tagging
44 374
        );
45 374
        $this->cache_adapter->setOptions($options);
46 374
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 170
    public function getItem($key)
52
    {
53 170
        $key = Item::normalizedKey($key);
54
55 170
        if (isset($this->deferred[$key])) {
56 170
            return $this->deferred[$key];
57 170
        }
58 170
59
        $value = $this->cache_adapter->loadKey($key);
60 170
61
        return new Item(
62
            $this->cache_adapter->removePrefixKey($key),
63
            $value,
64
            $this->cache_adapter->getTtl($key) ?: null,
65
            (bool) $value // indicates wether it is loaded from cache or not.
66 68
        );
67
    }
68 68
69 68
    /**
70 68
     * {@inheritdoc}
71 68
     */
72
    public function getItems(array $keys = array())
73 68
    {
74
        $items = array();
75
        foreach ($keys as $key) {
76
            $items[$key] = $this->getItem($key);
77
        }
78
79 68
        return $items;
80
    }
81 68
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function hasItem($key)
86
    {
87 34
        return $this->getItem($key)->isHit();
88
    }
89 34
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function clear()
94
    {
95 68
        $this->deferred = array();
96
        return $this->cache_adapter->flush(true);
97 68
    }
98 68
99 68
    /**
100
     * {@inheritdoc}
101 68
     */
102
    public function deleteItems(array $keys)
103
    {
104
        $checks = array();
105
        foreach ($keys as $key) {
106
            // Only delete from cache if it actually exists
107 34
            if($this->getItem($key)->isHit()) {
108
                $checks[] = $this->cache_adapter->delete($key);
109 34
            }
110
            unset($this->deferred[$key]);
111
        }
112
        return (bool) !in_array(false, $checks, true);
113
    }
114
115 170
    /**
116
     * {@inheritdoc}
117 170
     */
118
    public function deleteItem($key)
119 170
    {
120 170
        return $this->deleteItems(array($key));
121 170
    }
122 170
123 170
    /**
124 170
     * {@inheritdoc}
125 170
     */
126 170
    public function save(ItemInterface $item)
127
    {
128 170
        $ttl = $item->getTtlInSecond();
129
130
        $item->setHit(true);
131
        $success = $this->cache_adapter->save(
132
                        $item->get(),             // value to store
133
                        $item->getKey(),          // its key
134 34
                        null,                     // disable tags support
135
                        is_null($ttl) ? 0 : $ttl  // ttl in sec or null for ever
136 34
                    );
137
        $item->setHit($success);
138 34
139
        return $success;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144 34
     */
145
    public function saveDeferred(ItemInterface $item)
146 34
    {
147 34
        $this->deferred[$item->getKey()] = $item;
148 34
149 34
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Apix\Cache\PsrCache\Pool) is incompatible with the return type declared by the interface Psr\Cache\CacheItemPoolInterface::saveDeferred 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...
150 34
    }
151 34
152
    /**
153 34
     * {@inheritdoc}
154
     */
155
    public function commit()
156
    {
157
        foreach ($this->deferred as $key => $item) {
158
            $this->save($item);
159
            if ( $item->isHit() ) {
160
                unset($this->deferred[$key]);
161 289
            }
162
        }
163 289
164
        return empty($this->deferred);
165
    }
166
167 17
    /**
168
     * Returns the cache adapter for this pool.
169
     *
170
     * @return CacheAdapter
171
     */
172
    public function getCacheAdapter()
173
    {
174
        return $this->cache_adapter;
175
    }
176
177
    /**
178
     * Commit the deferred items ~ acts as the last resort garbage collector.
179
     */
180
    public function __destruct()
181
    {
182
        $this->commit();
183
    }
184
185
}
186