BasicCacheItemPoolTrait   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
wmc 13
eloc 31
dl 0
loc 80
ccs 32
cts 33
cp 0.9697
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A commit() 0 23 4
A saveDeferred() 0 9 1
A checkQueueNamespace() 0 11 3
A deleteItems() 0 11 3
A getItems() 0 10 2
1
<?php namespace Comodojo\Cache\Traits;
2
3
use \Comodojo\Cache\Components\ItemsIterator;
0 ignored issues
show
Bug introduced by
The type Comodojo\Cache\Components\ItemsIterator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
4
use \Psr\Cache\CacheItemInterface;
5
6
/**
7
 * @package     Comodojo Cache
8
 * @author      Marco Giovinazzi <[email protected]>
9
 * @license     MIT
10
 *
11
 * LICENSE:
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
 * THE SOFTWARE.
20
 */
21
22
trait BasicCacheItemPoolTrait {
23
24
    private $queue = [];
25
26 3
    public function getItems(array $keys = []) {
27
28 3
        $items = [];
29
30 3
        foreach ( $keys as $key ) {
31
32 2
            $items[$key] = $this->getItem($key);
0 ignored issues
show
Bug introduced by
The method getItem() does not exist on Comodojo\Cache\Traits\BasicCacheItemPoolTrait. Did you maybe mean getItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
            /** @scrutinizer ignore-call */ 
33
            $items[$key] = $this->getItem($key);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
33
        }
34
35 3
        return $items;
36
37
    }
38
39 3
    public function deleteItems(array $keys) {
40
41 3
        $result = [];
42
43 3
        foreach ( $keys as $key ) {
44
45 3
            $result[] = $this->deleteItem($key);
0 ignored issues
show
Bug introduced by
The method deleteItem() does not exist on Comodojo\Cache\Traits\BasicCacheItemPoolTrait. Did you maybe mean deleteItems()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
            /** @scrutinizer ignore-call */ 
46
            $result[] = $this->deleteItem($key);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
47
        }
48
49 3
        return in_array(false, $result) ? false : true;
50
51
    }
52
53 3
    public function saveDeferred(CacheItemInterface $item) {
54
55 3
        $this->checkQueueNamespace(true);
56
57 3
        $namespace = $this->getNamespace();
0 ignored issues
show
Bug introduced by
It seems like getNamespace() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        /** @scrutinizer ignore-call */ 
58
        $namespace = $this->getNamespace();
Loading history...
58
59 3
        $this->queue[$namespace][$item->getKey()] = $item;
60
61 3
        return true;
62
63
    }
64
65 3
    public function commit() {
66
67 3
        $result = [];
68
69 3
        $active_namespace = $this->getNamespace();
70
71 3
        foreach ( $this->queue as $namespace => $queue ) {
72
73 3
            $this->setNamespace($namespace);
0 ignored issues
show
Bug introduced by
It seems like setNamespace() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            $this->/** @scrutinizer ignore-call */ 
74
                   setNamespace($namespace);
Loading history...
74
75 3
            foreach ( $queue as $key => $item ) {
76
77 3
                $result[] = $this->save($item);
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
                /** @scrutinizer ignore-call */ 
78
                $result[] = $this->save($item);
Loading history...
78
79
            }
80
81
        }
82
83 3
        $this->queue = [];
84
85 3
        $this->setNamespace($active_namespace);
86
87 3
        return in_array(false, $result) ? false : true;
88
89
    }
90
91 3
    private function checkQueueNamespace($create = false) {
92
93 3
        $namespace = $this->getNamespace();
94
95 3
        if ( array_key_exists($namespace, $this->queue) ) {
96 1
            return true;
97 3
        } else if ( $create ) {
98 3
            $this->queue[$namespace] = [];
99 3
            return true;
100
        } else {
101
            return false;
102
        }
103
104
    }
105
106
}
107