Completed
Push — master ( 1fbafa...7aa89c )
by Ryan
03:14
created

Service::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.864

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 2
cts 5
cp 0.4
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2.864
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
        // determine path of file
54 8
        $path = $root . '/../config/settings/' . $environment . '/cache.yml';
55 8
        if (!file_exists($path) && $environment != '.') {
56
            $path = $root . '/config/settings/cache.yml';
57
        }
58 8
        if (!file_exists($path) && $environment != '.') {
59
            $path = $root . '/../config/settings/cache.yml';
60
        }
61 8
        if (!file_exists($path)) {
62 8
            return;
63
        }
64
65
        if (class_exists('\Memcache')) {
66
            // read configuration
67
            $config = Yaml::parse(file_get_contents($path));
68
            $this->host = $config['settings']['host'];
69
            $this->port = $config['settings']['port'];
70
            $this->engine = new \Memcache();
71
            return;
72
        }
73
        $this->engine = new FileCache($this->root);
74
    }
75
76 1
    public function delete($key, $timeout = 0)
77
    {
78 1
        $result = @$this->engine->pconnect($this->host, $this->port);
79
        if ($result === false) {
80
            return false;
81
        }
82
83
        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...
84
    }
85
86 1
    public function set($key, $value, $expire = 0, $flag = 2)
87
    {
88 1
        $result = @$this->engine->pconnect($this->host, $this->port);
89
        if ($result === false) {
90
            return false;
91
        }
92
93
        return $this->engine->set($key, $value, $flag, $expire);
94
    }
95
96 1
    public function get($key, $flag = 2)
97
    {
98 1
        $result = @$this->engine->pconnect($this->host, $this->port);
99
        if ($result === false) {
100
            return false;
101
        }
102
103
        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...
104
    }
105
106 1
    public function getSetGet($key, Closure $callback, $ttl = 0, $flag = 2)
107
    {
108 1
        $result = @$this->engine->pconnect($this->host, $this->port);
109
        if ($result === false) {
110
            return false;
111
        }
112
        $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...
113
        if ($data === false) {
114
            $data = $callback();
115
            if ($data !== false) {
116
                $this->engine->set($key, $data, $flag, $ttl);
117
            }
118
        }
119
120
        return $data;
121
    }
122
123 2
    public function getSetGetBatch(Array &$items, $ttl = 0, $flag = 2)
124
    {
125 2
        foreach ($items as $item) {
126 2
            if (!is_callable($item)) {
127 2
                throw new Exception('each item must have a callback defined');
128
            }
129
        }
130 1
        $result = @$this->engine->pconnect($this->host, $this->port);
131
        if ($result === false) {
132
            return false;
133
        }
134
        $data = $this->engine->get(array_keys($items));
135
        foreach ($items as $key => &$item) {
136
            if (!isset($data[$key]) || $data[$key] === false) {
137
                $items[$key] = $item();
138
            } else {
139
                $items[$key] = $data[$key];
140
            }
141
            if ($items[$key] !== false) {
142
                $this->engine->set($key, $items[$key], $flag, $ttl);
143
            }
144
        }
145
146
        return true;
147
    }
148
149 1
    public function getBatch(Array &$items, $flag = 2)
150
    {
151 1
        $count = sizeof($items);
152 1
        $result = @$this->engine->pconnect($this->host, $this->port);
153
        if ($result === false) {
154
            return false;
155
        }
156
        $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...
157
        $hits = 0;
158
        foreach ($items as $key => $item) {
159
            if (array_key_exists($key, $data)) {
160
                $items[$key] = $data[$key];
161
                $hits++;
162
            }
163
        }
164
        return $hits == $count;
165
    }
166
167 1
    public function deleteBatch(Array $items, $timeout = 0)
168
    {
169 1
        $result = @$this->engine->pconnect($this->host, $this->port);
170
        if ($result === false) {
171
            return false;
172
        }
173
        foreach ($items as $item) {
174
            $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...
175
        }
176
177
        return true;
178
    }
179
}
180