Pool   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 196
Duplicated Lines 10.71 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 4
dl 21
loc 196
ccs 77
cts 77
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 10 10 3
A getMultiple() 0 11 2
A has() 0 10 2
A clear() 0 4 1
A deleteMultiple() 0 12 2
A delete() 0 10 2
A set() 11 11 2
B setMultiple() 0 31 8
A setItemProperties() 0 6 1
A _normalizedKeys() 0 15 3
A _rethrow() 0 6 1
A getCacheAdapter() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\SimpleCache\PsrSimpleCache;
14
15
use Apix\Cache\PsrCache\Item as CacheItem;
16
use Psr\SimpleCache\CacheInterface as SimpleCacheInterface;
17
use Psr\Cache\CacheItemPoolInterface as CacheItemPool;
18
use Psr\Cache\InvalidArgumentException as CacheInvalidArgumentException;
19
20
/**
21
 * Provides a PSR-16 (SimpleCache) wrapper to PSR-6 (Cache).
22
 *
23
 * @author Franck Cassedanne <franck at ouarz.net>
24
 */
25
class Pool implements SimpleCacheInterface
26
{
27
28
    /**
29
     * @var CacheItemPool
30
     */
31
    protected $cache_item_pool;
32
33
    /**
34
     * Constructor.
35
     */
36 195
    public function __construct(CacheItemPool $cache_item_pool)
37
    {
38 195
        $this->cache_item_pool = $cache_item_pool;
39 195
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 46 View Code Duplication
    public function get($key, $default = null)
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...
45
    {
46
        try {
47 46
            $item = $this->cache_item_pool->getItem($key);
48 46
        } catch (CacheInvalidArgumentException $e) {
49 38
            self::_rethrow($e);
50
        }
51
52 8
        return $item->isHit() ? $item->get() : $default;
53 1
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 41
    public function getMultiple($keys, $default = null)
59
    {
60 41
        $keys = self::_normalizedKeys($keys);
61
62 28
        $items = array();
63 28
        foreach ($keys as $key) {
64 28
            $items[$key] = $this->get($key, $default);
65 4
        }
66
67 4
        return $items;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 23
    public function has($key)
74
    {
75
        try {
76 23
            $bool = $this->cache_item_pool->hasItem($key);
77 23
        } catch (CacheInvalidArgumentException $e) {
78 14
            self::_rethrow($e);
79
        }
80
81 9
        return $bool;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 1
    public function clear()
88
    {
89 1
        return $this->cache_item_pool->clear();
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 39
    public function deleteMultiple($keys)
96
    {
97 39
        $keys = self::_normalizedKeys($keys);
98
99
        try {
100 26
            $bool = $this->cache_item_pool->deleteItems($keys);
101 26
        } catch (CacheInvalidArgumentException $e) {
102 24
            self::_rethrow($e);
103
        }
104
105 2
        return $bool;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 16
    public function delete($key)
112
    {
113
        try {
114 16
            $bool = $this->cache_item_pool->deleteItem($key);
115 16
        } catch (CacheInvalidArgumentException $e) {
116 14
            self::_rethrow($e);
117
        }
118
119 2
        return $bool;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 21 View Code Duplication
    public function set($key, $value, $ttl = null)
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...
126
    {
127
        try {
128 21
            $item = $this->cache_item_pool->getItem($key);
129 7
            $this->setItemProperties($item, $value, $ttl);
0 ignored issues
show
Compatibility introduced by
$item of type object<Psr\Cache\CacheItemInterface> is not a sub-type of object<Apix\Cache\PsrCache\Item>. It seems like you assume a concrete implementation of the interface Psr\Cache\CacheItemInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
130 21
        } catch (CacheInvalidArgumentException $e) {
131 14
            self::_rethrow($e);
132
        }
133
134 7
        return $this->cache_item_pool->save($item);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 45
    public function setMultiple($values, $ttl = null)
141
    {
142 45
        if (!is_array($values) && !$values instanceof \Traversable) {
143 13
            throw new InvalidArgumentException(sprintf(
144 13
                'Expected an array or a \Traversable, got `%s`.',
145 13
                gettype($values)
146 13
            ));
147
        }
148
149 32
        $keys = array_keys((array) $values);
150
151
        try {
152 32
            $items = $this->cache_item_pool->getItems($keys);
153 32
        } catch (CacheInvalidArgumentException $e) {
154 24
            self::_rethrow($e);
155
        }
156
157 8
        $success = true;
158 8
        foreach ($items as $key => $item) {
159
            try {
160 8
                $this->setItemProperties($item, $values[$key], $ttl);
161 8
            } catch (CacheInvalidArgumentException $e) {
162 1
                self::_rethrow($e);
163
            }
164
165
            $success = $success
166 7
                       && $this->cache_item_pool->saveDeferred($item);
167 7
        }
168
169 7
        return $success && $this->cache_item_pool->commit();
170
    }
171
172
    /**
173
     * Sets the properties of an item object.
174
     *
175
     * @param CacheItem              $item
176
     * @param mixed                  $value The item value (unserialized)
177
     * @param integer|\DateInterval|null $ttl
178
     *
179
     * @return static The invoked object.
180
     */
181 13
    protected function setItemProperties(
182
        CacheItem $item, $value, $ttl = null
183
    ) {
184 13
        return $item->set($value)
185 13
                    ->expiresAfter($ttl);
186
    }
187
188 80
    private static function _normalizedKeys($keys)
189
    {
190 80
        if (!is_array($keys)) {
191 52
            if (!$keys instanceof \Traversable) {
192 26
                throw new InvalidArgumentException(sprintf(
193 26
                    'Expected an array or a \Traversable, got `%s`.',
194 26
                    gettype($keys)
195 26
                ));
196
            }
197
198 26
            $keys = iterator_to_array($keys, false);
199 26
        }
200
201 54
        return $keys;
202
    }
203
204 129
    private static function _rethrow(CacheInvalidArgumentException $e)
205
    {
206 129
        throw new InvalidArgumentException(
207 129
            $e->getMessage(), $e->getCode(), $e
208 129
        );
209
    }
210
211
    /**
212
     * Returns the cache adapter for this pool.
213
     *
214
     * @return CacheAdapter
215
     */
216 7
    public function getCacheAdapter()
217
    {
218 7
        return $this->cache_item_pool->getCacheAdapter();
219
    }
220
}
221