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