Service::deleteBatch()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3.0261
1
<?php
2
/**
3
 * Opine\Cache
4
 *
5
 * Copyright (c)2013, 2014 Ryan Mahoney, https://github.com/Opine-Org <[email protected]>
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
namespace Opine\Cache;
26
27
use Exception;
28
use Closure;
29
use Opine\Interfaces\Cache as CacheInterface;
30
use Symfony\Component\Yaml\Yaml;
31
32
class Service implements CacheInterface
33
{
34
    private $engine;
35
    private $host = false;
36
    private $port = false;
37
    private $root;
38
39 8
    public function __construct($root)
40
    {
41 8
        $this->root = $root;
42
43
        // determine config environment
44 8
        $environment = 'default';
45 8
        $test = getenv('OPINE_ENV');
46 8
        if ($test !== false) {
47
            $environment = $test;
48
        }
49 8
        if ($environment == 'default') {
50 8
            $environment = '.';
51
        }
52
53 8
        if (class_exists('\Memcache')) {
54
            // determine path of file
55
            $path = $root . '/../config/settings/' . $environment . '/cache.yml';
56
            if (!file_exists($path)) {
57
                $path = $root . '/config/settings/cache.yml';
58
            }
59
            if (!file_exists($path)) {
60
                $path = $root . '/../config/settings/cache.yml';
61
            }
62
            if (!file_exists($path)) {
63
                fwrite(STDERR, print_r([
64
                    $root, $path
65
                ], TRUE));
66
                return;
67
            }
68
69
            // read configuration
70
            $config = Yaml::parse(file_get_contents($path));
71
            $this->host = $config['settings']['host'];
72
            $this->port = $config['settings']['port'];
73
            $this->engine = new \Memcache();
74
            return;
75
        }
76 8
        $this->engine = new FileCache($this->root);
77 8
    }
78
79 1
    public function delete($key, $timeout = 0)
80
    {
81 1
        $result = @$this->engine->pconnect($this->host, $this->port);
82 1
        if ($result === false) {
83
            return false;
84
        }
85
86 1
        return $this->engine->delete($key, $timeout);
0 ignored issues
show
Unused Code introduced by
The call to FileCache::delete() has too many arguments starting with $timeout.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
87
    }
88
89 1
    public function set($key, $value, $expire = 0, $flag = 2)
90
    {
91 1
        $result = @$this->engine->pconnect($this->host, $this->port);
92 1
        if ($result === false) {
93
            return false;
94
        }
95
96 1
        return $this->engine->set($key, $value, $flag, $expire);
97
    }
98
99 4
    public function get($key, $flag = 2)
100
    {
101 4
        $result = @$this->engine->pconnect($this->host, $this->port);
102 4
        if ($result === false) {
103
            return false;
104
        }
105
106 4
        return $this->engine->get($key, $flag);
0 ignored issues
show
Unused Code introduced by
The call to FileCache::get() has too many arguments starting with $flag.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
107
    }
108
109 1
    public function getSetGet($key, Closure $callback, $ttl = 0, $flag = 2)
110
    {
111 1
        $result = @$this->engine->pconnect($this->host, $this->port);
112 1
        if ($result === false) {
113
            return false;
114
        }
115 1
        $data = $this->engine->get($key, $flag);
0 ignored issues
show
Unused Code introduced by
The call to FileCache::get() has too many arguments starting with $flag.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
116 1
        if ($data === false) {
117 1
            $data = $callback();
118 1
            if ($data !== false) {
119 1
                $this->engine->set($key, $data, $flag, $ttl);
120
            }
121
        }
122
123 1
        return $data;
124
    }
125
126 2
    public function getSetGetBatch(Array &$items, $ttl = 0, $flag = 2)
127
    {
128 2
        foreach ($items as $item) {
129 2
            if (!is_callable($item)) {
130 2
                throw new Exception('each item must have a callback defined');
131
            }
132
        }
133 1
        $result = @$this->engine->pconnect($this->host, $this->port);
134 1
        if ($result === false) {
135
            return false;
136
        }
137 1
        $data = $this->engine->get(array_keys($items));
138 1
        foreach ($items as $key => &$item) {
139 1
            if (!isset($data[$key]) || $data[$key] === false) {
140 1
                $items[$key] = $item();
141
            } else {
142 1
                $items[$key] = $data[$key];
143
            }
144 1
            if ($items[$key] !== false) {
145 1
                $this->engine->set($key, $items[$key], $flag, $ttl);
146
            }
147
        }
148
149 1
        return true;
150
    }
151
152 1
    public function getBatch(Array &$items, $flag = 2)
153
    {
154 1
        $count = sizeof($items);
155 1
        $result = @$this->engine->pconnect($this->host, $this->port);
156 1
        if ($result === false) {
157
            return false;
158
        }
159 1
        $data = $this->engine->get(array_keys($items), $flag);
0 ignored issues
show
Unused Code introduced by
The call to FileCache::get() has too many arguments starting with $flag.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
160 1
        $hits = 0;
161 1
        foreach ($items as $key => $item) {
162 1
            if (array_key_exists($key, $data)) {
163 1
                $items[$key] = $data[$key];
164 1
                $hits++;
165
            }
166
        }
167 1
        return $hits == $count;
168
    }
169
170 1
    public function deleteBatch(Array $items, $timeout = 0)
171
    {
172 1
        $result = @$this->engine->pconnect($this->host, $this->port);
173 1
        if ($result === false) {
174
            return false;
175
        }
176 1
        foreach ($items as $item) {
177 1
            $this->engine->delete($item, $timeout);
0 ignored issues
show
Unused Code introduced by
The call to FileCache::delete() has too many arguments starting with $timeout.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
178
        }
179
180 1
        return true;
181
    }
182
}
183